-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.java
213 lines (200 loc) · 6.06 KB
/
BinarySearchTree.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
public class BinarySearchTree<K extends Comparable,V> {
private Node<K,V> root;
public BinarySearchTree() {
root = null;
}
public BinarySearchTree(V val, K kluch) {
root = new Node(val, kluch);
}
public V remove(K kluch){
Node<K,V> toRemove = nodeLookup(kluch);
if(toRemove == null) {
System.out.println("this node does not exist");
return null;
}
else if(toRemove == root) {
Node<K,V> toReplace;
if(toRemove.getLeft() != null) {
toReplace = toRemove.getLeft();
while(toReplace.getRight()!=null) {
toReplace = toReplace.getRight();
}
if(toReplace.getLeft()!=null) {
toReplace.getLeft().setParent(toReplace.getParent());
toReplace.getParent().setRight(toReplace.getLeft());
}
}
}
else {
Node<K,V> parent = toRemove.getParent();
char lr;
// finds if toRemove is right or left child node of parent
if(parent.getRight() == toRemove) {
lr = 'r';
}
else {
lr = 'l';
}
// if leaf
if(toRemove.getRight() == null && toRemove.getLeft()==null) {
// deindexing toRemove
if(lr =='r') {
parent.setRight(null);
}
else {
parent.setLeft(null);
}
}
// if toRemove has one subtree
else if(toRemove.getRight() == null || toRemove.getLeft()==null) {
// if the child has right subtree connect it to parent thus deindexing toRemove
if(toRemove.getRight()!=null) {
if(lr =='r') {parent.setRight(toRemove.getRight()); }
else {parent.setLeft(toRemove.getRight());}
}
// if the child has a left subtree connect it to parent thus deindexing toRemove
else {
if(lr == 'r') { parent.setRight(toRemove.getLeft());}
else {parent.setLeft(toRemove.getLeft());}
}
}
/* if two children for toRemove
* if toRemove is right child:
* find smallest possible key in right toRemove.child subtrees and replace (should lead to leaf)
* if toRemove is left child:
* find largest possible key in left toRemove.child subtrees and replace(should also lead to leaf)
*/
else {
Node<K,V> replacementNode;
// if right child
if(lr == 'r') {
replacementNode = toRemove.getRight();
while(replacementNode.getLeft()!=null) {
replacementNode = replacementNode.getLeft();
}
// set parent of the replacement node to null
replacementNode.getParent().setLeft(null);
// set the parent of the replacement node to the parent node of toRemove
replacementNode.setParent(toRemove.getParent());
// set left and right nodes of new node to those of toRemove
replacementNode.setLeft(toRemove.getLeft());
replacementNode.setRight(toRemove.getRight());
// set the parent's right child to new replacement node
replacementNode.getParent().setRight(replacementNode);
// set children of replacements node parent as replacement Node thus completely deindexing toRemove Node
replacementNode.getRight().setParent(replacementNode);
replacementNode.getLeft().setParent(replacementNode);
}
// if left child
else {
replacementNode = toRemove.getLeft();
while(replacementNode.getRight()!=null) {
replacementNode = replacementNode.getRight();
}
// set parent of the replacement node to null
replacementNode.getParent().setRight(null);
// set the parent of the replacement node to the parent node of toRemove
replacementNode.setParent(toRemove.getParent());
// set left and right nodes of new node to those of toRemove
replacementNode.setLeft(toRemove.getLeft());
replacementNode.setRight(toRemove.getRight());
// set the parent's left child to new replacement node
replacementNode.getParent().setLeft(replacementNode);
// set children of replacements node parent as replacement Node thus completely deindexing toRemove Node
replacementNode.getRight().setParent(replacementNode);
replacementNode.getLeft().setParent(replacementNode);
}
}
}
return toRemove.getValue();
}
public void add(K kluch, V val) {
Node<K,V> addition = new Node<K,V>(val, kluch);
if(root == null) {
root = addition;
}
else {
Node<K,V> holder = root;
while(true) {
if((holder.getKey()).compareTo(kluch)==0) {
holder.setValue(val);
break;
}
else if(holder.getKey().compareTo(kluch)<0) {
//Sets Right Node and Parent
if(holder.getRight()==null) {
holder.setRight(addition);
holder.getRight().setParent(holder);
break;
}
// traverses to right
else {
holder= holder.getRight();
}
}
else {
//Sets Left Node and Parent
if(holder.getLeft()==null) {
holder.setLeft(addition);
holder.getLeft().setParent(holder);
break;
}
//traverses to left
else {
holder= holder.getLeft();
}
}
}
}
}
public V lookup(K key) {
Node<K,V> holdptr = root;
if(root ==null) {
return null;
}
while(holdptr!=null) {
if(holdptr.getKey()!=null && holdptr.getKey().compareTo(key)!=0) {
if(holdptr.getKey().compareTo(key)<0) {
holdptr = holdptr.getRight();
}
else {
holdptr = holdptr.getLeft();
}
}
else {break;}
}
if(holdptr == null) {return null;}
else {return holdptr.getValue();}
}
public void inOrderTraverse() {
Node<K,V> holdptr = root;
traversal(holdptr);
}
private Node<K,V> nodeLookup(K key){
Node<K,V> holdptr = root;
while(holdptr!=null) {
if(holdptr.getKey().compareTo(key)!=0 && holdptr.getKey()!=null) {
if(holdptr.getKey().compareTo(key)>0) {
holdptr = holdptr.getRight();
}
else {
holdptr = holdptr.getLeft();
}
}
else {
break;
}
}
return holdptr;
}
private void traversal(Node<K,V> n) {
if(n != null) {
traversal(n.getLeft());
nodePrint(n);
traversal(n.getRight());
}
}
private void nodePrint(Node<K,V> n) {
System.out.println("(" + n.getKey() + ", " + n.getValue() + ")" );
}
}