-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.js
193 lines (159 loc) · 5.2 KB
/
bst.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class Node {
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree{
constructor(){
this.root = null;
}
// insert function
insert(value){
// create a new node
const newNode = new Node(value);
// check if root exists
if(!this.root){
this.root = newNode;
return this;
}else{
// initialize the current with the root node
let current = this.root;
// Keep looping forever, will only stop once things get returned
while(true){
// check if the value is smaller then the current's value
if(value < current.value){
// keep going left if it exists
if(current.left){
current = current.left;
}else{
// if left isn't found, then assign the new node and return the tree
current.left = newNode;
return this;
}
}else{
// check if the value is larger then the current's value
// keep going left if it exists
if(current.right){
current = current.right;
}else{
// if right isn't found, then assign the new node and return the tree
current.right = newNode;
return this;
}
}
}
}
}
contains(value){
if(!this.root) return false;
let current = this.root;
while(current && !isFound){
if(value < current.value){
current = current.left;
}else if(value> current.value){
current = current.right;
}else{
return true;
// found = true;
}
// if(!isFound) return undefined;
// return current;
return false;
}
}
// breadth first traversal
bfs(){
// initialize queue, visisted array and current node with root node
let queue=[], visited = [], node = this.root;
// Add the root node to the queue
queue.push(node);
// Loop until queue isn't empty
while(queue.length !== 0){
// get the first item present in the queue
node = queue.shift();
// push the newly deleted out item from the queue
visited.push(node);
// if left contains a node, then add it the queue
if(node.left){
queue.push(node.left)
}
// if right property contains a node, then add it to the queue
if(node.right){
queue.push(node.right)
}
}
return visited;
}
dfsPreOrder(){
let visited= [];
// helper function with current variable
const traverse = (node) => {
// push the node to the visited array
visited.push(node);
// if left property of the node exists, then call this function on it as well
if(node.left){
traverse(node.left)
}
// if right property of the node exists, then call this function on it as well
if(node.right){
traverse(node.right)
}
}
// call the traverse function for the root node
traverse(this.root)
return visited;
}
dfsPostOrder(){
let visited= [];
// helper function with current variable
const traverse = (node) => {
// if left property of the node exists, then call this function on it as well
if(node.left){
traverse(node.left)
}
// if right property of the node exists, then call this function on it as well
if(node.right){
traverse(node.right)
}
// push the node to the visited array
visited.push(node);
}
// call the traverse function for the root node
traverse(this.root)
console.log(visited)
return visited;
}
dfsInOrder(){
let visited= [];
// helper function with current variable
const traverse = (node) => {
// if left property of the node exists, then call this function on it as well
if(node.left){
traverse(node.left)
}
// push the node to the visited array
visited.push(node);
// if right property of the node exists, then call this function on it as well
if(node.right){
traverse(node.right)
}
}
// call the traverse function for the root node
traverse(this.root)
console.log(visited)
return visited;
}
}
const tree = new BinarySearchTree();
tree.insert(10);
tree.insert(6);
tree.insert(15);
tree.insert(3);
tree.insert(8);
tree.insert(20);
// tree.bfs();
// tree.dfsPreOrder();
// tree.dfsPostOrder();
// tree.dfsInOrder();