-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbinary-tree.js
More file actions
212 lines (163 loc) · 4.22 KB
/
binary-tree.js
File metadata and controls
212 lines (163 loc) · 4.22 KB
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
class BST {
#root = null;
#compare;
constructor(compareFn = null) {
if(compareFn && typeof compareFn === 'function') {
this.#compare = compareFn;
} else {
this.#compare = (a, b) => {
if(a > b) return BST.comparison.BIGGER;
if(a < b) return BST.comparison.SMALLER;
return BST.comparison.EQUAL;
}
}
}
get root() {
return this.#root;
}
get min() {
if(this.root === null) return null;
return this.#minNode(this.root).key;
}
get max() {
if(this.root === null) return null;
return this.#maxNode(this.root).key;
}
static get comparison() {
return Object.freeze({
BIGGER: 1,
BIGGER_OR_EQUAL: [1, 0],
SMALLER: -1,
SMALLER_OR_EQUAL: [-1, 0],
EQUAL: 0
})
}
createNode(key) {
return Object.seal({
key, left: null, right: null
})
}
insert(key) {
const newNode = this.createNode(key);
if(this.root === null) {
this.#root = newNode;
} else {
this.#insertNode(newNode);
}
}
print() {
this.#printNode();
}
traverseInOrder(cb) {
this.#inOrder(this.root, cb);
}
traverseReversedOrder(cb) {
this.#reverseOrder(this.root, cb);
}
traversePreOrder(cb) {
this.#preOrder(this.root, cb);
}
traversePostOrder(cb) {
this.#postOrder(this.root, cb);
}
search(key) {
return this.#searchTree(key);
}
remove(key) {
this.#root = this.#removeNode(key);
}
#removeNode = (key, node = this.root) => {
if(node === null) return null;
if(this.#compare(key, node.key) === BST.comparison.SMALLER) {
node.left = this.#removeNode(key, node.left);
return node;
}
if(this.#compare(key, node.key) === BST.comparison.BIGGER) {
node.right = this.#removeNode(key, node.right);
return node;
}
if(node.left === null && node.right === null) {
node = null;
} else if(node.left === null) {
node = node.right;
} else if(node.right === null) {
node = node.left;
} else {
const max = this.#maxNode(node.left);
node.key = max.key;
node.left = this.#removeNode(max.key, node.left)
}
return node;
}
#maxNode = (node) => {
while(node && node.right) {
node = node.right;
}
return node;
}
#minNode = (node) => {
while(node && node.left) {
node = node.left;
}
return node;
}
#searchTree = (key, node = this.root) => {
if(node === null) return false;
if(this.#compare(key, node.key) === BST.comparison.EQUAL) return true;
if(this.#compare(key, node.key) === BST.comparison.SMALLER) {
return this.#searchTree(key, node.left);
}
return this.#searchTree(key, node.right);
}
#postOrder = (node, cb) => {
if(node !== null) {
this.#postOrder(node.left, cb);
this.#postOrder(node.right, cb);
cb(node.key);
}
}
#preOrder = (node, cb) => {
if(node !== null) {
cb(node.key);
this.#preOrder(node.left, cb);
this.#preOrder(node.right, cb);
}
}
#reverseOrder = (node, cb) => {
if(node !== null) {
this.#reverseOrder(node.right, cb);
cb(node.key);
this.#reverseOrder(node.left, cb);
}
}
#inOrder = (node, cb) => {
if(node !== null) {
this.#inOrder(node.left, cb);
cb(node.key);
this.#inOrder(node.right, cb);
}
}
#insertNode = (newNode, currentNode = this.root) => {
if(this.#compare(newNode.key, currentNode.key) === BST.comparison.SMALLER) {
if(currentNode.left === null) {
currentNode.left = newNode;
} else {
this.#insertNode(newNode, currentNode.left)
}
} else {
if(currentNode.right === null) {
currentNode.right = newNode;
} else {
this.#insertNode(newNode, currentNode.right)
}
}
}
#printNode = (node = this.root, spaceCount = 0, label = '* ') => {
if(node === null) {
return console.log(`${' -'.repeat(spaceCount)}${label}null`);
}
console.log(`${' -'.repeat(spaceCount)}${label}${node.key}`);
this.#printNode(node.right, spaceCount + 2, 'R: ');
this.#printNode(node.left, spaceCount + 2, 'L: ');
}
}