-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinTree.java
300 lines (211 loc) · 6.3 KB
/
BinTree.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import java.util.ArrayList;
import java.util.List;
/** Implementation of a proper linked Binary Tree with labels of type E */
public class BinTree<E> {
/** reference to the root: */
private BTNode<E> root;
/** number of nodes: */
private int size;
/** default constructor: */
public BinTree() {
root = new BTNode(null,null,null,null);
size = 1;
}
public static String outputBT(BinTree<Integer> t){
String str="";
if(t.root!=null){
//call the method on root
str=outputBT(t.root);
}
return str;
}
/*
*
* construct this art " 8(5(9, 3), 3(6, 2(5, 1)))."
* of notation recursively
*
*/
public static String outputBT(BTNode<Integer> node){
String output="";
//if node is a leaf , simply add and number to the
//string
if (node.isLeaf()){
return node.element().toString();
}
output+= node.element();
//if the node has left child then add "(" to string and go
//down
if(node.leftChild()!=null){
output+="("+outputBT(node.leftChild());
}
//if the node has right child
//add "," to string and go down the free to that right child
//then we return to the current level
//we close our string with ")"
if(node.rightChild()!=null){
output+= ","+outputBT(node.rightChild())+")";
}
return output;
}
public static void depth(BinTree<Integer> t){
depth(t.root, 0);
}
/**
* change the entries values of nodes
* to their depth
* calculates depth recursively
*
* @param node
* @param depth
*/
public static void depth(BTNode<Integer> node, int depth){
node.setElement(depth);
depth = depth+1;
if(node.leftChild()!=null){
depth(node.leftChild(), depth);
}
if(node.rightChild()!=null){
depth(node.rightChild(), depth);
}
}
public static void height(BinTree<Integer> t ){
height(t.root);
}
/**
* change the entries values of nodes
* to their height
* calculates height recursively
*
* @param node
* @return height of current node
*
*/
public static Integer height(BTNode<Integer> node){
if(node==null){
return 0;
}
//if node is leaf return 0 for height. the height of
//leaf is always 0
if(node.isLeaf()){
node.setElement(0);
return 0;
}
//go to left child node
int leftt = height(node.leftChild());
//go to right child node
int rigtht =height(node.rightChild());
//choose the biggest height of two trees
int height = 1+ Math.max(leftt, rigtht) ;
node.setElement(height);
return height;
}
/**
* returns nodes in pre order
* first display root, then left subtree then right subtree
* @param - binary tree
* @return list of nodes in pre order
*
*/
public List<E> preOrder(BinTree<E> t){
List<E> list = new ArrayList<>();
if(t.root!=null){
preOrder(list,root);
}
return list;
}
private void preOrder(List<E> list,BTNode<E> node) {
list.add(node.element());
if(node.leftChild()!=null){
preOrder(list,node.leftChild());
}
if(node.rightChild()!=null){
preOrder(list,node.rightChild());
}
}
/**
*
* returns list of nodes in post order
* first display left subtree, then right subtree
* and then root
* @param t binary tree
* @return
*/
public List<E> postOrder(BinTree<E> t){
List<E> list = new ArrayList<>();
if(t.root!=null){
postOrder(list,root);
}
return list;
}
private void postOrder(List<E> list,BTNode<E> node) {
if(node.leftChild()!=null){
postOrder(list,node.leftChild());
}
if(node.rightChild()!=null){
postOrder(list,node.rightChild());
}
list.add(node.element());
}
/** accessor methods: */
public int size() { return size; }
public boolean isTrivial() { return (size==1); }
/** a trivial Tree consists of the root node only
* empty trees are excluded */
public boolean isInternal(BTNode<E> v) {return v.isInternal(); }
public boolean isLeaf(BTNode<E> v) {return v.isLeaf(); }
public boolean isRoot(BTNode<E> v) { return (v==root()); }
public BTNode<E> root() { return root; }
public BTNode<E> leftChild(BTNode<E> v) { return v.leftChild(); }
public BTNode<E> rightChild(BTNode<E> v) { return v.rightChild(); }
public BTNode<E> sibling(BTNode<E> v) {
BTNode<E> p = parent(v);
BTNode<E> lc = leftChild(p);
if (v == lc)
return rightChild(p);
else
return lc;
}
public BTNode<E> parent(BTNode<E> v) { return v.parent(); }
/** update methods: */
/** turn a leaf v to an inner node by appending two new leaves
* if v is not a leaf, the method won't do anything
* alternatively one could throw an exception */
public void expandExternalNode(BTNode<E> v){
if( isLeaf(v)){
v.setLeft(new BTNode<E>(null,v,null,null));
v.setRight(new BTNode<E>(null,v,null,null));
size += 2;
}
}
/** delete a leaf v and replace v's parent node by v's sibling
* if v is the root or if v is not a leaf, the method won't do anything
* alternatively one could throw an exception */
public void removeAboveExternalNode(BTNode<E> v){
if( isLeaf(v) && !isRoot(v)){
BTNode<E> p = parent(v);
BTNode<E> s = sibling(v);
if( isRoot(p)){
s.setParent(null);
root = s;
}
else{
BTNode<E> g = parent(p);
if( p == leftChild(g))
g.setLeft(s);
else
g.setRight(s);
s.setParent(g);
}
size -= 2;
}
}
/** Just a short test: */
public static void main(String[] args){
BinTree<Integer> bt = new BinTree();
BTNode<Integer> v = new BTNode();
(bt.root()).setLeft(v);
bt.expandExternalNode(v);
bt.expandExternalNode(v.leftChild());
System.out.println(" Size = " + bt.size());
}
}