平衡二叉树的实现 操作 遍历
BST的实现
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
248
249
250
251
252
253
254
255
256
257
258
* 文件名:BinaryTree.java
* 时间:2014年10月23日下午8:27:34
* 作者:修维康
*/
package chapter4;
import java.util.*;
class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
private static class Node<AnyType> {
Node(AnyType data, Node<AnyType> leftChild, Node<AnyType> rightChild) {
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
private AnyType data;
private Node<AnyType> leftChild;
private Node<AnyType> rightChild;
}
private Node<AnyType> root;
BinarySearchTree(AnyType x) {
root = new Node<AnyType>(x, null, null);
}
BinarySearchTree() {
root = null;
}
public void makeEmpty() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
public boolean contains(AnyType x) {
return contains(x, root);
}
private boolean contains(AnyType x, Node<AnyType> t) {
if (t == null)
return false;
int compareResult = x.compareTo(t.data);
if (compareResult < 0)
return contains(x, t.leftChild);
else if (compareResult > 0)
return contains(x, t.rightChild);
else
return true;
}
public AnyType findMin() {
if (isEmpty())
return null;
return findMin(root).data;
}
// 递归是实现查找最大最小值
/*
* public Node<AnyType> findMin(Node<AnyType> t){ if(t == null) return null;
* else if(t.leftChild == null) return t; return findMin(t.leftChild); }
* public Node<AnyType> findMax(Node<AnyType> t){ if(t == null) return null;
* else if(t.rightChild == null) return t; else findMax(t.rightChild); }
*/
// 非递归实现查找最大最小值
private Node<AnyType> findMin(Node<AnyType> t) {
if (t != null)
while (t.leftChild != null) {
t = t.leftChild;
}
return t;
}
private Node<AnyType> findMax(Node<AnyType> t) {
if (t != null)
while (t.rightChild != null) {
t = t.rightChild;
}
return t;
}
public void insert(AnyType x) {
root = insert(x, root);
}
private Node<AnyType> insert(AnyType x, Node<AnyType> t) {
if (t == null)
t = new Node<AnyType>(x, null, null);
int compareResult = x.compareTo(t.data);
if (compareResult < 0)
t.leftChild = insert(x, t.leftChild);
if (compareResult > 0)
t.rightChild = insert(x, t.rightChild);
return t;
}
public void remove(AnyType x) {
root = remove(x, root);
}
private Node<AnyType> remove(AnyType x, Node<AnyType> t) {
if (t == null)
return t;// 没找到
int compareResult = x.compareTo(t.data);
if (compareResult < 0)
t.leftChild = remove(x, t.leftChild);
else if (compareResult > 0)
t.rightChild = remove(x, t.rightChild);
else if (t.leftChild != null && t.rightChild != null) {
t.data = findMin(t.rightChild).data;// 如果这个节点有2个子节点则选右子树中元素最小的节点,赋值给它
t.rightChild = remove(t.data, t.rightChild);// 同时递归右子树那个最小的节点
} else
t = (t.leftChild != null) ? t.leftChild : t.rightChild;
return t;
}
public void printTree() {
printTree5(root);
}
// 递归先序遍历
private void printTree(Node<AnyType> t) {
if (t != null) {
System.out.println(t.data + " ");
printTree(t.leftChild);
printTree(t.rightChild);
}
}
// 递归中序遍历 private void
private void printTree1(Node<AnyType> t) {
if (t != null) {
printTree(t.leftChild);
System.out.println(t.data + " ");
printTree(t.rightChild);
}
}
// 递归后序遍历
private void printTree3(Node<AnyType> t) {
if (t != null) {
printTree(t.leftChild);
printTree(t.rightChild);
System.out.println(t.data + " ");
}
}
// 非递归先序遍历
private void printTree4(Node<AnyType> p) {
Stack<Node> stack = new Stack<Node>();
stack.push(p);
while (!stack.empty()) {
p = stack.pop();
System.out.println(p.data); // 先右节点进栈,在左节点进栈,出来的时候顺序相反。
if (p.rightChild != null)
stack.push(p.rightChild);
if (p.leftChild != null)
stack.push(p.leftChild);
}
}
// 非递归中序遍历
private void printTree5(Node<AnyType> p) {
Stack<Node<AnyType>> stack = new Stack<Node<AnyType>>();
while (p != null || !stack.empty()) {
// 和递归一样的思路,很好想
if (p != null) {
stack.push(p);
p = p.leftChild;
} else {
p = stack.pop();
System.out.println(p.data);
p = p.rightChild;
}
}
}
// 非递归后序遍历为3中遍历中最复杂的一种,也是面试里经常问到的
// 后序里面每个节点都要进两次栈
private void printTree6(Node<AnyType> p) {
Stack<Node<AnyType>> stack = new Stack<Node<AnyType>>();
HashSet<Node<AnyType>> visited = new HashSet<Node<AnyType>>();// 通过一个容器来标记
while (p != null || !stack.empty()) {
if (p != null) {
stack.push(p);
visited.add(p);
p = p.leftChild;
} else if (!stack.empty()) {
p = stack.pop();
if (p != null && !visited.contains(p)) {
System.out.println(p.data);
p = null;
} else {
visited.remove(p);
stack.push(p);
p = p.rightChild;
}
}
}
}
// 层序遍历
private void printTree7(Node<AnyType> p) {
LinkedList<Node<AnyType>> queue = new LinkedList<Node<AnyType>>();
queue.push(p);
while (!queue.isEmpty()) {
p = queue.pop();
System.out.println(p.data);
if (p.leftChild != null)
queue.add(p.leftChild);
if (p.rightChild != null)
queue.add(p.rightChild);
}
}
}
/**
* 类名:BinarySearchTreeTest 说明:查找二叉树
*/
public class BinarySearchTreeTest {
/**
* 方法名:main 说明:二叉查找树数据结构的测试
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BinarySearchTree<Integer> st = new BinarySearchTree<Integer>(6);
st.insert(15);
st.insert(6);
st.insert(3);
st.insert(7);
st.insert(2);
st.insert(4);
st.insert(13);
st.insert(9);
st.insert(18);
st.insert(17);
st.insert(20);
// System.out.println(tree.contains(1));
// System.out.println(tree.contains(5));
// tree.remove(4);
st.printTree();
}
}