-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
236 lines (214 loc) · 6.54 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
236 lines (214 loc) · 6.54 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
package org.fmz.container;
public class BinarySearchTree extends BinaryTree {
public static class BSTNode extends BinaryTreeNode implements Comparable{
/**
*
* @param dat
*/
public BSTNode(Comparable dat){
super(dat) ;
}
/**
*
* @param dat
* @param lc
* @param rc
* @param par
*/
public BSTNode(Comparable dat, BSTNode lc, BSTNode rc, BSTNode par){
super(dat, lc, rc, par) ;
}
/**
*
* @param o
*/
public int compareTo(Object o){
Comparable c = (Comparable)((BSTNode)o).data;
return ((Comparable)data).compareTo(c) ;
}
}
/**
*
* @param target
*/
public Comparable find(BSTNode target){
BSTNode rs = findHelper((BSTNode)root, target) ;
if(rs == null)
return null;
return (Comparable)rs.data ;
}
/**
*
* @param current
* @param target
*/
protected BSTNode findHelper(BSTNode current, BSTNode target){
if(current == null)
return null;
if(target.compareTo(current) < 0)
findHelper((BSTNode)current.leftChild, target) ;
if(target.compareTo(current) > 0)
findHelper((BSTNode)current.rightChild, target) ;
return current ;
}
/**
*
* @param newItem
*/
public void insert(Comparable newItem){
root = insertHelper((BSTNode)root, new BSTNode(newItem)) ;
}
/**
*
* @param current
* @param newNode
*/
protected BSTNode insertHelper(BSTNode current, BSTNode newNode){
if(current == null){
numItems ++ ;
return newNode ;
}
if(newNode.compareTo(current) < 0){
current.leftChild = insertHelper((BSTNode)current.leftChild, newNode) ;
current.leftChild.parent = current ;
}else{
current.rightChild = insertHelper((BSTNode)current.rightChild, newNode) ;
current.rightChild.parent = current ;
}
return current ;
}
/**
*
* @param current
*/
protected BSTNode maxHelper(BSTNode current){
if(current == null)
return null ;
while(current.rightChild != null)
current = (BSTNode)current.rightChild ;
return current ;
}
public Comparable maximum(){
BSTNode max = maxHelper((BSTNode)root) ;
if(max == null)
return null;
return (Comparable)max.data ;
}
/**
*
* @param current
*/
protected BSTNode minHelper(BSTNode current){
if(current == null)
return null;
while(current.leftChild != null)
current = (BSTNode)current.leftChild ;
return current ;
}
public Comparable minimum(){
BSTNode min = minHelper((BSTNode)root) ;
if(min == null)
return null;
return (Comparable)min.data ;
}
/**
*
* @param target
*/
public Comparable predecessor(Comparable target){
BSTNode found = findHelper((BSTNode)root, new BSTNode(target)) ;
if(found == null)
return null ;
if(found.leftChild != null)
return (Comparable)maxHelper((BSTNode)found.leftChild).data ;
BSTNode parent = (BSTNode)found.parent ;
while(parent != null && parent.compareTo(found) > 0)
parent = (BSTNode)parent.parent ;
if(parent == null)
return null ;
return (Comparable)parent.data ;
}
/**
*
* @param target
*/
public void remove(Comparable target){
removeHelper((BSTNode)root, new BSTNode(target)) ;
}
/**
*
* @param current
* @param target
*/
protected BSTNode removeHelper(BSTNode current, BSTNode target){
if(current == null)
return null ;
if(target.compareTo(current) < 0)
current.leftChild = removeHelper((BSTNode)current.leftChild, target) ;
else if(target.compareTo(current) > 0)
current.rightChild = removeHelper((BSTNode)current.rightChild, target) ;
else{
if(current.isLeaf()){
numItems -- ;
return null ;
}
BSTNode temp ;
if(current.leftChild == null){
temp = (BSTNode)current.rightChild ;
current.data = temp.data ;
current.leftChild = temp.leftChild ;
if(current.leftChild != null)
current.leftChild.parent = current ;
current.rightChild = temp.rightChild ;
if(current.rightChild != null)
current.rightChild.parent = current ;
}
if(current.rightChild == null){
temp = (BSTNode)current.leftChild ;
current.data = temp.data ;
current.leftChild = temp.leftChild ;
if(current.leftChild != null)
current.leftChild.parent = current ;
current.rightChild = temp.rightChild ;
if(current.rightChild != null)
current.rightChild.parent = current ;
}else{
temp = (BSTNode)current.rightChild ;
if(temp.isLeaf()){
current.data = temp.data ;
current.rightChild = null ;
}else if(temp.leftChild == null){
current.data = temp.data ;
current.rightChild = temp.rightChild ;
if(current.rightChild != null)
current.rightChild.parent = current ;
}else{
while(temp.leftChild.leftChild != null)
temp = (BSTNode)temp.leftChild ;
current.data = temp.leftChild.data ;
removeHelper((BSTNode)temp, new BSTNode((Comparable)temp.leftChild.data)) ;
numItems ++ ;
}
}
numItems -- ;
}
return current ;
}
/**
*
* @param target
*/
public Comparable successor(Comparable target){
BSTNode found = findHelper((BSTNode)root, new BSTNode(target)) ;
if(found == null)
return null ;
if(found.rightChild != null)
return (Comparable)minHelper((BSTNode)found.rightChild).data ;
BSTNode parent = (BSTNode)found.parent ;
while(parent != null && parent.compareTo(found) <= 0)
parent = (BSTNode)parent.parent ;
if(parent == null)
return null ;
return (Comparable)parent.data ;
}
}