-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.js
204 lines (168 loc) · 4.64 KB
/
BinarySearch.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
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Node
*/
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
/**
* Binary Search Tree
*/
export class BinarySearchTree {
constructor(value) {
this.root = new Node(value);
this.count = 1;
}
/**
* insert node to the tree
* @param {*} value
*/
insertNode = (value) => {
// create new node
const newNode = new Node(value);
// traverse tree
const traverseTree = node => {
// case 1: left size of the tree
// case 2: right size of the tree
if (value < node.value) {
if (!node.left) {
node.left = newNode;
} else {
traverseTree(node.left);
}
} else if (value > node.value) {
if (!node.right) {
node.right = newNode;
} else {
traverseTree(node.right);
}
}
};
traverseTree(this.root);
// count tree
this.count++;
};
/**
* get minimum value node in the tree
*/
minNode = () => {
// set current node to root node
let currentNode = this.root;
// go to the very left node of the tree
while (currentNode.left) {
currentNode = currentNode.left;
}
return currentNode.value;
};
/**
* get maximum value node in the tree
*/
maxNode = () => {
// set current node to root node
let currentNode = this.root;
// go to the very right node of the tree
while (currentNode.right) {
currentNode = currentNode.right;
}
return currentNode.value;
};
/**
* validate if value is present in the tree
* @param {*} value
*/
nodeContains = (value) => {
let currentNode = this.root;
while (currentNode) {
// case: value found at root
if (value === currentNode.value) return true;
// case: left and right
if (value < currentNode.value) {
currentNode = currentNode.left;
} else {
currentNode = currentNode.right;
}
}
return false;
};
/**
* get total size of the tree
*/
size() {
return this.count;
};
// depth first search - branch by branch
// in-order
// case: left, root, right
dfsInOrder() {
const result = [];
// traverse tree
const traverseTree = node => {
// if left child exists, go left again
if (node.left) traverseTree(node.left);
// root value
result.push(node.value);
// if right child exists, go right again
if (node.right) traverseTree(node.right);
};
traverseTree(this.root);
return result;
};
// pre-order
// case: root, left, right
dfsPreOrder() {
const result = [];
// traverse tree
const traverseTree = node => {
// root value
result.push(node.value);
// if left child exists, go left again
if (node.left) traverseTree(node.left);
// if right child exists, go right again
if (node.right) traverseTree(node.right);
};
traverseTree(this.root);
return result;
};
// post-order
// case: left, right, root
dfsPostOrder() {
const result = [];
// traverse tree
const traverseTree = node => {
// if left child exists, go left again
if (node.left) traverseTree(node.left);
// if right child exists, go right again
if (node.right) traverseTree(node.right);
// root value
result.push(node.value);
};
traverseTree(this.root);
return result;
};
// breadth first search - level by level
// use a queue
breadthFirstSearch() {
const result = [];
const queue = [];
// push to queue
queue.push(this.root);
while (queue.length) {
// pop node from queue
const currentNode = queue.shift();
// push it to result
result.push(currentNode.value);
// check for left child
if (currentNode.left) {
queue.push(currentNode.left);
}
// check for right child
if (currentNode.right) {
queue.push(currentNode.right);
}
}
return result;
};
}