-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.js
More file actions
330 lines (295 loc) · 8.06 KB
/
Copy pathTree.js
File metadata and controls
330 lines (295 loc) · 8.06 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
var Utils = require('./Utils')
// Node class
class Node {
constructor(value) {
this.value = value
this.leftChild = null
this.rightChild = null
}
}
class Tree {
constructor() {
// root of a binary search tree
this.root = null
this._toString = (node) => {
console.log(`Node = ${node.value}`)
}
this.insert = (value) => {
var node = new Node(value)
if (this.root === null) {
// root is null then node will
// be added to the tree and made root.
this.root = node
return
}
let current = this.root
while (true) {
if (value < current.value) {
if (current.leftChild === null) {
// we found the parent
current.leftChild = node
break
}
// otherwise, go one level down
current = current.leftChild
} else {
if (current.rightChild === null) {
// we found the parent
current.rightChild = node
break
}
// otherwise, go one level down
current = current.rightChild
}
}
}
this.find = (value) => {
let current = this.root
while (current) {
if (value < current.value) {
// go to the left sub tree
if (current.leftChild) {
current = current.leftChild
} else {
return false
}
} else if (value > current.value) {
// go to the right sub tree
if (current.rightChild) {
current = current.rightChild
} else {
return false
}
} else {
return true
}
}
return false
}
this.traversePreOrder = () => {
this._traversePreOrder(this.root)
}
this._traversePreOrder = (root) => {
// Base Condition to avoid a cycle
if (!root) {
console.log('That was the leaf: stop going forward, go back!')
return
}
// root (print)
console.log(`_traversePreOrder found root (${root.value})`)
// left, recursively call the method itself
this._traversePreOrder(root.leftChild)
// right
this._traversePreOrder(root.rightChild)
}
this.traverseInOrder = () => {
this._traverseInOrder(this.root)
}
this._traverseInOrder = (root) => {
// Base Condition to avoid a cycle
if (!root) {
console.log('That was the leaf: stop going forward, go back!')
return
}
// left, recursively call the method itself
this._traverseInOrder(root.leftChild)
// root (print)
console.log(`_traverseInOrder found root (${root.value})`)
// right
this._traverseInOrder(root.rightChild)
}
this.traversePostOrder = () => {
this._traversePostOrder(this.root)
}
this._traversePostOrder = (root) => {
// Base Condition to avoid a cycle
if (!root) {
console.log('That was the leaf: stop going forward, go back!')
return
}
// left, recursively call the method itself
this._traversePostOrder(root.leftChild)
// right
this._traversePostOrder(root.rightChild)
// root (print)
console.log(`_traversePostOrder found root (${root.value})`)
}
this.height = () => {
return this._height(this.root)
}
this._height = (root) => {
// Base Condition
if (!root) {
return -1
}
// If we reach the leaf node
if (this._isLeaf(root)) {
return 0
}
const max = Math.max(
this._height(root.leftChild),
this._height(root.rightChild),
)
return 1 + max
}
this._isLeaf = (node) => {
return !node?.leftChild && !node?.rightChild
}
// O(n)
this.min = () => {
return this._min(this.root)
}
// O(log n)
this.minForBinarySearchTree = () => {
if (this.root) {
throw Utils.CustomException(
'We cannot find a minimum value in an empty tree',
)
}
let current = this.root
let last = current
while (current) {
last = current
// Keep going down the left subtree
current = current.leftChild
}
// return the leftmost child
return last.value
}
this._min = (root) => {
if (!root) {
return 0
}
// If we get to the leaf node
// return the value of that node itself
if (this._isLeaf(root)) {
return root.value
}
let left = this._min(root.leftChild)
let right = this._min(root.rightChild)
return Math.min(Math.min(left, right), root.value)
}
this.equals = (other) => {
if (!other) {
return false
}
// Compare two nodes, make sure are equal
// Then use recursion to make sure left and right sub trees are equal
return this._equals(this.root, other.root)
}
// Pre-order traversal
// root
// left
// right
this._equals = (first, second) => {
if (!first && !second) {
return true
}
if (first && second) {
return (
first.value == second.value &&
this._equals(first.leftChild, second.leftChild) &&
this._equals(first.rightChild, second.rightChild)
)
}
return false
}
this.swapRoot = () => {
let temp = this.root.leftChild
this.root.leftChild = this.root.rightChild
this.root.rightChild = temp
}
this.isBinarySearchTree = () => {
// Traverse the tree, visit each node only one
// Check if the value is in the possible range
return this._isBinarySearchTree(
this.root,
Number.MIN_VALUE,
Number.MAX_VALUE,
)
}
this._isBinarySearchTree = (node, min, max) => {
if (!node) {
console.log('Empty tree is binary search tree')
return true
}
// root
console.log('_isBinarySearchTree receive', min, node.value, max)
console.log('node.value < min ', node.value < min)
console.log('node.value > max', node.value > max)
if (node.value < min || node.value > max) {
console.log('out of range')
return false
}
return (
this._isBinarySearchTree(node.leftChild, min, node.value - 1) &&
this._isBinarySearchTree(node.rightChild, node.value + 1, max)
)
}
this.getNodesAtDistance = (distance) => {
let list = []
this._getNodesAtDistance(this.root, distance, list)
return list
}
this._getNodesAtDistance = (root, distance, list) => {
if (!root) {
return
}
// Base Condition
if (root && distance == 0) {
list.push(root.value)
return
}
this._getNodesAtDistance(root.leftChild, distance - 1, list)
this._getNodesAtDistance(root.rightChild, distance - 1, list)
}
this.traverseLevelOrder = () => {
for (var i = 0; i <= this.height(); i++) {
for (let value of this.getNodesAtDistance(i)) {
console.log(value)
}
}
}
}
}
const binarySearchTree = new Tree()
const testArray = [7, 4, 9, 1, 6, 8, 10]
testArray.forEach(binarySearchTree.insert)
// binarySearchTree.traversePreOrder()
// binarySearchTree.traverseInOrder()
// binarySearchTree.traversePostOrder()
// const height = binarySearchTree.height()
// console.log('height', height)
// const min = binarySearchTree.min()
// console.log('min', min)
const tree1 = new Tree()
tree1.insert(7)
tree1.insert(4)
tree1.insert(9)
tree1.insert(1)
tree1.insert(6)
tree1.insert(8)
tree1.insert(10)
// const list = tree1.getNodesAtDistance(2)
// console.log('list', list)
tree1.traverseLevelOrder()
// const tree2 = new Tree()
// tree2.insert(7)
// tree2.insert(4)
// tree2.insert(9)
// tree2.insert(1)
// tree2.insert(6)
// tree2.insert(8)
// tree2.insert(10)
// const isEqual = tree1.equals(null)
// console.log('isEqual', isEqual)
const tree3 = new Tree()
tree3.insert(20)
tree3.insert(10)
tree3.insert(30)
tree3.insert(21)
tree3.insert(6)
tree3.insert(3)
tree3.insert(4)
tree3.swapRoot()
// console.log('isBinarySearchTree', tree3.isBinarySearchTree())