-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
247 lines (220 loc) · 6.33 KB
/
BinaryTree.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package Register;
/**
* Created by lanyage on 2018/5/14.
*/
import java.util.LinkedList;
/**
* 二叉树有非常良好的性质,虽然其是树种的一个特例,但是其可以用来表示一般的树
*/
class Node {
int data;
Node parent;
Node left;
Node right;
Node root;
public Node() {
this(null, null, null, null);
}
public Node(int data) {
this.data = data;
}
public Node(Node parent, Node left, Node right, Node root) {
this.parent = parent;
this.left = left;
this.right = right;
this.root = null;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
", left=" + left +
", right=" + right +
", root=" + root +
'}';
}
}
public class BinaryTree {
private Node genesis = new Node();
/**
* 二叉树的初始化
* @param data root节点的内容
* @return
*/
private Node init(int data) {
genesis.root = new Node(data);
genesis.root.parent = genesis;
return genesis.root;
}
public void addLeftNodeAfter(Node parent, Node left) {
parent.left = left;
left.parent = parent;
}
public void addRightNodeAfter(Node parent, Node right) {
parent.right = right;
right.parent = parent;
}
/**
* 递归前序遍历
* @param root
*/
public void preOrder(Node root) {
if (root == null) return;
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
/**
* 迭代前序遍历
* @param root
*/
public void preOrderII(Node root) {
System.out.print("前序迭代:");
//需要一个辅助的
LinkedList<Node> stack = new LinkedList<>();
class Walker {
private void walk(Node node) {
while (node != null) {
System.out.print(node.data + " ");
stack.push(node.right);
node = node.left;
}
}
}
Walker walker = new Walker();
walker.walk(root);
while (!stack.isEmpty()) {
Node curNode = stack.pop();
if (curNode != null) {
walker.walk(curNode);
}
}
}
/**
* 递归中序遍历
* @param root
*/
public void inOrder(Node root) {
if (root == null) return;
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
/**
* 迭代中序遍历
* @param root
*/
public void inOrderII(Node root) {
System.out.print("中序迭代:");
LinkedList<Node> stack = new LinkedList<>();
class Walker {
private void walk(Node node) {
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
Walker walker = new Walker();
walker.walk(root);
while (!stack.isEmpty()) {
Node left = stack.pop();
System.out.print(left.data + " ");
if (left.right != null) {
walker.walk(left.right);
}
}
}
/**
* 递归后序遍历
* @param root
*/
public void suffixOrder(Node root) {
if (root == null) return;
suffixOrder(root.left);
suffixOrder(root.right);
System.out.print(root.data + " ");
}
/**
* 迭代后续遍历
* @param root
*/
public void suffixOrderII(Node root) {
System.out.print("后序迭代:");
LinkedList<Node> stack = new LinkedList<>();
class Walker {
private void walk(Node node) {
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
Walker walker = new Walker();
walker.walk(root);
while (!stack.isEmpty()) {
Node currNode = stack.pop();
System.out.print(currNode.data + " ");
Node parent = currNode.parent;
if (parent.right != null && currNode == parent.left) {
walker.walk(parent.right);
}
}
}
public void level(Node root) {
System.out.print("层次遍历:");
LinkedList<Node> queue = new LinkedList<>();
queue.push(root);
while (!queue.isEmpty()) {
Node currNode = queue.pollLast();
System.out.print(currNode.data + " ");
Node currLeft = currNode.left;
Node currRight = currNode.right;
if (currLeft != null) {
queue.push(currLeft);
}
if (currRight != null) {
queue.push(currRight);
}
}
}
public static void main(String[] args) {
/** 新建树,会有一个创世节点*/
BinaryTree binaryTree = new BinaryTree();
/** 初始化一个根节点 */
Node root = binaryTree.init(0);
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
Node four = new Node(4);
Node five = new Node(5);
Node six = new Node(6);
Node seven = new Node(7);
Node eight = new Node(8);
binaryTree.addLeftNodeAfter(three, seven);
binaryTree.addRightNodeAfter(three, eight);
binaryTree.addLeftNodeAfter(one, three);
binaryTree.addRightNodeAfter(one, four);
binaryTree.addLeftNodeAfter(two, five);
binaryTree.addRightNodeAfter(two, six);
binaryTree.addLeftNodeAfter(root, one);
binaryTree.addRightNodeAfter(root, two);
System.out.print("前序递归:");
binaryTree.preOrder(root);
System.out.println();
binaryTree.preOrderII(root);
System.out.println();
System.out.print("中序递归:");
binaryTree.inOrder(root);
System.out.println();
binaryTree.inOrderII(root);
System.out.println();
System.out.print("后序递归:");
binaryTree.suffixOrder(root);
System.out.println();
binaryTree.suffixOrderII(root);
System.out.println();
binaryTree.level(root);
System.out.println();
}
}