-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.ts
261 lines (213 loc) · 6.81 KB
/
BinarySearchTree.ts
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
252
253
254
255
256
257
258
259
260
261
import TreeNode from './TreeNode.js';
const ErrorDuplicateKey = new Error("duplicate key")
const ErrorKeyNoExist = new Error("key doesn't exist")
const ErrorNoRoot = new Error("no root")
// Right >
// Left <
class Tree {
root: TreeNode;
_count: number;
constructor() {
this.root = null;
this._count = 0;
}
count(): number {
return this._count;
}
min(startingNode: TreeNode = this.root): TreeNode {
if(!this.root) {
return null;
}
let current = startingNode;
while(current) {
if(current.left == null) {
return current;
}
current = current.left;
}
}
max(): TreeNode {
if(!this.root) {
return null;
}
let current = this.root;
while(current) {
if(current.right == null) {
return current
}
current = current.right;
}
}
insert(newKey: number, newValue: string) {
this._count++;
const newNode = new TreeNode(newKey, newValue)
// If the root is nil set the root
if(!this.root) {
this.root = newNode;
return this.root;
}
let current = this.root;
while(current) {
if(newKey === current.key) {
return ErrorDuplicateKey;
}
if(newKey > current.key) {
if(current.right === null) {
current.right = newNode;
return this;
}
current = current.right
continue
}
if(newKey < current.key) {
if(current.left === null) {
current.left = newNode;
return this;
}
current = current.left
}
}
}
find(key: number): TreeNode {
if(!this.root) {
return null
}
let current = this.root;
while(current) {
if(current.key == key) {
return current
}
if(key > current.key) {
current = current.right
continue;
}
if (key < current.key) {
current = current.left
}
}
return null;
}
remove(key: number): Tree {
if (!this.root) {
return null
}
let current: TreeNode = this.root;
let parent: TreeNode = null;
while(current) {
// is leaf
if(key === current.key) {
if (!current.left && !current.right) {
if (parent.left === current) {
parent.left = null;
}
if (parent.right === current) {
parent.right = null;
}
this._count--;
return this;
}
if(current.left === null) {
if (parent.left === current) {
parent.left = current.right;
}
if (parent.right === current) {
parent.right = current.right;
}
this._count--;
return this;
}
if(current.right === null) {
if (parent.left === current) {
parent.left = current.left;
}
if (parent.right === current) {
parent.right = current.left;
}
this._count--;
return this;
}
if(current.right && current.left) {
let leftMost = this.min(current.right);
this.remove(leftMost.key);
if(!parent) {
this.root = leftMost;
this.root.left = current.left;
this.root.right = current.right;
return this;
}
if(parent.left === current) {
parent.left = leftMost;
return this;
}
if(parent.right === current) {
parent.right = leftMost;
return this;
}
this._count--;
return this;
}
}
if(key < current.key) {
parent = current;
current = current.left;
}
if(key > current.key) {
parent = current;
current = current.right;
}
}
}
dfsInOrder(): Array<number> {
let nodesToVisit = [this.root];
let visitedNodes = [];
while(nodesToVisit.length > 0) {
let current = nodesToVisit.pop()
visitedNodes.push(current.key)
if(current.left) {
nodesToVisit.push(current.left)
}
if(current.right) {
nodesToVisit.push(current.right)
}
}
return visitedNodes;
}
isValidTree(): boolean {
// Traverse Left
let isValidLeft = true;
let currentLeft = this.root;
while(currentLeft) {
if(currentLeft.left === null) {
break;
}
// If the current node is less than the child node
// Then we know that the tree is not valid because the child node
// should be greater than the current node because we're going right
console.log("L_PREV", currentLeft.key)
console.log("L_NEXT", currentLeft.left.key)
if(currentLeft.key < currentLeft.left.key) {
isValidLeft = false;
}
currentLeft = currentLeft.left;
}
// Traverse Right
let isValidRight = true;
let currentRight = this.root;
while(currentRight) {
if(currentRight.right === null) {
break;
}
// If the current node is greater than the child node
// Then we know that the tree is not valid because the child node
// should be greater than the current node because we're going right
console.log("R_PREV: ", currentRight.key)
console.log("R_NEXT: ", currentRight.right.key)
if(currentRight.key > currentRight.right.key) {
isValidRight = false;
}
currentRight = currentRight.right;
}
return isValidLeft && isValidRight;
}
}
export default Tree;