1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
| public class TreeTraversal {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
Node root = binaryTree.createTree();
System.out.println("====== 前序 =======");
binaryTree.preOrder(root);
System.out.println("====== 中序 =======");
binaryTree.inOrder(root);
System.out.println("====== 後序 =======");
binaryTree.postOrder(root);
System.out.println("====== 前序搜尋 =======");
Node findNode = binaryTree.preOrderSearch(root, 5);
System.out.println(findNode);
}
}
class BinaryTree {
private Node root;
public BinaryTree() {
}
public BinaryTree(Node root) {
this.root = root;
}
public Node createTree() {
Node node1 = new Node(1, "Bill");
Node node2 = new Node(2, "Alice");
Node node3 = new Node(3, "Mary");
Node node4 = new Node(4, "Tom");
Node node5 = new Node(5, "Alex");
node1.left = node2;
node1.right = node3;
node3.left = node4;
node3.right = node5;
return node1;
}
public void preOrder(Node node) {
if (node == null) return;
System.out.println(node);
preOrder(node.left);
preOrder(node.right);
}
public void inOrder(Node node) {
if (node == null) return;
inOrder(node.left);
System.out.println(node);
inOrder(node.right);
}
public void postOrder(Node node) {
if (node == null) return;
postOrder(node.left);
postOrder(node.right);
System.out.println(node);
}
public Node preOrderSearch(Node node, int searchId) {
if (node == null) return null;
if (node.id == searchId) {
return node;
}
Node rtn = preOrderSearch(node.left, searchId);
if (rtn != null) {
return rtn;
}
return preOrderSearch(node.right, searchId);
}
}
class Node {
Node(int id, String name) {
this.id = id;
this.name = name;
}
public int id;
public String name;
public Node left;
public Node right;
@Override
public String toString() {
return
"id=" + id +
", name=" + name ;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| public class TreeTraversal {
public static void main(String[] args) {
Node node1 = new Node(1, "Bill");
Node node2 = new Node(2, "Alice");
Node node3 = new Node(3, "Mary");
Node node4 = new Node(4, "Tom");
Node node5 = new Node(5, "Alex");
node1.left = node2;
node1.right = node3;
node3.left = node4;
node3.right = node5;
BinaryTree binaryTree = new BinaryTree(node1);
System.out.println("====== 前序 =======");
binaryTree.preOrder();
System.out.println("====== 中序 =======");
binaryTree.inOrder();
System.out.println("====== 後序 =======");
binaryTree.postOrder();
}
}
class BinaryTree {
private Node root;
public BinaryTree(Node root) {
this.root = root;
}
public void preOrder() {
this.root.preOrder();
}
public void postOrder() {
this.root.postOrder();
}
public void inOrder() {
this.root.inOrder();
}
}
class Node {
Node(int id, String name) {
this.id = id;
this.name = name;
}
public int id;
public String name;
public Node left;
public Node right;
@Override
public String toString() {
return
"id=" + id +
", name=" + name ;
}
public void preOrder() {
if (this == null) return;
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
public void inOrder() {
if (this == null) return;
if (this.left != null) {
this.left.preOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.preOrder();
}
}
public void postOrder() {
if (this == null) return;
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
System.out.println(this);
}
}
|
====== 前序 =======
id=1, name=Bill
id=2, name=Alice
id=3, name=Mary
id=4, name=Tom
id=5, name=Alex
====== 中序 =======
id=2, name=Alice
id=1, name=Bill
id=3, name=Mary
id=4, name=Tom
id=5, name=Alex
====== 後序 =======
id=2, name=Alice
id=3, name=Mary
id=4, name=Tom
id=5, name=Alex
id=1, name=Bill