-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree.ts
More file actions
226 lines (196 loc) · 4.87 KB
/
binary-tree.ts
File metadata and controls
226 lines (196 loc) · 4.87 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
import chalk from "chalk";
class BTNode<T> {
value: T;
right: BTNode<T> | null;
left: BTNode<T> | null;
constructor(value: T) {
this.value = value;
this.right = null;
this.left = null;
}
}
class BinarySearchTree<T> {
root: BTNode<T> | null;
constructor() {
this.root = null;
}
// O(log n)
insert(value: T) {
const newNode = new BTNode(value);
if (!this.root) {
this.root = newNode;
return this;
}
let currNode = this.root;
// Divide & Conquer
while (currNode) {
if (value === currNode.value) {
return this; // Handle duplicate case
}
if (value > currNode.value) {
if (currNode.right === null) {
currNode.right = newNode;
return this;
}
currNode = currNode.right;
}
if (value < currNode.value) {
if (currNode.left === null) {
currNode.left = newNode;
return this;
}
currNode = currNode.left;
}
}
}
// O(log n)
lookup(value: T) {
if (!this.root) return null;
let currNode: BTNode<T> | null = this.root;
while (currNode) {
if (value === currNode.value) {
return currNode;
}
if (value > currNode.value) {
currNode = currNode.right;
} else {
currNode = currNode.left;
}
}
return null;
}
delete(value: T) {
if (!this.root) return null;
let currNode: BTNode<T> | null = this.root;
let parentNode: BTNode<T> | null = null;
// Find the node
while (currNode!.value !== value) {
parentNode = currNode;
if (value < currNode!.value) {
currNode = currNode!.left;
} else {
currNode = currNode!.right;
}
}
if (!currNode) return null; // 404 Not Found
// Case 1: Node to delete has no children (leaf node)
if (!currNode.left && !currNode.right) {
if (currNode === this.root) {
// Then it is the root
this.root = null;
return this;
}
if (parentNode!.left === currNode) {
parentNode!.left = null;
return this;
} else {
parentNode!.right = null;
return this;
}
}
// Case 2: Node has only one child
if (!currNode.left || !currNode.right) {
const child = currNode.left || currNode.right;
if (currNode === this.root) {
// Then it is the root
this.root = child;
return this;
}
if (parentNode!.left === currNode) {
parentNode!.left = child;
return this;
} else {
parentNode!.right = child;
return this;
}
}
// Case 3: Node has two children
// Find successor (smallest value in right subtree)
// Replace current node with successor
// Fix the connections
let successorParent = currNode;
let successor = currNode.right;
while (successor.left) {
successorParent = successor;
successor = successor.left;
}
if (successorParent !== currNode) {
successorParent.left = successor.right;
successor.right = currNode.right;
}
successor.left = currNode.left;
// If deleting root
if (currNode === this.root) {
this.root = successor;
} else {
if (parentNode!.left === currNode) {
parentNode!.left = successor;
} else {
parentNode!.right = successor;
}
}
return this;
}
}
function traverse<T>(node: BTNode<T>): BTNode<T> {
const tree: BTNode<T> = { value: node.value, right: null, left: null };
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);
return tree;
}
const tree = new BinarySearchTree();
tree.insert(9);
tree.insert(4);
tree.insert(6);
tree.insert(20);
tree.insert(170);
tree.insert(150);
tree.insert(180);
tree.insert(15);
tree.insert(1);
tree.delete(9);
console.log(chalk.red("Lookup 1", JSON.stringify(tree.lookup(1))), "\n\n");
if (tree.root) console.log(chalk.green(JSON.stringify(traverse(tree.root))));
/*
9
/ \
4 20
/ \ / \
1 6 15 170
/ \
150 180
*/
export const beforeDeleting20 = {
value: 9,
right: {
value: 20,
right: {
value: 170,
right: { value: 180, right: null, left: null },
left: { value: 150, right: null, left: null },
},
left: { value: 15, right: null, left: null },
},
left: {
value: 4,
right: { value: 6, right: null, left: null },
left: { value: 1, right: null, left: null },
},
};
export const afterDeleting20 = {
value: 9,
right: {
value: 150,
right: {
value: 170,
right: { value: 180, right: null, left: null },
left: null,
},
left: { value: 15, right: null, left: null },
},
left: {
value: 4,
right: { value: 6, right: null, left: null },
left: { value: 1, right: null, left: null },
},
};