This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTrees.java
More file actions
351 lines (178 loc) · 5.77 KB
/
BinaryTrees.java
File metadata and controls
351 lines (178 loc) · 5.77 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
public class BinaryTrees {
// Trees provide quick insertion, searching and deletion
// Time to do any operation on a Tree is O(log N), so we can see why it is such a great data structure
// Top node of the Tree is called it's root, Leaf is a node that does not have any children nodes to it
// Binary trees are special trees which only have two or less children each node and always the parent has mid value, left child has
// smaller value and right child has larger value, NOTE that right child MUST be greater than both parent and left child and left child
// needs to be smaller than both parent and right child; for the tree to be called Binary tree.
// Unbalanced trees are those trees in which most of the data is found on one particular side of the whole tree, either left or right
Node root;
public void addNode(int key, String name) {
Node newNode = new Node(key, name);
if(root == null)
root = newNode;
else {
Node focusNode = root;
Node parent;
while(true) {
parent = focusNode;
if(key < focusNode.key) {
focusNode = focusNode.leftChild;
if(focusNode == null) {
parent.leftChild = newNode;
return;
}
}
else {
focusNode = focusNode.rightChild;
if(focusNode == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
// Deleting a node is the only thing that is a bit longer and complex relatively and so is important too!!!
public boolean remove(int key) {
Node focusNode = root;
Node parent = root;
boolean isItLeftChild = true;
while(focusNode.key!=key) {
parent = focusNode;
if(key < focusNode.key) {
isItLeftChild = true;
focusNode = focusNode.leftChild;
}
else {
isItLeftChild = false;
focusNode = focusNode.rightChild;
}
if(focusNode == null)
return false;
}
if(focusNode.leftChild == null && focusNode.rightChild == null) {
if(focusNode == root)
root = null;
else if(isItLeftChild) {
parent.leftChild = null;
}
else
parent.rightChild = null;
}
else if(focusNode.rightChild == null) {
if(focusNode == root)
root = focusNode.leftChild;
else if(isItLeftChild) {
parent.leftChild = focusNode.leftChild;
}
else
parent.rightChild = focusNode.leftChild;
}
else if(focusNode.leftChild == null) {
if(focusNode == root)
root = focusNode.rightChild;
else if(isItLeftChild)
parent.leftChild = focusNode.rightChild;
else
parent.rightChild = focusNode.leftChild;
}
else {
Node replacement = getReplacementNode(focusNode);
if(focusNode == root)
root = replacement;
else if(isItLeftChild)
parent.leftChild = replacement;
else
parent.rightChild = replacement;
replacement.leftChild = focusNode.leftChild;
}
return true;
}
public Node getReplacementNode(Node toReplaceNode) {
Node replacementParent = toReplaceNode;
Node replacement = toReplaceNode;
Node focusNode = toReplaceNode.rightChild;
while(focusNode != null) {
replacementParent = replacement;
replacement = focusNode;
focusNode = focusNode.leftChild;
}
if(replacement!=toReplaceNode.leftChild) {
replacementParent.leftChild = replacement.rightChild;
replacement.rightChild = toReplaceNode.rightChild;
}
return replacement;
}
// There are three types of traversal in Trees, In-Order traversal aims for the smallest value first (start from left child from root)
public void inOrderTraverseTree(Node focusNode) {
if(focusNode!=null) {
inOrderTraverseTree(focusNode.leftChild);
System.out.println(focusNode);
inOrderTraverseTree(focusNode.rightChild);
}
}
// Second traversing way of Trees is Pre-order Traversal
public void preOrderTraverseTree(Node focusNode) {
if(focusNode!=null) {
System.out.println(focusNode);
preOrderTraverseTree(focusNode.leftChild);
preOrderTraverseTree(focusNode.rightChild);
}
}
// Third traversing way of Trees
public void postOrderTraverseTree(Node focusNode) {
if(focusNode!=null) {
postOrderTraverseTree(focusNode.leftChild);
postOrderTraverseTree(focusNode.rightChild);
System.out.println(focusNode);
}
}
public static void main(String[] args) {
BinaryTrees tree = new BinaryTrees();
tree.addNode(50, "Boss");
tree.addNode(25, "Vice presidnet");
tree.addNode(15, "Manager");
tree.addNode(30, "Secretary");
tree.addNode(75, "Sales Manager");
tree.addNode(80, "SalesMan");
tree.inOrderTraverseTree(tree.root);
System.out.println();
tree.preOrderTraverseTree(tree.root);
System.out.println();
tree.postOrderTraverseTree(tree.root);
System.out.println();
System.out.println(tree.findNode(30));
System.out.println();
tree.remove(15);
System.out.println("Removed 15");
System.out.println();
tree.inOrderTraverseTree(tree.root);
}
public Node findNode(int key) {
Node focusNode = root;
while(focusNode.key!=key) {
if(key < focusNode.key)
focusNode = focusNode.leftChild;
else
focusNode = focusNode.rightChild;
if(focusNode == null)
return null;
}
return focusNode;
}
}
class Node {
int key;
String name;
Node leftChild;
Node rightChild;
public Node(int key, String name) {
this.key = key;
this.name = name;
}
@Override
public String toString() {
return name+" with key "+key;
}
}