-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
241 lines (202 loc) · 4.3 KB
/
BinarySearchTree.java
File metadata and controls
241 lines (202 loc) · 4.3 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
package lecture9a20;
public class BinarySearchTree {
private class Node {
int data;
Node left;
Node right;
}
private Node root;
public BinarySearchTree(int[] in) {
this.root = construct(in, 0, in.length - 1);
}
private Node construct(int[] in, int ilo, int ihi) {
if (ilo > ihi) {
return null;
}
int mid = (ilo + ihi) / 2;
Node nn = new Node();
nn.data = in[mid];
// left
nn.left = construct(in, ilo, mid - 1);
// right
nn.right = construct(in, mid + 1, ihi);
return nn;
}
public void display() {
System.out.println("--------------------------");
display(this.root);
System.out.println("--------------------------");
}
// O(n)
private void display(Node node) {
if (node == null)
return;
String str = "";
if (node.left == null) {
str += ".";
} else {
str += node.left.data;
}
str += "->" + node.data + "<-";
if (node.right == null) {
str += ".";
} else {
str += node.right.data;
}
System.out.println(str);
display(node.left);
display(node.right);
}
//O(n)
public int size() {
return size(this.root);
}
private int size(Node node) {
if (node == null)
return 0;
int ls = size(node.left);
int rs = size(node.right);
return ls + rs + 1;
}
//O(n)
public int max() {
return max(this.root);
}
private int max(Node node) {
if (node.right == null) {
return node.data;
}
return max(node.right);
}
// O(n)
public boolean find(int item) {
return find(item, this.root);
}
private boolean find(int item, Node node) {
if (node == null)
return false;
if (node.data > item) {
return find(item, node.left);
} else if (node.data < item) {
return find(item, node.right);
} else {
return true;
}
}
// O(n)
public int ht() {
return ht(this.root);
}
private int ht(Node node) {
if (node == null)
return -1;
int lht = ht(node.left);
int rht = ht(node.right);
int maxht = lht > rht ? lht : rht;
maxht++;
return maxht;
}
public void printDec() {
printDec(this.root);
System.out.println("");
}
// O(n)
private void printDec(Node node) {
if (node == null)
return;
printDec(node.right);
System.out.print(node.data + " ");
printDec(node.left);
}
public void ReplaceWithSumOfLarger() {
HeapMover mover = new HeapMover();
ReplaceWithSumOfLarger(this.root, mover);
}
private class HeapMover {
int sum = 0;
}
// O(n)
private void ReplaceWithSumOfLarger(Node node, HeapMover mover) {
if (node == null) {
return;
}
ReplaceWithSumOfLarger(node.right, mover);
int temp = node.data;
node.data = mover.sum;
mover.sum += temp;
ReplaceWithSumOfLarger(node.left, mover);
}
public void printInRange(int lo, int hi) {
printInRange(this.root, lo, hi);
}
private void printInRange(Node node, int lo, int hi) {
if (node == null)
return;
if (hi < node.data) {
printInRange(node.left, lo, hi);
} else if (node.data < lo) {
printInRange(node.right, lo, hi);
} else {
printInRange(node.left, lo, hi);
System.out.println(node.data);
printInRange(node.right, lo, hi);
}
}
public void add(int item) {
add(this.root, item);
}
private void add(Node node, int item) {
if (node.data > item) {
if (node.left == null) {
Node nn = new Node();
nn.data = item;
nn.left = null;
nn.right = null;
node.left = nn;
return;
}
add(node.left, item);
} else {
if (node.right == null) {
Node nn = new Node();
nn.data = item;
nn.left = null;
nn.right = null;
node.right = nn;
return;
}
add(node.right, item);
}
}
public void remove(int item) {
remove(null, this.root, item);
}
private void remove(Node parent, Node node, int item) {
if (node.data > item) {
remove(node, node.left, item);
} else if (node.data < item) {
remove(node, node.right, item);
} else {
if (node.left == null && node.right == null) {
if (parent.right == node)
parent.right = null;
else
parent.left = null;
} else if (node.left == null) {
if (parent.right == node)
parent.right = node.right;
else
parent.left = node.right;
} else if (node.right == null) {
if (parent.right == node)
parent.right = node.left;
else
parent.left = node.left;
} else {
int temp = max(node.left);
remove(node, node.left, temp);
node.data = temp;
}
}
}
}