Skip to content

Commit fc1f466

Browse files
authored
Merge pull request knaxus#56 from knaxus/problems
delete() & enhancements in BST
2 parents b52001e + 699fd82 commit fc1f466

File tree

1 file changed

+110
-10
lines changed

1 file changed

+110
-10
lines changed

src/_DataStructures_/Trees/BST/index.js

+110-10
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,129 @@ class BinarySearchTree {
8282
}
8383
return false;
8484
}
85+
86+
delete(root, value) {
87+
if (root === null) {
88+
return root;
89+
}
90+
91+
if (value > root.value) {
92+
// eslint-disable-next-line no-param-reassign
93+
root.rightChild = this.delete(root.rightChild, value);
94+
} else if (value < root.value) {
95+
// eslint-disable-next-line no-param-reassign
96+
root.leftChild = this.delete(root.leftChild, value);
97+
} else {
98+
// found the node
99+
if (root.leftChild === null) {
100+
// there is a right sub-tree
101+
return root.rightChild;
102+
}
103+
if (root.rightChild === null) {
104+
// there is a left sub-tree
105+
return root.leftChild;
106+
}
107+
/**
108+
* the root contain 2 childs, we got 2 options:
109+
* 1. We can either find the Node with minimum value at from the right sub-tree
110+
* 2. Or, we can find the Node with maximum value from the left sub-tree
111+
*
112+
* I'm picking up 1 here
113+
*/
114+
const minRightNode = this.findMinNode(root.rightChild);
115+
// eslint-disable-next-line no-param-reassign
116+
root.value = minRightNode.value;
117+
// eslint-disable-next-line no-param-reassign
118+
root.rightChild = this.delete(root.rightChild, minRightNode.value);
119+
return root;
120+
}
121+
return root;
122+
}
123+
124+
findMinNode(root) {
125+
/** The minnimum values is the let most leaf node in BST */
126+
if (root.leftChild === null) return root;
127+
return this.findMinNode(root.leftChild);
128+
}
129+
130+
findMaxNode(root) {
131+
if (root.rightChild === null) return root;
132+
return this.findMaxNode(root.rightChild);
133+
}
134+
135+
isEmpty() {
136+
return this.root === null;
137+
}
138+
139+
/** Layered methods to simplify the BST API */
140+
141+
add(value) {
142+
return this.insert(this.root, value);
143+
}
144+
145+
traversePreorder() {
146+
return this.preorder(this.root);
147+
}
148+
149+
traversePostorder() {
150+
return this.postorder(this.root);
151+
}
152+
153+
traverseInorder() {
154+
return this.inorder(this.root);
155+
}
156+
157+
searchFor(value) {
158+
return this.search(this.root, value);
159+
}
160+
161+
getMinimum() {
162+
const minNode = this.findMinNode(this.root);
163+
return minNode.value;
164+
}
165+
166+
getMaximum() {
167+
const maxNode = this.findMaxNode(this.root);
168+
return maxNode.value;
169+
}
170+
171+
remove(value) {
172+
return this.delete(this.root, value);
173+
}
85174
}
86175

87176
// const bst = new BinarySearchTree(6);
88177
// console.log(bst.root);
89-
// bst.insert(bst.root, 4);
90-
// bst.insert(bst.root, 9);
91-
// bst.insert(bst.root, 2);
92-
// bst.insert(bst.root, 5);
93-
// bst.insert(bst.root, 8);
94-
// bst.insert(bst.root, 12);
178+
// bst.add(4);
179+
// bst.add(9);
180+
// bst.add(2);
181+
// bst.add(5);
182+
// bst.add(8);
183+
// bst.add(12);
95184

96185
// console.log(bst.root);
97186

98-
// const preorder = bst.preorder(bst.root);
187+
// const preorder = bst.traversePreorder();
99188
// console.log('Preorder Traversal - ', preorder);
100189

101-
// const inorder = bst.inorder(bst.root);
190+
// const inorder = bst.traverseInorder();
102191
// console.log('Inorder Traversal - ', inorder);
103192

104-
// const postorder = bst.postorder(bst.root);
193+
// const postorder = bst.traversePostorder();
105194
// console.log('Postorder Traversal - ', postorder);
106195

107196
// const search = 18;
108-
// console.log(`Search for ${search}`, bst.search(bst.root, search));
197+
// console.log(`Search for ${search}`, bst.searchFor(search));
198+
199+
// const minNode = bst.getMinimum();
200+
// console.log('Minimum value =>', minNode);
201+
202+
// const maxNode = bst.getMaximum();
203+
// console.log('Maximum value =>', maxNode);
204+
205+
// bst.remove(4);
206+
// console.log(bst.traversePreorder());
207+
208+
// console.log(bst.root);
109209

110210
module.exports = BinarySearchTree;

0 commit comments

Comments
 (0)