-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBinarySearchTree.js
202 lines (165 loc) · 4.58 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
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
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
insert(value) {
const node = new Node(value);
if (!this.root) this.root = node;
else {
let currentNode = this.root;
while (currentNode) {
if (value < currentNode.value) {
if (currentNode.left) currentNode = currentNode.left;
else {
currentNode.left = node;
currentNode = null;
}
} else {
if (currentNode.right) currentNode = currentNode.right;
else {
currentNode.right = node;
currentNode = null;
}
}
}
}
return this;
}
remove(value) {
if (!this.root) return;
let parent;
let current = this.root;
// check if the current node on the left side or right side of the parent node
const fromLeft = (current, parent) => current.value < parent.value;
while (current) {
if (value === current.value) {
// node has been found
if (!current.left && !current.right) {
// is found node a leaf? delete found node
if (current === this.root) this.root = null;
else if (fromLeft(current, parent)) parent.left = null;
else parent.right = null;
return current; // removed node
} else if ((current.left && !current.right) || (!current.left && current.right)) {
// does found node have one child? bypass found node
if (current.left) {
if (current === this.root) this.root = current.left;
else if (fromLeft(current, parent)) parent.left = current.left;
else parent.right = current.left;
} else {
if (current === this.root) this.root = current.right;
else if (fromLeft(current, parent)) parent.left = current.right;
else parent.right = current.right;
}
return current; // removed node
} else {
// found node definitely has two children, find successor
let removed = new Node(current.value); // create node to be returned with the value about to be replaced
let successor = current.right;
let prevSuccessor = current;
while (successor.left) {
prevSuccessor = successor;
successor = successor.left;
}
// check to see if the successor is further down or is a direct child of current
// append the replace successor with its children on the right
if (prevSuccessor !== current) prevSuccessor.left = successor.right;
else prevSuccessor.right = successor.right;
current.value = successor.value;
return removed;
}
} else {
// keep searching
parent = current;
if (value < current.value) current = current.left;
else current = current.right;
}
}
return removed;
}
find(value) {
let currentNode = this.root;
while (currentNode) {
if (value === currentNode.value) return currentNode;
else if (value < currentNode.value) currentNode = currentNode.left;
else currentNode = currentNode.right;
}
return;
}
DFSPreOrder() {
const result = [];
(function helper(node) {
if (!node) return;
result.push(node.value);
helper(node.left);
helper(node.right);
})(this.root);
return result;
}
DFSInOrder() {
const result = [];
(function helper(node) {
if (!node) return;
helper(node.left);
result.push(node.value);
helper(node.right);
})(this.root);
return result;
}
DFSPostOrder() {
const result = [];
(function helper(node) {
if (!node) return;
helper(node.left);
helper(node.right);
result.push(node.value);
})(this.root);
return result;
}
BFS() {
if (!this.root) return [];
const result = [];
const queue = [this.root];
let pointer = 0;
while (pointer < queue.length) {
let currentNode = queue[pointer];
result.push(currentNode.value);
if (currentNode.left) queue.push(currentNode.left);
if (currentNode.right) queue.push(currentNode.right);
pointer++;
}
return result;
}
findSecondLargest() {
if (!this.root) return;
let prev;
let current = this.root;
while (current.right) {
prev = current;
current = current.right;
}
return prev ? prev.value : prev;
}
isBalanced() {
// A balanced tree is defined as a tree where the depth of all leaf nodes or nodes
// with single children differ by no more than one
if (!this.root) return true;
let depths = [];
const helper = (node, depth) => {
if (node.left) helper(node.left, depth + 1);
else depths.push(depth);
if (node.right) helper(node.right, depth + 1);
else depths.push(depth);
};
helper(this.root, 0);
const diff = Math.abs(Math.max(...depths) - Math.min(...depths));
return diff >= 0 && diff < 2;
}
}