-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearchTree.js
239 lines (210 loc) · 6.93 KB
/
binarySearchTree.js
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
// assuming that the given input has no duplicates
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class Tree {
constructor(arr) {
this.root = null;
this.root = this.buildTree(arr);
}
buildTree(arr) {
arr.forEach((value) => {
this.insert(value);
});
return this.root;
}
// iterative insertion of nodes
insert(value) {
if (!this.root) {
this.root = new Node(value);
return;
}
let currentNode = this.root;
while (currentNode) {
if (value < currentNode.value) {
if (!currentNode.left) {
currentNode.left = new Node(value);
break;
}
currentNode = currentNode.left;
} else {
if (!currentNode.right) {
currentNode.right = new Node(value);
break;
}
currentNode = currentNode.right;
}
}
}
// delete using successor recursively
delete(value) {
this.root = this.deleteNode(this.root, value);
}
deleteNode(node, value) {
if (node === null) {
return null;
}
if (value < node.value) {
node.left = this.deleteNode(node.left, value);
} else if (value > node.value) {
node.right = this.deleteNode(node.right, value);
} else {
// the 4 cases
if (node.left === null) {
return node.right;
} else if (node.right === null) {
return node.left;
}
node.value = this.minValue(node.right);
node.right = this.deleteNode(node.right, node.value);
}
return node;
}
minValue(node) {
let min = node.value;
while (node.left !== null) {
min = node.left.value;
node = node.left;
}
return min;
}
// the traversal methods constructing the string
levelOrder(node = this.root, string = "") {
if (node === null) {
return;
}
const queue = [];
queue.push(node);
while (queue.length > 0) {
const currentNode = queue.shift();
string += currentNode.value + " ";
if (currentNode.left !== null) {
queue.push(currentNode.left);
}
if (currentNode.right !== null) {
queue.push(currentNode.right);
}
}
return string;
}
inOrder(node = this.root, string = "") {
if (node === null) {
return string;
}
// Recursively traverse the left subtree and append to string
string = this.inOrder(node.left, string);
// Append the current node's value to the string
string += node.value + " ";
// Recursively traverse the right subtree and append to string
string = this.inOrder(node.right, string);
// Return the updated string
return string;
}
preOrder(node = this.root, string = "") {
if (node === null) {
return string;
}
string += node.value + " ";
string = this.preOrder(node.left, string);
string = this.preOrder(node.right, string);
return string;
}
postOrder(node = this.root, string = "") {
if (node === null) {
return string;
}
string = this.postOrder(node.left, string);
string = this.postOrder(node.right, string);
string += node.value + " ";
return string;
}
}
const prettyPrint = (node, prefix = "", isLeft = true) => {
if (node === null) {
return "";
}
let result = "";
if (node.right !== null) {
result += prettyPrint(node.right, `${prefix}${isLeft ? "│ " : " "}`, false);
}
result += `${prefix}${isLeft ? "└── " : "┌── "}${node.value}\n`;
if (node.left !== null) {
result += prettyPrint(node.left, `${prefix}${isLeft ? " " : "│ "}`, true);
}
return result;
};
class UIManager {
constructor() {
this.tree = null;
this.input = document.getElementById("input");
this.output = document.getElementsByClassName("output");
this.insertButton = document.getElementById("insert");
this.deleteButton = document.getElementById("delete");
this.clearButton = document.getElementById("clear");
this.inOrderOutput = document.getElementById("inorder");
this.preOrderOutput = document.getElementById("preorder");
this.postOrderOutput = document.getElementById("postorder");
this.levelOrderOutput = document.getElementById("levelorder");
this.prettyOutput = document.getElementById("pretty-output");
this.insertButton.addEventListener("click", this.insert.bind(this));
this.deleteButton.addEventListener("click", this.delete.bind(this));
this.clearButton.addEventListener("click", this.clear.bind(this));
}
insert() {
const value = this.input.value;
if (value === "") {
return;
}
if (this.tree === null) {
this.tree = new Tree([value]);
} else {
this.tree.insert(value);
}
this.input.value = "";
this.print();
}
delete() {
const value = this.input.value;
if (value === "") {
return;
}
if (this.tree === null) {
return;
}
this.tree.delete(value);
this.input.value = "";
this.print();
}
clear() {
this.tree = null;
this.input.value = "";
this.print();
}
print() {
if (this.tree === null) {
this.output.innerHTML = "";
this.inOrderOutput.textContent = "In-Order: ";
this.preOrderOutput.textContent = "Pre-Order: ";
this.postOrderOutput.textContent = "Post-Order: ";
this.levelOrderOutput.textContent = "Level-Order: ";
this.prettyOutput.textContent = "";
return;
}
// Perform the tree traversals and pretty print
const inOrderString = this.tree.inOrder();
const preOrderString = this.tree.preOrder();
const postOrderString = this.tree.postOrder();
const levelOrderString = this.tree.levelOrder();
const prettyPrintString = prettyPrint(this.tree.root);
this.output.innerHTML = levelOrderString;
this.inOrderOutput.textContent = "In-Order: " + inOrderString;
this.preOrderOutput.textContent = "Pre-Order: " + preOrderString;
this.postOrderOutput.textContent = "Post-Order: " + postOrderString;
this.levelOrderOutput.textContent = "Level-Order: " + levelOrderString;
this.prettyOutput.textContent = prettyPrintString;
}}
const uiManager = new UIManager();