-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-minimal-bs-tree.mjs
50 lines (44 loc) · 1.17 KB
/
2-minimal-bs-tree.mjs
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
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
toString() {
function print(prefix, node, isLeft) {
if (!node) return;
if (prefix) {
console.log(prefix + (isLeft ? '↙️--' : '↘️--') + node.value);
} else {
console.log('▶️' + node.value);
}
print(prefix + (isLeft ? '🔽 ' : ' '), node.left, true);
print(prefix + (isLeft ? '🔽 ' : ' '), node.right, false);
}
print('', this, false);
}
}
/**
* Time complexity O(nlogn)
* @param arr Array
* @param start start index
* @param end end index of array
* @returns root node
*/
export function createMinimalBSTree(arr, start, end) {
if (end < start) {
return;
}
const mid = start + Math.floor((end - start) / 2);
const node = new Node(arr[mid]);
node.left = createMinimalBSTree(arr, start, mid - 1);
node.right = createMinimalBSTree(arr, mid + 1, end);
return node;
}
export function start() {
const arr = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
console.log(arr);
const node = createMinimalBSTree(arr, 0, arr.length - 1);
console.log(node.toString());
}
console.log(start());