-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBST.java
More file actions
224 lines (170 loc) · 4.88 KB
/
Copy pathBST.java
File metadata and controls
224 lines (170 loc) · 4.88 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
public class BST< E extends Comparable< E > > implements StorageInterface< E > {
Node< E > root;
int noOfNullVal;
public BST() {
noOfNullVal = 0;
}
@Override
public void add( E x ) {
if ( x == null ) {
noOfNullVal += 1;
System.out.println( "added a null, counter: " + noOfNullVal );
} else {
if ( root == null ) {
root = new Node< E >( x );
root.counter += 1;
System.out.println( "done root: " + x );
} else {
Node< E > current = root;
boolean posfound = false;
System.out.println(
"...........Tracing Movement................" );
while ( !posfound ) {
if ( current.value.compareTo( x ) == 0 ) {
current.counter += 1;
posfound = true;
System.out.println(
"added equal: " + x + "/" + current.counter );
// value < root => move to left
} else if ( current.value.compareTo( x ) > 0 ) {
if ( current.left == null ) {
current.left = new Node< E >( x );
current.left.counter += 1;
posfound = true;
System.out.println( "added to left: " + x + "/"
+ current.left.counter );
} else {
current = current.left;
System.out.println( "moved left" );
}
} else { // value > root => move to right
if ( current.right == null ) {
current.right = new Node< E >( x );
current.right.counter += 1;
posfound = true;
System.out.println( "added to right: " + x + "/"
+ current.right.counter );
} else {
current = current.right;
System.out.println( "moved right" );
}
}
}
}
}
}
@Override
public boolean find( E x ) {
if ( x == null ) {
if ( noOfNullVal > 0 ) {
return true;
}
return false;
} else {
Node< E > current = root;
boolean found = false;
System.out.println( "-----------------------------------------" );
System.out.println( "for: " + x );
while ( !found
|| ( current.left != null && current.right != null ) ) {
if ( current.value.compareTo( x ) == 0 ) {
found = true;
System.out.println( "found" );
return true;
} else if ( current.value.compareTo( x ) > 0 ) { // move to left
if ( current.left == null ) {
return false;
} else {
current = current.left;
System.out.println( "moved left" );
}
} else { // move to right
if ( current.right == null ) {
return false;
} else {
current = current.right;
System.out.println( "moved right" );
}
}
}
}
return false;
}
@Override
public boolean includesNull() {
if ( noOfNullVal > 0 ) {
return true;
}
return false;
}
private Node< E > minimumElement( Node< E > thisNode ) {
if ( thisNode.left == null ) return thisNode;
else {
return minimumElement( thisNode.left );
}
}
public Node< E > deleteThisElementInTree( Node< E > root, E payLoad ) {
if ( root == null ) return null;
if ( root.value.compareTo( payLoad ) > 0 ) { // see insert
root.left = deleteThisElementInTree( root.left, payLoad );
} else if ( root.value.compareTo( payLoad ) < 0 ) { // see insert
root.right = deleteThisElementInTree( root.right, payLoad );
} else {
if ( root.counter > 1 ) {
root.counter -= 1;
} else {
if ( ( root.left != null ) && ( root.right != null ) ) {
Node< E > tmp = root;
Node< E > minimumNodeOnRight = minimumElement( tmp.right );
root.value = minimumNodeOnRight.value;
root.counter = minimumNodeOnRight.counter;
root.right = deleteThisElementInTree( root.right,
minimumNodeOnRight.value );
} else if ( root.left != null ) {
root = root.left;
} else if ( root.right != null ) {
root = root.right;
} else {
root = null;
}
}
}
return root;
}
@Override
public boolean delete( E x ) {
Node< E > n = deleteThisElementInTree( root, x );
if ( n == null ) {
return false;
} else {
return true;
}
}
public String getValuesStored( Node< E > root ) {
String left = "";
String right = "";
if ( root.left == null && root.right == null ) {
return root.value.toString() + "/" + root.counter;
} else {
if ( root != null ) {
if ( root.left == null ) {
left += "L: null ";
} else {
left += "L: " + getValuesStored( root.left ) + " ";
}
if ( root.right == null ) {
right += "R: null ";
} else {
right += "R: " + getValuesStored( root.right ) + " ";
}
return "(" + left + " " + "<-[" + root.value + "/"
+ root.counter + "]->" + " " + right + ")";
} else {
return "";
}
}
}
public String toString() {
return "soManyNullValues: "+ noOfNullVal + "\n" + getValuesStored( root );
}
}