-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
286 lines (259 loc) · 8.91 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
279
280
281
282
283
284
285
286
public class BinaryTree {
// Members
int value;
int size;
BinaryTree root;
BinaryTree parent;
BinaryTree left;
BinaryTree right;
// Constructors
BinaryTree(int val) {
this.value = val;
this.left = null;
this.right = null;
this.parent = null;
size++;
this.root = this;
}
BinaryTree() {
this.right = null;
this.left = null;
this.parent = null;
this.root = null;
size++;
}
// Returns false if val is found
// Otherwise add val (as a node) to the tree and returns true
boolean add(int val) {
boolean member = search(val);
if (member) {
System.out.println("Value already in tree");
return false;
} else {
BinaryTree node = root;
addNode(node, val);
return true;
}
}
private void addNode(BinaryTree node, int val) {
if (val < node.value) {
if (node.left == null) {
BinaryTree child = new BinaryTree();
child.value = val;
child.parent = node;
child.root = node.root;
node.left = child;
size++;
} else {
addNode(node.left, val);
}
} else {
if (node.right == null) {
BinaryTree child = new BinaryTree();
child.value = val;
child.parent = node;
child.root = node.root;
node.right = child;
size++;
} else {
addNode(node.right, val);
}
}
}
// Returns true if val is found, otherwise returns false
boolean search(int val) {
// start from root and search every children
boolean result = searchChildren(root, val);
return result;
}
// search val in children of a specific node
boolean searchChildren(BinaryTree node, int val) {
if (node == null)
return false;
if (node.value == val)
return true;
boolean result;
if (val < node.value) {
result = searchChildren(node.left, val);
} else {
result = searchChildren(node.right, val);
}
return result;
}
// Returns the # of branches needed (from root) to reach lowest leaf
int height(BinaryTree node) {
int ht = 0;
if (node.left != null) {
int leftHt = height(node.left);
ht = Math.max(ht, leftHt);
}
if (node.right != null) {
int rightHt = height(node.right);
ht = Math.max(ht, rightHt);
}
ht++;
return ht;
}
int height() {
int ht = height(root);
return ht;
}
// Returns number of children the given node has
private int degree(BinaryTree node) {
int deg = 0;
if (node.left != null)
deg++;
if (node.right != null)
deg++;
return deg;
}
// If node has two children return false
// If node has no children, return false
// If node has only one child, returns the child node
private BinaryTree getOnlyChild(BinaryTree node) {
int deg = degree(node);
if (deg == 1) {
if (node.left != null)
return node.left;
else {
return node.right;
}
}
return null;
}
// If val is found, remove the node and return true
// Otherwise return false
// remove uses helper method removeHelper for the recursion
boolean remove(int val) {
boolean bool = search(val);
if (bool){
root = removeHelper(root, val);
System.out.println("Removed " + val + " from tree");
}
return bool;
}
// Uses recursion to find and remove the node that contains val
// If val is not found, returns false
private BinaryTree removeHelper(BinaryTree node, int val) {
// dead node = node to remove
if (node == null)
return null;
if (val > node.value) {
node.right = removeHelper(node.right, val); // attach returned node to right
} else if (val < node.value) {
node.left = removeHelper(node.left, val); // attach returned node to left
} else {
if (node.left != null && node.right != null) {
BinaryTree leftMaxNode = getInorderPredecessor(node);
node.value = leftMaxNode.value;
node.left = removeHelper(node.left, leftMaxNode.value); // remove the leftMaxNode from left-subtree
} else {
return getOnlyChild(node); //for no child it will return null & for only 1 child it will return that child
}
}
return node; // if the node is not dead node and not in left or right of dead node return itself
}
// Returns node with the largest value that is also
// smaller than or equal to the value of the calling node
// This method returns null if left node is null
// Otherwise it returns left.getInOrderPredecessorHelper()
BinaryTree getInorderPredecessor(BinaryTree node) {
if (node.left == null)
return null;
return getInorderPredecessorHelper(node.left);
}
// Returns the largest node it can find by recursively calling
// itself using the right node
BinaryTree getInorderPredecessorHelper(BinaryTree node) {
if (node.right == null)
return node;
else
return getInorderPredecessorHelper(node.right);
}
// Traverses and prints the binary tree in Preorder by calling the
// recursive method printPreoderHelper and then prints a newline
void printPreorder() {
printPreorderHelper(root);
System.out.println();
}
// Prints the value of the node and then calls itself with the
// left node (if it’s not null) and then calls itself with the
// right node (if it’s not null)
private void printPreorderHelper(BinaryTree node) {
if (node == null)
return;
System.out.print(node.value + " ");
printPreorderHelper(node.left);
printPreorderHelper(node.right);
}
// Traverses and prints the binary tree in Inorder by calling the
// recursive method printInoderHelper and then prints a newline
void printInorder() {
printInorderHelper(root);
System.out.println();
}
// Calls itself with the left node (if it’s not null) and then
// prints the value of the node and then finally calls itself with
// the right node (if it’s not null)
private void printInorderHelper(BinaryTree node) {
if (node == null)
return;
printInorderHelper(node.left);
System.out.print(node.value + " ");
printInorderHelper(node.right);
}
// Traverses and prints the binary tree in Postorder by calling the
// recursive method printPostoderHelper and then prints a newline
void printPostorder() {
printPostorderHelper(root);
System.out.println();
}
// Calls itself with the left node (if it’s not null) and then
// calls itself with the right node (if it’s not null) and finally
// prints the value of the node
private void printPostorderHelper(BinaryTree node) {
if (node == null)
return;
printPostorderHelper(node.left);
printPostorderHelper(node.right);
System.out.print(node.value + " ");
}
public static void main(String[] args) {
BinaryTree a = new BinaryTree(30);
// boolean bool = a.search(1);
// System.out.println(bool);
a.add(20);
a.add(40);
a.add(25);
a.add(10);
a.add(50);
a.add(35);
a.add(15);
a.add(16);
a.add(19);
a.add(55);
a.add(45);
a.add(36);
a.add(32);
// System.out.println(added);
// BinaryTree c = a.right;
// System.out.println(c.root.value);
System.out.println("Preorder : -");
a.printPreorder();
System.out.println("Inorder : -");
a.printInorder();
System.out.println("Postorder : -");
a.printPostorder();
a.remove(19);
a.remove(35);
a.remove(50);
a.remove(10);
System.out.println("Inorder after removal: -");
a.printInorder();
// BinaryTree n = a.getInorderPredecessor(a.right);
// System.out.println(n.value);
// int ht = a.height();
// System.out.println(ht);
}
}
// The quick brown fox jumps over the lazy dog. 1234567890