-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.java
More file actions
407 lines (330 loc) · 11.1 KB
/
Copy pathbinarySearchTree.java
File metadata and controls
407 lines (330 loc) · 11.1 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import java.util.Iterator;
import java.util.*;
public class binarySearchTree{
binarySearchTreeNode root;
int size;
public Object find(Object k){
binarySearchTreeNode node = getRoot();
while(node!=null ){
int temp= ((Comparable) k).compareTo(node.getElement());
if(temp<0){
node=node.getLeft();
}else if(temp>0){
node=node.getRight();
}else{
return node.getElement();
}
}
return null;
}
public binarySearchTree(){
root=null;
size=0;
}
public binarySearchTreeNode getRoot(){
return root;
}
public void addRoot(Object element){
if(isEmpty())
root=createNode(element,null,null,null);
size++;
}
public binarySearchTreeNode createNode(Object element,binarySearchTreeNode parent,binarySearchTreeNode left,binarySearchTreeNode right){
size++;
return new binarySearchTreeNode(element,parent,left,right);
}
public boolean isEmpty(){
if (size==0) return true;
else return false;
}
public Iterator allNodes() throws NoSuchElementException{
BTDList allNodes = new BTDList();
if(size!=0){
inOrderNodeElements(getRoot(),allNodes);
}
return allNodes.getAllElementsIterator();
}
/* gets first element greater than object */
public Object elementGreaterThanObject(Object o) throws NoRootException{
/* find the element position
* if right element is present return right element
* else return null
*/
/* check if root is null if null throw no root exception */
if(getRoot()==null) throw new NoRootException("no root present. Please insert root elemnt");
return traversalToFindElementGreaterThanObject(getRoot(),o);
}
/* CODE::
* get element less than object
* if root is null throw no root element exception
* else traverse to find element less than object
*/
public Object elementLessThanObject(Object o) throws NoRootException {
if(getRoot()==null) throw new NoRootException("no root present. Please insert root element first");
return(traversalToFindElementLessThanObject(getRoot(),o).getElement());
}
/* CODE::
* find element position
* recursively search if left element is less than object
* if left element is less than object return left elemnt
* if left element is equal to object check if node has left child
* if no left child check if parent element is less than object if yes then return parent element
* else return null
*/
public binarySearchTreeNode traversalToFindElementLessThanObject(binarySearchTreeNode node,Object o){
if((Integer) o<(Integer)node.getElement()){
if(node.getParent()!=null){
return node.getParent();
}else{
return null;
}
}else if((Integer) o>(Integer)node.getElement()){
if(hasRight(node)){
return traversalToFindElementLessThanObject(node.getRight(),o);
}else{
return node;
}
}else{
/*control comes here when node element is = to object o */
if(node.getParent()!=null){
if((Integer)node.getParent().getElement()<(Integer)o){
return node.getParent();
}else{
return null;
}
}else{
if(hasLeft(node)){
return traversalToFindElementLessThanObject(node.getLeft(),o);
}else{
return null;
}
}
}
}
/* CODE:
*if root is null throw no root excepition
* else call findandRemove()
*/
public Object removeObject(Object o) throws NoRootException{
if( getRoot()==null) throw new NoRootException("please add root node first.");
return findAndRemoveObject(getRoot(),o);
}
/* CODE::
* find position of node
* if node has no child
* then remove node
* elseif node has only 1 child
* replace node with child node
* else
* move to rightchild and traverse through right child's left's subtree recursively untill you find last node
* replace node to be removed with last node value
* if last node has single right child replace last node value with single right child value
*/
public Object findAndRemoveObject(binarySearchTreeNode node, Object o){
binarySearchTreeNode correctPositionNode = inOrderTraversalToFindPostion(node,o);
System.out.println("correct position node is "+ correctPositionNode.getElement().toString());
if(correctPositionNode==null){
return null;
}else{
//System.out.println("node elment is "+ (Integer)correctPositionNode.getElement()+" and object is "+(Integer)o + "and both are equal is "+((Integer)o==(Integer)correctPositionNode.getElement()));
int temp= ((Comparable) o).compareTo(correctPositionNode.getElement());
if(temp==0){
if((!hasLeft(correctPositionNode)) && (!hasRight(correctPositionNode))){
System.out.println("searching for parent and removing child");
binarySearchTreeNode parentNode = correctPositionNode.getParent();
System.out.println("parent element is "+ (Integer)parentNode.getElement());
if(hasLeft(parentNode)){
if(((Comparable) o).compareTo(parentNode.getLeft().getElement())==0){
System.out.println("removing left node");
parentNode.setLeft(null);
}else{
System.out.println("removing right node");
parentNode.setRight(null);
}
}else{
System.out.println("removing right node");
parentNode.setRight(null);
}
}else{
System.out.println("searching for node less than "+ correctPositionNode.getElement().toString());
binarySearchTreeNode nodeWithValueLessThanCurrentNode = traversalToFindNodeLessThanObjectForRemoving(correctPositionNode,correctPositionNode.getElement());
System.out.println("node with value less than "+ correctPositionNode.getElement().toString()+"is "+nodeWithValueLessThanCurrentNode.getElement().toString());
correctPositionNode.setElement(nodeWithValueLessThanCurrentNode.getElement());
nodeWithValueLessThanCurrentNode.getParent().setLeft(null);
}
}
return o;
}
}
/*code::
* if node has only left subtree return left child node
* else if node has right subtree only return right child node
* else go to right child and traverse along left subtree of this child recursively untill you reach null value
* send node just above null value
*/
public binarySearchTreeNode traversalToFindNodeLessThanObjectForRemoving(binarySearchTreeNode node,Object o){
if((hasRight(node))&&(!hasLeft(node))){
System.out.println("only right child and no left child");
return node.getRight();
}else if((hasLeft(node)) && (!hasRight(node))){
System.out.println("only left child and no right child");
return node.getLeft();
}else{
/* if left child has no children return left child */
if((!hasRight(node.getLeft())) && (!hasLeft(node.getLeft()))){
return node.getLeft();
}else{
if((!hasRight(node.getRight())) && (!hasLeft(node.getRight()))){
System.out.println(" right child has no children");
return node.getRight();
}else{
System.out.println(" right child has children and finding smallest child in left subtree");
binarySearchTreeNode rightChild = node.getRight();
while(rightChild.getLeft()!=null){
rightChild=rightChild.getLeft();
}
System.out.println(" child with smallest value in left subtree is "+ rightChild.getElement().toString());
return rightChild;
}
}
}
}
public Object checkIfObjectIsPresent(Object o){
binarySearchTreeNode node = inOrderTraversalToFindPostion(getRoot(),o);
if(node!=null){
return o;
}else{
return null;
}
}
public binarySearchTreeNode inOrderTraversalToFindPostion(binarySearchTreeNode node,Object o){
if((Integer)o>(Integer)node.getElement()){
if(hasRight(node)){
return inOrderTraversalToFindPostion(node.getRight(),o);
}else{
return null;
}
}else if((Integer)o<(Integer)node.getElement()){
if(hasLeft(node)){
return inOrderTraversalToFindPostion(node.getLeft(),o);
}else{
return null;
}
}else{
return node;
}
}
public boolean hasSingleChild(binarySearchTreeNode node){
//if( (hasLeft(node))|| (hasRight(node)) ){
if (((hasLeft(node)) && (!hasRight(node))) || ((!hasLeft(node)) && (hasRight(node)))){
return true;
}else{
return false;
}
}
public Object traversalToFindElementGreaterThanObject(binarySearchTreeNode node, Object o) {
/* if current element is greater than node element move right */
if((Integer) o >(Integer)node.getElement()){
if(hasRight(node)){
return traversalToFindElementGreaterThanObject(node.getRight(),o);
}else{
/* check if parent element is greater than object else return null */
if((Integer)node.getParent().getElement()>(Integer)o){
return node.getParent().getElement();
}else{
return null;
}
}
}else if ((Integer) o<(Integer)node.getElement()){
if(hasLeft(node)){
return traversalToFindElementGreaterThanObject(node.getLeft(),o);
}else{
return node.getElement();
}
}else{
/* control comes here only when node element == object */
if(hasRight(node))
return node.getRight().getElement();
else{
/* check if parent is greater than object else return null */
if((Integer)node.getParent().getElement()>(Integer)o){
return node.getParent().getElement();
}else{
return null;
}
}
}
}
public BTDList inOrderNodeElements ( binarySearchTreeNode node,BTDList allNodes){
if(hasLeft(node)){
binarySearchTreeNode leftNode = node.getLeft();
allNodes = inOrderNodeElements(leftNode,allNodes);
}
this.visit(node,allNodes);
if(hasRight(node)){
binarySearchTreeNode rightNode = node.getRight();
allNodes = inOrderNodeElements(rightNode,allNodes);
}
return allNodes;
}
public void visit(binarySearchTreeNode node,BTDList allNodes){
allNodes.insertBack(node.getElement());
}
public boolean hasLeft(binarySearchTreeNode node){
return (node.getLeft()!=null);
}
public boolean hasRight(binarySearchTreeNode node){
return (node.getRight()!=null);
}
public Object getFirst(){
if(root==null)return null;
else{
binarySearchTreeNode current=root;
while(hasLeft(current)){
current=current.getLeft();
}
return current.getElement();
}
}
public Object getLast(){
if(root==null)return null;
else{
binarySearchTreeNode current=root;
while(hasRight(current)){
current=current.getRight();
}
return current.getElement();
}
}
public Object insert ( Object o) throws NoRootException{
binarySearchTreeNode startingNode = getRoot();
/* check if root is empty
* if yes throw nullElementException
* else continue
*/
if(startingNode==null){
throw new NoRootException("no root element. please add new root element first");
}else{
/* find where to insert */
while(startingNode !=null){
if((Integer)o>(Integer)startingNode.getElement()){
if(startingNode.getRight()==null){
startingNode.setRight(new binarySearchTreeNode(o,startingNode,null,null));
break;
}else{
startingNode=startingNode.getRight();
}
}else{
if(startingNode.getLeft()==null){
startingNode.setLeft(new binarySearchTreeNode(o,startingNode,null,null));
break;
}else{
startingNode= startingNode.getLeft();
}
}
}
System.out.println("adding element now");
}
return o;
}
}