1+ import Comparator from '../../utils/comparator/Comparator' ;
2+
13export default class BinaryTreeNode {
4+ /**
5+ * @param {* } value
6+ * @param {BinaryTreeNode } parent
7+ */
28 constructor ( value = null , parent = null ) {
39 this . left = null ;
410 this . right = null ;
511 this . parent = parent ;
612 this . value = value ;
13+
14+ // This comparator is used to compare binary tree nodes with each other.
15+ this . nodeComparator = new Comparator ( ) ;
716 }
817
18+ /**
19+ * @return {number }
20+ */
921 get leftHeight ( ) {
1022 if ( ! this . left ) {
1123 return 0 ;
@@ -14,6 +26,9 @@ export default class BinaryTreeNode {
1426 return this . left . height + 1 ;
1527 }
1628
29+ /**
30+ * @return {number }
31+ */
1732 get rightHeight ( ) {
1833 if ( ! this . right ) {
1934 return 0 ;
@@ -22,14 +37,24 @@ export default class BinaryTreeNode {
2237 return this . right . height + 1 ;
2338 }
2439
40+ /**
41+ * @return {number }
42+ */
2543 get height ( ) {
2644 return Math . max ( this . leftHeight , this . rightHeight ) ;
2745 }
2846
47+ /**
48+ * @return {number }
49+ */
2950 get balanceFactor ( ) {
3051 return this . leftHeight - this . rightHeight ;
3152 }
3253
54+ /**
55+ * @param {BinaryTreeNode } node
56+ * @return {BinaryTreeNode }
57+ */
3358 setLeft ( node ) {
3459 // Reset parent for left node since it is going to be detached.
3560 if ( this . left ) {
@@ -47,6 +72,10 @@ export default class BinaryTreeNode {
4772 return this ;
4873 }
4974
75+ /**
76+ * @param {BinaryTreeNode } node
77+ * @return {BinaryTreeNode }
78+ */
5079 setRight ( node ) {
5180 // Reset parent for right node since it is going to be detached.
5281 if ( this . right ) {
@@ -64,38 +93,50 @@ export default class BinaryTreeNode {
6493 return this ;
6594 }
6695
96+ /**
97+ * @param {BinaryTreeNode } nodeToRemove
98+ * @return {boolean }
99+ */
67100 removeChild ( nodeToRemove ) {
68- if ( this . left && this . left === nodeToRemove ) {
101+ if ( this . left && this . nodeComparator . equal ( this . left , nodeToRemove ) ) {
69102 this . left = null ;
70103 return true ;
71104 }
72105
73- if ( this . right && this . right === nodeToRemove ) {
106+ if ( this . right && this . nodeComparator . equal ( this . right , nodeToRemove ) ) {
74107 this . right = null ;
75108 return true ;
76109 }
77110
78111 return false ;
79112 }
80113
114+ /**
115+ * @param {BinaryTreeNode } nodeToReplace
116+ * @param {BinaryTreeNode } replacementNode
117+ * @return {boolean }
118+ */
81119 replaceChild ( nodeToReplace , replacementNode ) {
82120 if ( ! nodeToReplace || ! replacementNode ) {
83121 return false ;
84122 }
85123
86- if ( this . left && this . left === nodeToReplace ) {
124+ if ( this . left && this . nodeComparator . equal ( this . left , nodeToReplace ) ) {
87125 this . left = replacementNode ;
88126 return true ;
89127 }
90128
91- if ( this . right && this . right === nodeToReplace ) {
129+ if ( this . right && this . nodeComparator . equal ( this . right , nodeToReplace ) ) {
92130 this . right = replacementNode ;
93131 return true ;
94132 }
95133
96134 return false ;
97135 }
98136
137+ /**
138+ * @return {*[] }
139+ */
99140 traverseInOrder ( ) {
100141 let traverse = [ ] ;
101142
@@ -115,6 +156,9 @@ export default class BinaryTreeNode {
115156 return traverse ;
116157 }
117158
159+ /**
160+ * @return {string }
161+ */
118162 toString ( ) {
119163 return this . traverseInOrder ( ) . toString ( ) ;
120164 }
0 commit comments