/
BSTGraph.js
244 lines (219 loc) · 4.57 KB
/
BSTGraph.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
238
239
240
241
242
243
244
const BSTNode = require("./BSTNode");
module.exports = class BSTGraph {
#root = null;
constructor(data) {
this.root = new BSTNode(data, null);
}
add(data) {
const node = this.root;
if (node === null) {
this.root = new BSTNode(data, null);
return;
} else {
return this.placeNodeOnTree(node, data);
}
}
placeNodeOnTree(node, data) {
if (data < node.data) {
if (node.left === null) {
node.left = new BSTNode(data, node.data);
return;
} else if (node.left !== null) {
return this.placeNodeOnTree(node.left, data);
}
} else if (data > node.data) {
if (node.right === null) {
node.right = new BSTNode(data, node.data);
return;
} else if (node.right !== null) {
return this.placeNodeOnTree(node.right, data);
}
} else {
return null;
}
}
findNode(data) {
let current = this.root;
while (current.data !== data) {
if (data < current.data) {
current = current.left;
} else {
current = current.right;
}
if (current === null) {
return null;
}
}
return current;
}
printTreeBySearch(node) {
console.log(`node ${node.data} with parent ${node.parent}`);
if (node.left !== null) {
this.printTreeBySearch(node.left);
}
if (node.right !== null) {
this.printTreeBySearch(node.right);
}
if (node === null) {
return;
}
}
printTree() {
this.printTreeBySearch(this.root);
}
isPresent(data) {
let current = this.root;
while (current) {
if (data === current.data) {
return true;
}
if (data < current.data) {
current = current.left;
} else {
current = current.right;
}
}
return false;
}
removeNode(node, data) {
if (node == null) {
return null;
}
if (data === node.data) {
// node has no children
if (node.left === null && node.right === null) {
return null;
}
// node has no left child
if (node.left === null) {
return node.right;
}
// node has no right child
if (node.right === null) {
return node.left;
}
// node has two children
let tempNode = node.right;
while (tempNode.left !== null) {
tempNode = tempNode.left;
}
node.data = tempNode.data;
node.right = this.removeNode(node.right, tempNode.data);
return node;
} else if (data < node.data) {
node.left = this.removeNode(node.left, data);
return node;
} else {
node.right = this.removeNode(node.right, data);
return node;
}
}
remove(data) {
this.root = this.removeNode(this.root, data);
}
isBalanced() {
return this.findMinHeight() >= this.findMaxHeight() - 1;
}
findMinHeight(node = this.root) {
if (node == null) {
return -1;
}
let left = this.findMinHeight(node.left);
let right = this.findMinHeight(node.right);
if (left < right) {
return left + 1;
} else {
return right + 1;
}
}
findMaxHeight(node = this.root) {
if (node == null) {
return -1;
}
let left = this.findMaxHeight(node.left);
let right = this.findMaxHeight(node.right);
if (left > right) {
return left + 1;
} else {
return right + 1;
}
}
traverseInOrder(node, result) {
if (node.left !== null) {
this.traverseInOrder(node.left, result);
}
result.push(node.data);
if (node.right !== null) {
this.traverseInOrder(node.right, result);
}
}
traversePreOrder(node, result) {
result.push(node.data);
if (node.left !== null) {
this.traversePreOrder(node.left, result);
}
if (node.right !== null) {
this.traversePreOrder(node.right, result);
}
}
traversePostOrder(node, result) {
if (node.left !== null) {
this.traversePostOrder(node.left, result);
}
if (node.right !== null) {
this.traversePostOrder(node.right, result);
}
result.push(node.data);
}
// smallest to largest
inOrder() {
if (this.root == null) {
return null;
} else {
const result = [];
this.traverseInOrder(this.root, result);
return result;
}
}
// roots before leaves
preOrder() {
if (this.root == null) {
return null;
} else {
const result = [];
this.traversePreOrder(this.root, result);
return result;
}
}
// leaves before roots
postOrder() {
if (this.root == null) {
return null;
} else {
const result = [];
this.traversePostOrder(this.root, result);
return result;
}
}
// breadth first search
levelOrder() {
let result = [];
let Q = [];
if (this.root != null) {
Q.push(this.root);
while (Q.length > 0) {
let node = Q.shift();
result.push(node.data);
if (node.left != null) {
Q.push(node.left);
}
if (node.right != null) {
Q.push(node.right);
}
}
return result;
} else {
return null;
}
}
};