-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
251 lines (214 loc) · 6.22 KB
/
BST.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
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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package Week13.BST;
/**
*
* @author szeyu
*/
import java.util.ArrayList;
public class BST<E extends Comparable<E>> {
private TreeNode<E> root;
private int size;
public BST() {
root = null;
size = 0;
}
public BST(E[] eArr) {
root = null;
size = 0;
for (int i = 0; i < eArr.length; i++) {
E e = eArr[i];
insert(e);
}
}
public boolean search(E e) {
TreeNode<E> current = root;
while (current != null) {
if (e.compareTo(current.value) == 0) {
return true;
} else if (e.compareTo(current.value) < 0) {
current = current.left;
} else {
current = current.right;
}
}
return false;
}
public boolean insert(E e) {
TreeNode<E> newNode = new TreeNode<>(e);
if (size == 0) {
root = newNode;
size++;
return true;
} else {
TreeNode<E> current = root;
TreeNode<E> parentNode = null;
while (current != null) {
parentNode = current;
if (e.compareTo(current.value) < 0) {
current = current.left;
} else if (e.compareTo(current.value) > 0) {
current = current.right;
} else {
return false; // no duplicate
}
}
if (e.compareTo(parentNode.value) < 0) {
parentNode.left = newNode;
} else {
parentNode.right = newNode;
}
size++;
return true;
}
}
public int getSize() {
return size;
}
public int height() {
return height(root);
}
private int height(TreeNode<E> node) {
if (node == null) {
return 0;
}
TreeNode<E> leftTree = node.left;
TreeNode<E> rightTree = node.right;
return 1 + Math.max(height(leftTree), height(rightTree));
}
public E getRoot() {
if (root == null) {
return null;
}
return root.value;
}
public E minValue() {
if (root == null) {
return null;
}
TreeNode<E> current = root;
while (current.left != null) {
current = current.left;
}
return current.value;
}
public E maxValue() {
if (root == null) {
return null;
}
TreeNode<E> current = root;
while (current.right != null) {
current = current.right;
}
return current.value;
}
public ArrayList<TreeNode<E>> path(E e) {
ArrayList<TreeNode<E>> arrayPath = new ArrayList<>();
TreeNode<E> current = root;
while (current != null) {
arrayPath.add(current);
if (e.compareTo(current.value) == 0) {
return arrayPath;
} else if (e.compareTo(current.value) < 0) {
current = current.left;
} else {
current = current.right;
}
}
return null;
}
public boolean delete(E e) {
TreeNode<E> current = root;
TreeNode<E> parentNode = null;
while (current != null) {
if (e.compareTo(current.value) < 0) {
parentNode = current;
current = current.left;
} else if (e.compareTo(current.value) > 0){
parentNode = current;
current = current.right;
} else {
break; // node found
}
}
if(current == null){
return false; // node not found
}
// case 1
// if current does not have a left child
if(current.left == null){
// handle the case if the delete node is root
if (parentNode == null){
root = current.right;
}
// parent don't know his current is left or right
// current attach to parent node right side
if(parentNode.value.compareTo(current.value) < 0){
parentNode.right = current.right;
}
// current attach to parent node left side
else {
parentNode.left = current.right;
}
}
// case 2
// current has a left child
else {
TreeNode<E> parentRightMost = current;
TreeNode<E> rightMost = current.left;
while(rightMost.right != null){
parentRightMost = rightMost;
rightMost = rightMost.right;
}
current.value = rightMost.value;
if(parentRightMost.right == rightMost)
parentRightMost.right = rightMost.left;
else
// special case
// if parentRightMost = current
parentRightMost.left = rightMost.left;
}
size--;
return true;
}
public boolean clear() {
root = null;
size = 0;
return true;
}
public void inorder(){
inorder(root);
}
public void preorder(){
preorder(root);
}
public void postorder(){
postorder(root);
}
protected void inorder(TreeNode<E> root) {
if (root == null) {
return;
}
inorder(root.left);
System.out.print(root.value + " ");
inorder(root.right);
}
protected void postorder(TreeNode<E> root) {
if (root == null) {
return;
}
postorder(root.left);
postorder(root.right);
System.out.print(root.value + " ");
}
protected void preorder(TreeNode<E> root) {
if (root == null) {
return;
}
System.out.print(root.value + " ");
preorder(root.left);
preorder(root.right);
}
}