forked from ievolved/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearchtree.js
85 lines (70 loc) · 2.38 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
class BinarySearchTree {
constructor() {
//initially root is null
this.root = null;
}
insertNumberNode(data, left = null, right = null) {
//creating a Node
//data we pass will act as individual parent Node
//left and right are subtrees
let Node = {
data,
left,
right
};
//suppose currentNumberNode as a parent node
//current Num Node value decides position of next value
//if it goes to left subtree or right subtree
let currentNumberNode;
if (!this.root) {
//if its not a root make it one by passing a Node
this.root = Node;
} else {
//and if its a root now, assign it to currentNumberNode
currentNumberNode = this.root;
while (currentNumberNode) {
//if data is smaller than cuurent data, send it in left subtree
if (data < currentNumberNode.data) {
//if current num node don't have Node properties
//we will assign it node properties
if (!currentNumberNode.left) {
currentNumberNode.left = Node;
break;
} else {
//if it has node properties and it is sent by root to left
//we will make it a left node because it is smaller than root node
currentNumberNode = currentNumberNode.left;
}
//if data is larger than cuurent data, send it in right subtree
} else if (data > currentNumberNode.data) {
//if current num node don't have Node properties
//we will assign it node properties
if (!currentNumberNode.right) {
currentNumberNode.right = Node;
break;
} else {
//if it has node properties and it is sent by root to right
//we will make it a right node because it is larger than root node
currentNumberNode = currentNumberNode.right;
}
} else {
console.log("Try Different Value");
break;
}
}
}
}
}
let BSTtest = new BinarySearchTree();
//tests
BSTtest.insertNumberNode(10);
BSTtest.insertNumberNode(7);
BSTtest.insertNumberNode(14);
BSTtest.insertNumberNode(5);
BSTtest.insertNumberNode(13);
BSTtest.insertNumberNode(8);
BSTtest.insertNumberNode(18);
BSTtest.insertNumberNode(15);
BSTtest.insertNumberNode(6);
BSTtest;