-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathbinary_search_tree.js
147 lines (133 loc) · 3.81 KB
/
binary_search_tree.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
class Node {
constructor(value) {
this.left = null;
this.right = null;
this.value = value;
}
}
class BinarySearchTree {
constructor() {
this.root = null
}
insert(value) {
const newNode = new Node(value);
if(this.root === null) {
this.root = newNode;
return this;
}
let currentNode = this.root;
while(true) {
if(value < currentNode.value) {
// Left
if(currentNode.left === null) {
currentNode.left = newNode;
return this;
}
currentNode = currentNode.left
} else if (value > currentNode.value){
// Right
if(currentNode.right === null) {
currentNode.right = newNode;
return this;
}
currentNode = currentNode.right;
} else {
return this;
}
}
}
lookup(value) {
if(this.root === null) {
return false;
}
let currentNode = this.root;
while(currentNode) {
if(value < currentNode.value) {
currentNode = currentNode.left;
} else if(value > currentNode.value) {
currentNode = currentNode.right;
} else {
return true;
}
}
return false;
}
minValueNode(currentNode) {
while(true) {
if(!currentNode.left) return currentNode;
currentNode = currentNode.left;
}
}
maxValueNode(currentNode) {
while(true) {
if(!currentNode.right) return currentNode;
currentNode = currentNode.right;
}
}
bfs() {
let currentNode = this.root;
let queue = [];
let output = [];
queue.push(currentNode);
while(queue.length) {
currentNode = queue.shift();
output.push(currentNode.value);
if(currentNode.left) queue.push(currentNode.left);
if(currentNode.right) queue.push(currentNode.right);
}
return output;
}
dfsPreOrder() {
let output = [];
function traverse(currentNode) {
output.push(currentNode.value);
if(currentNode.left) traverse(currentNode.left);
if(currentNode.right) traverse(currentNode.right);
}
traverse(this.root);
return output;
}
dfsPostOrder() {
let output = [];
function traverse(currentNode) {
if(currentNode.left) traverse(currentNode.left);
if(currentNode.right) traverse(currentNode.right);
output.push(currentNode.value);
}
traverse(this.root);
return output;
}
dfsInOrder() {
let output = [];
function traverse(currentNode) {
if(currentNode.left) traverse(currentNode.left);
output.push(currentNode.value);
if(currentNode.right) traverse(currentNode.right);
}
traverse(this.root);
return output;
}
}
const tree = new BinarySearchTree();
tree.insert(20);
tree.insert(15);
tree.insert(25);
tree.insert(12);
tree.insert(16);
tree.insert(22);
tree.insert(30);
tree.insert(10);
tree.insert(14);
tree.insert(28);
tree.insert(35);
console.dir(tree, {depth: null});
console.log(tree.lookup(25));
console.log(tree.lookup(7));
console.log(tree.lookup(10));
console.log(tree.lookup(12));
console.log(tree.minValueNode(tree.root.left));
console.log(tree.maxValueNode(tree.root.right));
console.log(tree.bfs());
console.log(tree.dfsPreOrder());
console.log(tree.dfsPostOrder());
console.log(tree.dfsInOrder());