打印从根到叶的所有路径

时间:2019-11-23 13:17:38

标签: rust binary-tree

我有一个二叉树结构,我正在尝试使用Rust中的fmt::Display特性来打印从根到叶节点的所有路径。

例如,对于以下树:

    a
   / \
  b   e
 / \
c   d  

我想要类似以下内容的输出。

a -> b -> c -> Nil
a -> b -> c -> Nil
a -> b -> d -> Nil
a -> b -> d -> Nil
a -> e -> Nil
a -> e -> Nil

这就是我所拥有的:

use std::fmt;

enum Node {
    Nil,
    Data(String, Box<Node>, Box<Node>),
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Node::Nil => write!(f, "Nil"),
            Node::Data(data, left, right) => write!(f, "{} -> {} , {}", data, *left, *right),
        }
    }
}

fn main() {
    let tree = Node::Data(
        String::from("a"),
        Box::new(Node::Data(
            String::from("b"),
            Box::new(Node::Data(
                String::from("c"),
                Box::new(Node::Nil),
                Box::new(Node::Nil),
            )),
            Box::new(Node::Data(
                String::from("d"),
                Box::new(Node::Nil),
                Box::new(Node::Nil),
            )),
        )),
        Box::new(Node::Data(
            String::from("e"),
            Box::new(Node::Nil),
            Box::new(Node::Nil),
        )),
    );

    println!("{}", tree);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=76af35929c56ee8c583b019a9380a9f4

我不确定是否可以将父节点的信息传递给子节点。

1 个答案:

答案 0 :(得分:2)

use std::fmt;

#[derive(Clone)]
enum Node {
    Nil,
    Data(String, Box<Node>, Box<Node>),
}


impl Node {
    fn fmt_tree(&self, path: &mut Vec<Node>) {
        match self {
            Node::Nil => {
                for node in path {
                    print!("{} -> ", node);
                }
                println!("{}", self);
            },
            Node::Data(_, left, right) => {
                path.push(self.clone());
                left.fmt_tree(path);
                right.fmt_tree(path);
                path.pop();
            },
        }
    }
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Node::Nil => write!(f, "Nil"),
            Node::Data(s, _, _) => write!(f, "{}", s)
        }
    }
}

fn main() {

    let tree = Node::Data(String::from("a"),
                            Box::new(Node::Data(String::from("b"),
                                        Box::new(Node::Data(String::from("c"),
                                                    Box::new(Node::Nil), Box::new(Node::Nil))),
                                        Box::new(Node::Data(String::from("d"),
                                                    Box::new(Node::Nil), Box::new(Node::Nil))))),
                            Box::new(Node::Data(String::from("e"),
                                        Box::new(Node::Nil), Box::new(Node::Nil))));

    tree.fmt_tree(&mut Vec::new());
}

打印:

a -> b -> c -> Nil
a -> b -> c -> Nil
a -> b -> d -> Nil
a -> b -> d -> Nil
a -> e -> Nil
a -> e -> Nil

确切地说,使用简单的递归深度优先二叉树搜索(在fmt_tree中)

相关问题