@@ -82,29 +82,129 @@ class BinarySearchTree {
82
82
}
83
83
return false ;
84
84
}
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
+ }
85
174
}
86
175
87
176
// const bst = new BinarySearchTree(6);
88
177
// 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);
95
184
96
185
// console.log(bst.root);
97
186
98
- // const preorder = bst.preorder(bst.root );
187
+ // const preorder = bst.traversePreorder( );
99
188
// console.log('Preorder Traversal - ', preorder);
100
189
101
- // const inorder = bst.inorder(bst.root );
190
+ // const inorder = bst.traverseInorder( );
102
191
// console.log('Inorder Traversal - ', inorder);
103
192
104
- // const postorder = bst.postorder(bst.root );
193
+ // const postorder = bst.traversePostorder( );
105
194
// console.log('Postorder Traversal - ', postorder);
106
195
107
196
// 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);
109
209
110
210
module . exports = BinarySearchTree ;
0 commit comments