-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.mjs
More file actions
50 lines (37 loc) · 1.12 KB
/
Copy pathtest.mjs
File metadata and controls
50 lines (37 loc) · 1.12 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
/**
* Example Javascript Binary Search Tree
*/
import BinarySearchTree from './BinarySearchTree.mjs';
const bst = new BinarySearchTree();
const nums = [50,40,10,30,100,25,22,13,80,67,97,55,43,32,5];
for (const n of nums) bst.insert(n);
console.log('> Root:', bst.root.value);
bst.printTree();
var leftDepth = 0;
var left = bst.root;
var nodes = [left.value];
while (left.left) {
leftDepth++;
left = left.left;
nodes.push(left.value);
}
console.log('> Traverse left parent nodes:', nodes.reverse().join(', '));
var rightDepth = 0;
var right = bst.root;
nodes = [right.value];
while (right.right) {
rightDepth++;
right = right.right;
nodes.push(right.value);
}
console.log('> Traverse right parent nodes:', nodes.join(', '));
console.log('> Searching for node 22');
var found = bst.find(22);
console.log('> Found node 22, left:',found.left?.value,'right:', found.right?.value);
left = bst.leftMost();
console.log('> Leftmost node:', left.value);
right = bst.rightMost();
console.log('> Rightmost node:', right.value);
const depth = Math.max(leftDepth, rightDepth);
console.log('Depth:', depth);
bst.printOrder();