-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBinarySearchTree.js
63 lines (56 loc) · 1.94 KB
/
BinarySearchTree.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
/*
Binary tree - is a tree data structure in which each node has at most two children,
which are referred to as the left child and the right child.
Binary search tree - keep their keys in the sorted order so that lookup and other operations can use the principle of binary search.
- each comparison allows the operations to skip about half of the tree, so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree.
- This is much better than the linear time required to find items by key in an (unsorted) array but slower than the corresponding operations on hash tables.
*/
class BinarySearchTree {
constructor() {
this.root = null
}
insertNode(val) {
var node = {
data: val,
left: null,
right: null
};
var currentNode
if (!this.root) {
this.root = node
} else {
currentNode = this.root
while (currentNode) {
if (val < currentNode.data) {
if (!currentNode.left) {
currentNode.left = node
break
} else {
currentNode = currentNode.left
}
} else if (val > currentNode.data) {
if (!currentNode.right) {
currentNode.right = node
break
} else {
currentNode = currentNode.right
}
} else {
console.log('Ignoring this value due to duplicate values')
break
}
}
}
}
}
var BST = new BinarySearchTree();
BST.insertNode(8);
BST.insertNode(3);
BST.insertNode(10);
BST.insertNode(1);
BST.insertNode(6);
BST.insertNode(14);
BST.insertNode(4);
BST.insertNode(7);
BST.insertNode(13);
console.log(BST);