示例:执行后序树遍历的Java程序
class Node {
int item;
Node left, right;
public Node(int key) {
item = key;
left = right = null;
}
}
class Tree {
// Root of Binary Tree
Node root;
Tree() {
root = null;
}
void postorder(Node node) {
if (node == null)
return;
// traverse the left child
postorder(node.left);
// traverse the right child
postorder(node.right);
// traverse the root node
System.out.print(node.item + "->");
}
public static void main(String[] args) {
// create an object of Tree
Tree tree = new Tree();
// create nodes of the tree
tree.root = new Node(1);
tree.root.left = new Node(12);
tree.root.right = new Node(9);
// child nodes of left child
tree.root.left.left = new Node(5);
tree.root.left.right = new Node(6);
// postorder tree traversal
System.out.println("Postorder traversal");
tree.postorder(tree.root);
}
}
输出
Postorder traversal
5->6->12->9->1->
在上面的示例中,我们已经用Java实现了树数据结构。在这里,我们正在执行树的后置遍历。
推荐读物 :
- 二叉树数据结构
- 树遍历
- Java中的二叉树实现