-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTree.js
73 lines (62 loc) · 1.49 KB
/
binaryTree.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
class BinaryTree {
constructor(value) {
this.value = null;
this.left = null;
this.right = null;
if (value) {
this.value = value;
}
}
add(value) {
const newNode = new BinaryTree(value);
let queue = [];
queue.push(this);
while (true) {
let node = queue.pop();
if (node.left === null) {
// if left child is null insert new node as left child
node.left = newNode;
return newNode;
} else {
// if there is a child available on left, add it to the queue.
// This node will be picked in case right child also have node
queue.unshift(node.left);
}
if (node.right === null) {
// if right child is null insert new node as left child
node.right = newNode;
return newNode;
} else {
// if there is a child available on right child, add it to the queue.
// This node will be picked in case child of left node are also filled
queue.unshift(node.right);
}
}
}
}
function printTree(root, level) {
let index = level || 0;
if (root) {
console.log(`Level ${index}: ${root.value}`);
printTree(root.left, index+1);
printTree(root.right, index+1);
}
}
const bt = new BinaryTree('0');
bt.add('1');
bt.add('2');
bt.add('3');
bt.add('4');
bt.add(5);
bt.add(6);
bt.add(7);
bt.add(8);
bt.add(9);
bt.add(10);
bt.add(11);
bt.add(12);
bt.add(13);
bt.add(14);
bt.add(15);
console.log(printTree(bt));
console.log(JSON.stringify(bt));