-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBinaryTree.java
252 lines (209 loc) · 5.17 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
package ds_010_trees;
public class BinaryTree<T extends Comparable<T>> {
private BinaryNode<T> root;
private int size;
public BinaryTree() {}
public void add(T[] data) {
for(int i = 0; i < data.length; i++) {
add(data[i]);
}
}
public void add(T data) {
if(isEmpty()) {
root = new BinaryNode<T>(data);
} else {
add(root, data);
}
size++;
}
private void add(BinaryNode<T> node, T data) {
if(data.compareTo(node.data) < 0) {
if(node.left != null) {
add(node.left, data);
} else {
node.left = new BinaryNode<T>(data);
size++;
return;
}
} else if(data.compareTo(node.data) > 0) {
if(node.right != null) {
add(node.right, data);
} else {
node.right = new BinaryNode<T>(data);
size++;
return;
}
} else {
return;
}
}
public boolean find(T data) {
if(isEmpty()) {
return false;
} else {
return find(root, data) == null ? false : true;
}
}
private BinaryNode<T> find(BinaryNode<T> node, T data) {
if(data.compareTo(node.data) < 0) {
if(node.left != null) {
return find(node.left, data);
} else {
return null;
}
} else if(data.compareTo(node.data) > 0) {
if(node.right != null) {
return find(node.right, data);
} else {
return null;
}
} else {
return node;
}
}
public boolean remove(T data) {
if(isEmpty()) {
return false;
}
if(root.data.equals(data) && size == 1) {
root = null;
size--;
return true;
}
/*
* From this point the height of the tree > 0
* and also it is guaranteed that we won' remove
* root so every removed object will have a parent
* The parent is guaranteed not to be null
*
* Cases;
* 1 - If node is a leaf, just remove
* 2 - If node has only a one child, replace it with its child
* 3 - If node has two children;
* a - If left child hasn't right-child, replace the node with its left child
* b - If left child has a right-child, find the right-most child of the
* left sub-tree and replace it with the node
*/
return remove(root, data);
}
private boolean remove(BinaryNode<T> node, T data) {
BinaryNode<T> parent = null;
BinaryNode<T> removed = root;
Boolean isLeftChildOfParent = null;
while(removed != null) {
if(data.compareTo(removed.data) < 0) {
parent = removed;
removed = removed.left;
isLeftChildOfParent = true;
} else if(data.compareTo(removed.data) > 0 ) {
parent = removed;
removed = removed.right;
isLeftChildOfParent = false;
} else {
delete(parent, removed, isLeftChildOfParent);
size--;
return true;
}
}
return false;
}
public void delete(BinaryNode<T> parent, BinaryNode<T> removed, Boolean isLeftChildOfParent) {
BinaryNode<T> replaced = null;
if(removed.left == null && removed.right == null) {
// case 1: removed is a leaf
replaced = null;
} else if(removed.left == null || removed.right == null) {
// case 2: removed has only one child
if(removed.left != null) {
replaced = removed.left;
} else {
replaced = removed.right;
}
} else {
// case 3: removed has two children
if(removed.left.right == null) {
// case 3-a: removed has a left child with no right child
replaced = removed.left;
replaced.right = removed.right;
} else {
// case 3-b: removed has a left child right child
BinaryNode<T> parentOfRightMost = removed.left;
BinaryNode<T> rightMost = removed.left.right;
while(rightMost.right != null) {
parentOfRightMost = rightMost;
rightMost = rightMost.right;
}
parentOfRightMost.right = rightMost.left;
rightMost.left = removed.left;
rightMost.right = removed.right;
replaced = rightMost;
}
}
if(parent == null && isLeftChildOfParent == null) {
root = replaced;
} else {
if(isLeftChildOfParent) {
parent.left = replaced;
} else {
parent.right = replaced;
}
}
}
public int height() {
return height(root);
}
private int height(BinaryNode<T> node) {
if(node == null) {
return -1; // height is number of layers - 1
}
return Integer.max(height(node.left), height(node.right)) + 1;
}
// trivial
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void print() {
System.out.print("Binary Tree: ");
if(isEmpty()) {
System.out.print("Empty");
} else {
printInOrder(root);
}
System.out.print("\n");
}
private void printInOrder(BinaryNode<T> node) {
if(node == null) {
return;
}
printInOrder(node.left);
System.out.printf("%3d ", node.data);
printInOrder(node.right);
}
private void printPreOrder(BinaryNode<T> node) {
if(node == null) {
return;
}
System.out.printf("%3d ", node.data);
printInOrder(node.left);
printInOrder(node.right);
}
private void printPostOrder(BinaryNode<T> node) {
if(node == null) {
return;
}
printInOrder(node.left);
printInOrder(node.right);
System.out.printf("%3d ", node.data);
}
private class BinaryNode<G> {
private G data;
private BinaryNode<G> left;
private BinaryNode<G> right;
public BinaryNode(G data) {
this.data = data;
}
}
}