Skip to content

Commit 4d9fe74

Browse files
feat: implement remove method in binary search class
1 parent d6de1de commit 4d9fe74

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import './linked-list/LinkedList';
55
import './doubly-linked-list/DoublyLinkedList';
66
import './stack/StackClass';
77
import './queue/QueueClass';
8+
import './tree/binary-search/BinarySearchTree';

src/tree/binary-search/BinarySearchTree.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,81 @@ export class BinarySearchTree<T> implements BinarySearchTree<T> {
5555
}
5656
return null;
5757
}
58+
59+
public remove(value: T): boolean {
60+
if (!this.root) {
61+
return false;
62+
}
63+
let currentNode = this.root;
64+
let parentNode = null;
65+
while (currentNode) {
66+
if (value < currentNode.value) {
67+
parentNode = currentNode;
68+
currentNode = currentNode.left;
69+
} else if (value > currentNode.value) {
70+
parentNode = currentNode;
71+
currentNode = currentNode.right;
72+
} else if (currentNode.value === value) {
73+
// We have a match, get to work!
74+
75+
// Option 1: No right child:
76+
if (currentNode.right === null) {
77+
if (parentNode === null) {
78+
this.root = currentNode.left;
79+
} else {
80+
// if parent > current value, make current left child a child of parent
81+
if (currentNode.value < parentNode.value) {
82+
parentNode.left = currentNode.left;
83+
84+
// if parent < current value, make left child a right child of parent
85+
} else if (currentNode.value > parentNode.value) {
86+
parentNode.right = currentNode.left;
87+
}
88+
}
89+
90+
// Option 2: Right child which doesnt have a left child
91+
} else if (currentNode.right.left === null) {
92+
currentNode.right.left = currentNode.left;
93+
if (parentNode === null) {
94+
this.root = currentNode.right;
95+
} else {
96+
// if parent > current, make right child of the left the parent
97+
if (currentNode.value < parentNode.value) {
98+
parentNode.left = currentNode.right;
99+
100+
// if parent < current, make right child a right child of the parent
101+
} else if (currentNode.value > parentNode.value) {
102+
parentNode.right = currentNode.right;
103+
}
104+
}
105+
106+
// Option 3: Right child that has a left child
107+
} else {
108+
//find the Right child's left most child
109+
let leftmost = currentNode.right.left;
110+
let leftmostParent = currentNode.right;
111+
while (leftmost.left !== null) {
112+
leftmostParent = leftmost;
113+
leftmost = leftmost.left;
114+
}
115+
116+
// Parent's left subtree is now leftmost's right subtree
117+
leftmostParent.left = leftmost.right;
118+
leftmost.left = currentNode.left;
119+
leftmost.right = currentNode.right;
120+
121+
if (parentNode === null) {
122+
this.root = leftmost;
123+
} else {
124+
if (currentNode.value < parentNode.value) {
125+
parentNode.left = leftmost;
126+
} else if (currentNode.value > parentNode.value) {
127+
parentNode.right = leftmost;
128+
}
129+
}
130+
}
131+
return true;
132+
}
133+
}
134+
}
58135
}

src/tree/binary-search/__tests__/BinarySearchTree.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,23 @@ describe('Binary Search Tree', () => {
4242
expect(bst.lookup(6).value).toBe(6);
4343
expect(bst.lookup(45)).toBeFalsy();
4444
});
45+
46+
test.only('remove nodes from tree', () => {
47+
const bst = new BinarySearchTree<number>();
48+
49+
bst.insert(9);
50+
bst.insert(4);
51+
bst.insert(6);
52+
bst.insert(20);
53+
bst.insert(170);
54+
bst.insert(15);
55+
bst.insert(1);
56+
57+
expect(bst.remove(9)).toBeTruthy();
58+
expect(bst.remove(4)).toBeTruthy();
59+
expect(bst.remove(170)).toBeTruthy();
60+
expect(bst.remove(53)).toBeFalsy();
61+
expect(bst.remove(678)).toBeFalsy();
62+
expect(bst.remove(138)).toBeFalsy();
63+
});
4564
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/* Additional Checks */
2121
"noUnusedLocals": true /* Report errors on unused locals. */,
2222
"noUnusedParameters": true /* Report errors on unused parameters. */,
23-
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
23+
"noImplicitReturns": false /* Report error when not all code paths in function return a value. */,
2424

2525
/* Module Resolution Options */
2626
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,

0 commit comments

Comments
 (0)