-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBST.js
More file actions
209 lines (195 loc) · 5.55 KB
/
BST.js
File metadata and controls
209 lines (195 loc) · 5.55 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
'use strict';
class TreeNode {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
get left() {
return this.left;
}
set left() {
return this.left;
}
get right() {
return this.left;
}
set right() {
return this.left;
}
}
/**
*insertData(data) ->used to insert data
*removeData(data)->used to remove data from node and this function returns the root node after deletion
*inorder()->returns inorder traversal of tree in array
*preorder()->returns preorder traversal of tree in array
*postorder()->returns postorder traversal of tree in arrayss
*search(data)->returns the node with the given data if not found retruns null
*getRootNode()->returns root node
*findMin()->returns min node s
* @class BST
*/
class BST {
constructor() {
this.root = null;
}
insertData(data) {
let node = new TreeNode(data);
if (!this.root) {
this.root = node;
} else {
this.insertDataInToTree(node, this.root);
}
}
insertDataInToTree(node, root) {
if (node.data >= root.data) {
if (root.right) {
this.insertDataInToTree(node, root.right);
} else {
root.right = node;
}
} else {
if (root.right) {
this.insertDataInToTree(node, root.left);
} else {
root.left = node;
}
}
}
removeData(data) {
this.root = this.removeDataFromTree(data, this.root);
}
removeDataFromTree(data, root) {
if (!root) {
return null;
} else if (root.data > data) {
root.left = this.removeDataFromTree(data, root.left);
} else if (root.data < data) {
root.right = this.removeDataFromTree(data, root.right)
} else {
if (!root.left && !root.right) {
return null;
} else if (root.left && !root.right) {
return root.left;
} else if (root.right && !root.left) {
return root.right;
} else {
let tempNode = root.right;
let childNode;
let flag = 1;
if (!tempNode.left && !tempNode.right) flag = 0;
while (flag) {
tempNode = childNode ? childNode : tempNode;
if (tempNode.left) {
childNode = tempNode.left;
}
else if (tempNode.right) {
childNode = tempNode.right;
}
if (!childNode.left && !childNode.right) {
flag = 0;
}
}
if (tempNode.left && childNode) {
childNode.left = root.left;
childNode.right = root.right;
root = childNode;
tempNode.left = null;
} else if (tempNode.right && childNode) {
childNode.left = root.left;
childNode.right = root.right;
root = childNode;
tempNode.right = null;
} else {
// when tempnode->left and tempnode->right are null and child node dosen't exist
tempNode.left = root.left;
tempNode.right = null;
root = tempNode;
}
}
}
return root;
}
inorder() {
let data = [];
if (!this.root) {
return data;
}
return this.inOrderTraversal(this.root, data);
}
inOrderTraversal(root, data) {
if (root.left) {
this.inOrderTraversal(root.left, data);
}
data.push(root.data);
if (root.right) {
this.inOrderTraversal(root.right, data);
}
return data;
}
preorder() {
let data = [];
if (!this.root) {
return data;
}
return this.preOrderTraversal(this.root, data);
}
preOrderTraversal(root, data) {
data.push(root.data);
if (root.left) {
this.preOrderTraversal(root.left, data);
}
if (root.right) {
this.preOrderTraversal(root.right, data);
}
return data;
}
postOrder() {
let data = [];
if (!this.root) {
return data;
}
return this.postOrderTraversal(this.root, data);
}
postOrderTraversal(root, data) {
if (root.left) {
this.postOrderTraversal(root.left, data);
}
if (root.right) {
this.postOrderTraversal(root.right, data);
}
return data;
}
search(data) {
if (!this.root) {
return null;
}
return this.searchNode(this.root, data);
}
searchNode(root, data) {
if (!root) {
return null;
} else if (root.data > data) {
return this.searchNode(root.left, data);
} else if (root.data < data) {
return this.searchNode(root.right, data);
} else {
return root;
}
}
getRootNode() {
return this.root;
}
findMin() {
return this.findMinNode(this.root);
}
findMinNode(root) {
if (!root) {
return null;
} else if (root.left) {
return this.findMinNode(root.left);
} else {
return root;
}
}
}