-
Notifications
You must be signed in to change notification settings - Fork 2
/
BinaryTree.java
278 lines (251 loc) · 5.36 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.util.*;
public class BinaryTree<T> implements BinaryTTree< T >
{
BinaryNode< T > root;
public BinaryTree()
{
this.root=null;
}
//树是否为空
public boolean isEmpty()
{
return this.root==null;
}
//通过前序,中序序列构建二叉树
public BinaryTree(T[] prelist, T[] inlist )
{
this.root=create(prelist,inlist,0,0,prelist.length);
}
public BinaryNode<T> create(T[] prelist,T[] inlist,int preStart,int inStart, int n)
{
if(n<=0) return null;
T elem=prelist[preStart];
BinaryNode<T> p=new BinaryNode<T>(elem);
int i=0;
while(i<n&&!elem.equals(inlist[inStart+i]))
i++;
p.leftChild=create(prelist,inlist,preStart+1,inStart,i);
p.rightChild=create(prelist,inlist,preStart+1+i,inStart+i+1,n-i-1);
return p;
}
//前序遍历
public void preOrder()
{
System.out.println("前序遍历");
preOrder(root);
System.out.println();
}
//前序遍历具体实现方法
public void preOrder(BinaryNode<T> curr)
{
if(curr==null) return ;
System.out.println(curr.data.toString());
preOrder(curr.leftChild);
preOrder(curr.rightChild);
}
//中序遍历
public void inOrder()
{
System.out.println("中序遍历");
inOrder(root);
System.out.println();
}
//中序遍历具体实现方法
public void inOrder(BinaryNode<T> curr )
{
if(curr==null)return;
inOrder(curr.leftChild);
System.out.println(curr.data.toString());
inOrder(curr.rightChild);
}
//递归后续遍历
public void postOrder()
{
System.out.println("后续遍历");
postOrder(root);
System.out.println();
}
//递归后续遍历具体实现方法
public void postOrder(BinaryNode<T> curr)
{
if(curr==null) return;
postOrder(curr.leftChild);
postOrder(curr.rightChild);
System.out.println(curr.data.toString());
}
//层次遍历
public void levelOrder()
{
System.out.println("层次遍历");
levelOrder(root);
System.out.println();
}
//层次遍历具体实现方法
public void levelOrder(BinaryNode<T> curr)
{
//未实现
}
//统计树中节点数目
public void count()
{
System.out.println (count(this.root));
}
//统计树中节点数目具体方法
public int count(BinaryNode<T> node)
{
if(node==null) return 0 ;
return 1+count(node.leftChild)+count(node.rightChild);
}
//树的最大深度
public void hight()
{
System.out.println("the hight of the Binarytree");
System.out.println(hight(this.root));
}
//树的最大深度具体实现方法
public int hight(BinaryNode<T> curr)
{
if(curr==null) return 0;
int lh=hight(curr.leftChild);
int rh=hight(curr.rightChild);
return lh>=rh?(1+lh):(1+rh);
}
//寻找树中是否含有关键字为key的节点
public T search(T key)
{
return searchNode(this.root,key).data;
}
public BinaryNode< T > searchNode(T key)
{
BinaryNode<T> p=searchNode(this.root,key);
return p;
}
//寻找树中是否含有关键字为key的节点node
public BinaryNode<T> searchNode(BinaryNode<T> node,T key) //未能测试
{
if(node==null||key==null)
{
return null;
}
if(node.data==key){
return node;
}
BinaryNode<T> find=searchNode(node.leftChild,key);
if(find==null)
{
find=searchNode(node.rightChild,key);
}
return find;
}
//插入关键字key
public void insertRoot(T x) //只是一个root节点,此时树中没有任何其他节点
{
/*
*
* 原理 就是将原来的根节点作为已x为值得节点的左孩子并将新节点作为根节点
*
*/
this.root=new BinaryNode<T>(x, this.root , null);
}
//插入孩子节点
public BinaryNode<T> insertChild(BinaryNode<T> node ,T x,boolean left)
{
/*
*
*question why is x but nor the node
*
*
*/
if(x==null) return null;
BinaryNode<T> temp=new BinaryNode<T>(x);
if(left)
{
node.leftChild=temp;
}
else
{
node.rightChild=temp;
}
return node;
}
//移除孩子节点
public void removeChild(BinaryNode<T> curr, boolean left)
{
if(curr==null) return ;
if(left)
{
curr.leftChild=null;
}else
{
curr.rightChild=null;
}
}
//移除所有孩子节点
public void removeAllChild()
{
this.root=null;
}
//获得节点父节点
public BinaryNode<T> getParent(BinaryNode<T> curr)
{
if(root==null|| curr==null||curr==root ) //还要考虑curr为root的情况 ,思维不是很严谨
{
return null;
}
return getParent(root,curr);
}
//获得父节点具体实现
public BinaryNode<T> getParent(BinaryNode<T> p ,BinaryNode<T> x )
{
if(p==null||x==null) {
return null;
}
if(p.leftChild==x||p.rightChild==x)
{
return p;
}
BinaryNode<T> node=getParent(p.leftChild, x);
if(node==null) {
node=getParent(p.rightChild, x);
}
return node;
}
//判断节点是树是否为叶子节点
public void leaf()
{
leaf(root);
}
//判断节点是否是叶子节点
public void leaf(BinaryNode<T> curr )
{
if(curr!=null)
{
if( curr.leftChild==null&&curr.leftChild==null)
{
System.out.println(curr.data.toString());
return ;
}
/*
*
*this case mean p isn't a leaf BinaryNode<T>
//这里可能有点思路问题
else {
leaf(curr.leftChild);
leaf(curr.rightChild);
}
}
}
//统计叶子节点数目
public int countLeaf()
{
return countLeaf(root);
}
//统计叶子节点具体实现方法
public int countLeaf(BinaryNode<T> curr )
{
if(curr==null) return 0;
if(curr.leftChild==null&&curr.rightChild==null) {
return 1;
}
return count(curr.leftChild)+count(curr.rightChild);
}