-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBST.js
More file actions
271 lines (202 loc) · 5.46 KB
/
BST.js
File metadata and controls
271 lines (202 loc) · 5.46 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// binary search tree
// 15
// / \
// 10 25
// / \ / \
// 7 13 22 27
// / \ /
// 5 9 17
class Node{
// initializing the node (each node has a data, left node and right node)
constructor(data){
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree{
// initializing the root to null
constructor(){
this.root = null;
}
getRoot(){
return this.root;
}
// add new node to the tree
insert(data){
const newNode = new Node(data);
if(this.root === null){
this.root = newNode;
}else{
this.insertNode(this.root, newNode);
}
}
// helper recursive method for adding a new node
insertNode(node, newNode){
if(newNode.data < node.data){
if(node.left === null){
node.left = newNode;
}else{
this.insertNode(node.left, newNode);
}
}else {
if(node.right === null){
node.right = newNode;
}else{
this.insertNode(node.right,newNode);
}
}
}
// binary search
search(data){
return this.searchNode(this.root, data);
}
// helper recursive method for binary search
searchNode(node, data){
if(node === null){
return null;
}else if(data < node.data){
return this.searchNode(node.left, data);
}else if(data > node.data){
return this.searchNode(node.right, data);
}else{
return node;
}
}
remove(data){
this.root = this.removeNode(this.root, data);
}
removeNode(node, data){
if(node == null){
return null;
}else if(data < node.data){
node.left = this.removeNode(node.left,data);
return node;
}else if(data > node.data){
node.right = this.removeNode(node.right,data);
return node;
}else{
// delete leaf node (with no children)
if(node.left === null && node.right === null){
node = null;
return node;
}
// delete node with one children
if(node.left === null){
node = node.right;
return node;
}else if(node.right === null){
node = node.left;
return node;
}
// delete node with two children
// 1- find the minimum node in the right subtree
let minRightNode = this.findMinNode(node.right);
// 2- assign the minimum node to the current node
node.data = minRightNode.data;
// 3- remove the minimum node in the right subtree
node.right = this.removeNode(node.right,minRightNode.data);
return node;
}
}
findMinNode(node){
if(node.left == null){
return node;
}else{
return this.findMinNode(node.left);
}
}
//------------------------------------------------------------ tree traversal ----------------------------------------------------------
// Depth first traversal (inorder, preorder, postorder)
// 1- inorder
inorder(node){
if(node != null){
this.inorder(node.left);
console.log(node.data);
this.inorder(node.right);
}
}
// 2- preorder
preorder(node){
if(node != null){
console.log(node.data);
this.preorder(node.left);
this.preorder(node.right);
}
}
// 3- postorder
postorder(node){
if(node != null){
this.postorder(node.left);
this.postorder(node.right);
console.log(node.data);
}
}
// Bredth First Traversal
// try to solve this problem using queue :)
}
// ------------------------------------------testing the tree------------------------------------------------
// create an object for the BinarySearchTree
const BST = new BinarySearchTree();
// Inserting nodes to the BinarySearchTree
BST.insert(15);
BST.insert(25);
BST.insert(10);
BST.insert(7);
BST.insert(22);
BST.insert(17);
BST.insert(13);
BST.insert(5);
BST.insert(9);
BST.insert(27);
// 15
// / \
// 10 25
// / \ / \
// 7 13 22 27
// / \ /
// 5 9 17
let root = BST.getRoot();
console.log(JSON.stringify(root, null, 4));
// prints 5 7 9 10 13 15 17 22 25 27
// BST.inorder(root);
// Removing node with no children
// BST.remove(5);
// 15
// / \
// 10 25
// / \ / \
// 7 13 22 27
// \ /
// 9 17
// root = BST.getRoot();
// prints 7 9 10 13 15 17 22 25 27
// BST.inorder(root);
// Removing node with one child
// BST.remove(7);
// 15
// / \
// 10 25
// / \ / \
// 9 13 22 27
// /
// 17
// root = BST.getRoot();
// prints 9 10 13 15 17 22 25 27
// BST.inorder(root);
// Removing node with two children
// BST.remove(15);
// console.dir(root);
// 17
// / \
// 10 25
// / \ / \
// 9 13 22 27
// root = BST.Root;
// console.log("inorder traversal");
// // prints 9 10 13 17 22 25 27
// BST.inorder(root);
// console.log("postorder traversal");
// BST.postorder(root);
// console.log("preorder traversal");
// BST.preorder(root);