@@ -54,6 +54,30 @@ class BinarySearchTree {
5454 return undefined ;
5555 }
5656
57+ /**
58+ * Get the node with the max value of subtree: the right-most value.
59+ * @param {TreeNode } root subtree's root
60+ */
61+ getMax ( root = this . root ) {
62+ let current = root ;
63+ while ( current && current . right ) {
64+ current = current . right ;
65+ }
66+ return current ;
67+ }
68+
69+ /**
70+ * Get the node with the min value of subtree: the left-most value.
71+ * @param {TreeNode } root subtree's root
72+ */
73+ getMin ( root = this . root ) {
74+ let current = root ;
75+ while ( current && current . left ) {
76+ current = current . left ;
77+ }
78+ return current ;
79+ }
80+
5781 /**
5882 * Remove a node from the tree
5983 * @returns {boolean } false if not found and true if it was deleted
@@ -65,20 +89,24 @@ class BinarySearchTree {
6589 const { parent } = found ;
6690
6791 if ( found === this . root ) {
92+ // removing root
6893 this . root = found . right || found . left ; // replace root
6994 this . root . parent = null ;
70- // if the root had any left subtree, place it at the end of the new root subtree.
95+ // if the root had any left subtree,
96+ // place it at the end of the new root subtree.
7197 if ( found . left ) {
7298 const newRootLeftmost = this . getMin ( this . root . left ) ;
7399 newRootLeftmost . left = found . left ;
74100 }
75101 } else if ( parent . left === found ) {
102+ // removing node on the left
76103 parent . left = found . right || found . left ;
77104
78105 if ( found . left ) {
79106 parent . left . left = found . left ;
80107 }
81108 } else if ( parent . right === found ) {
109+ // removing node on the right
82110 parent . right = found . right || found . left ;
83111
84112 if ( found . left ) {
@@ -90,50 +118,6 @@ class BinarySearchTree {
90118 return true ;
91119 }
92120
93- /**
94- * Represent Binary Tree as an array.
95- *
96- * Leaf nodes will have two `undefined` descendents.
97- *
98- * The array representation of the binary tree is as follows:
99- *
100- * First element (index=0) is the root.
101- * The following two elements (index=1,2) are descendents of the root: left (a) and right (b).
102- * The next two elements (index=3,4) are the descendents of a
103- * The next two elements (index=5,6) are the descendents of b and so on.
104- *
105- * 0 1 2 3 4 5 6 n
106- * [root, a=root.left, b=root.right, a.left, a.right, b.left, b.right, ...]
107- *
108- * You can also find the parents as follows
109- *
110- * e.g.
111- * Parent 0: children 1,2
112- * Parent 1: children 3,4
113- * Parent 2: children 5,6
114- * Parent 3: children 7,8
115- *
116- * Given any index you can find the parent index with the following formula:
117- *
118- * parent = (index) => Math.floor((index-1)/2)
119- */
120- toArray ( ) {
121- const array = [ ] ;
122- const queue = new Queue ( ) ;
123-
124- if ( this . root ) { queue . add ( this . root ) ; }
125-
126- while ( ! queue . isEmpty ( ) ) {
127- const current = queue . remove ( ) ;
128- array . push ( current && current . value ) ;
129-
130- if ( current ) { queue . add ( current . left ) ; }
131- if ( current ) { queue . add ( current . right ) ; }
132- }
133-
134- return array ;
135- }
136-
137121 /**
138122 * Breath-first search for a tree (always starting from the root element).
139123 *
@@ -206,27 +190,47 @@ class BinarySearchTree {
206190 }
207191
208192 /**
209- * Get the node with the max value of subtree: the right-most value.
210- * @param {TreeNode } root subtree's root
193+ * Represent Binary Tree as an array.
194+ *
195+ * Leaf nodes will have two `undefined` descendents.
196+ *
197+ * The array representation of the binary tree is as follows:
198+ *
199+ * First element (index=0) is the root.
200+ * The following two elements (index=1,2) are descendents of the root: left (a) and right (b).
201+ * The next two elements (index=3,4) are the descendents of a
202+ * The next two elements (index=5,6) are the descendents of b and so on.
203+ *
204+ * 0 1 2 3 4 5 6 n
205+ * [root, a=root.left, b=root.right, a.left, a.right, b.left, b.right, ...]
206+ *
207+ * You can also find the parents as follows
208+ *
209+ * e.g.
210+ * Parent 0: children 1,2
211+ * Parent 1: children 3,4
212+ * Parent 2: children 5,6
213+ * Parent 3: children 7,8
214+ *
215+ * Given any index you can find the parent index with the following formula:
216+ *
217+ * parent = (index) => Math.floor((index-1)/2)
211218 */
212- getMax ( root = this . root ) {
213- let current = root ;
214- while ( current && current . right ) {
215- current = current . right ;
216- }
217- return current ;
218- }
219+ toArray ( ) {
220+ const array = [ ] ;
221+ const queue = new Queue ( ) ;
219222
220- /**
221- * Get the node with the min value of subtree: the left-most value.
222- * @param { TreeNode } root subtree's root
223- */
224- getMin ( root = this . root ) {
225- let current = root ;
226- while ( current && current . left ) {
227- current = current . left ;
223+ if ( this . root ) { queue . add ( this . root ) ; }
224+
225+ while ( ! queue . isEmpty ( ) ) {
226+ const current = queue . remove ( ) ;
227+ array . push ( current && current . value ) ;
228+
229+ if ( current ) { queue . add ( current . left ) ; }
230+ if ( current ) { queue . add ( current . right ) ; }
228231 }
229- return current ;
232+
233+ return array ;
230234 }
231235}
232236
0 commit comments