1
+ import Comparator from '../../utils/comparator/Comparator' ;
2
+
1
3
export default class BinaryTreeNode {
4
+ /**
5
+ * @param {* } value
6
+ * @param {BinaryTreeNode } parent
7
+ */
2
8
constructor ( value = null , parent = null ) {
3
9
this . left = null ;
4
10
this . right = null ;
5
11
this . parent = parent ;
6
12
this . value = value ;
13
+
14
+ // This comparator is used to compare binary tree nodes with each other.
15
+ this . nodeComparator = new Comparator ( ) ;
7
16
}
8
17
18
+ /**
19
+ * @return {number }
20
+ */
9
21
get leftHeight ( ) {
10
22
if ( ! this . left ) {
11
23
return 0 ;
@@ -14,6 +26,9 @@ export default class BinaryTreeNode {
14
26
return this . left . height + 1 ;
15
27
}
16
28
29
+ /**
30
+ * @return {number }
31
+ */
17
32
get rightHeight ( ) {
18
33
if ( ! this . right ) {
19
34
return 0 ;
@@ -22,14 +37,24 @@ export default class BinaryTreeNode {
22
37
return this . right . height + 1 ;
23
38
}
24
39
40
+ /**
41
+ * @return {number }
42
+ */
25
43
get height ( ) {
26
44
return Math . max ( this . leftHeight , this . rightHeight ) ;
27
45
}
28
46
47
+ /**
48
+ * @return {number }
49
+ */
29
50
get balanceFactor ( ) {
30
51
return this . leftHeight - this . rightHeight ;
31
52
}
32
53
54
+ /**
55
+ * @param {BinaryTreeNode } node
56
+ * @return {BinaryTreeNode }
57
+ */
33
58
setLeft ( node ) {
34
59
// Reset parent for left node since it is going to be detached.
35
60
if ( this . left ) {
@@ -47,6 +72,10 @@ export default class BinaryTreeNode {
47
72
return this ;
48
73
}
49
74
75
+ /**
76
+ * @param {BinaryTreeNode } node
77
+ * @return {BinaryTreeNode }
78
+ */
50
79
setRight ( node ) {
51
80
// Reset parent for right node since it is going to be detached.
52
81
if ( this . right ) {
@@ -64,38 +93,50 @@ export default class BinaryTreeNode {
64
93
return this ;
65
94
}
66
95
96
+ /**
97
+ * @param {BinaryTreeNode } nodeToRemove
98
+ * @return {boolean }
99
+ */
67
100
removeChild ( nodeToRemove ) {
68
- if ( this . left && this . left === nodeToRemove ) {
101
+ if ( this . left && this . nodeComparator . equal ( this . left , nodeToRemove ) ) {
69
102
this . left = null ;
70
103
return true ;
71
104
}
72
105
73
- if ( this . right && this . right === nodeToRemove ) {
106
+ if ( this . right && this . nodeComparator . equal ( this . right , nodeToRemove ) ) {
74
107
this . right = null ;
75
108
return true ;
76
109
}
77
110
78
111
return false ;
79
112
}
80
113
114
+ /**
115
+ * @param {BinaryTreeNode } nodeToReplace
116
+ * @param {BinaryTreeNode } replacementNode
117
+ * @return {boolean }
118
+ */
81
119
replaceChild ( nodeToReplace , replacementNode ) {
82
120
if ( ! nodeToReplace || ! replacementNode ) {
83
121
return false ;
84
122
}
85
123
86
- if ( this . left && this . left === nodeToReplace ) {
124
+ if ( this . left && this . nodeComparator . equal ( this . left , nodeToReplace ) ) {
87
125
this . left = replacementNode ;
88
126
return true ;
89
127
}
90
128
91
- if ( this . right && this . right === nodeToReplace ) {
129
+ if ( this . right && this . nodeComparator . equal ( this . right , nodeToReplace ) ) {
92
130
this . right = replacementNode ;
93
131
return true ;
94
132
}
95
133
96
134
return false ;
97
135
}
98
136
137
+ /**
138
+ * @return {*[] }
139
+ */
99
140
traverseInOrder ( ) {
100
141
let traverse = [ ] ;
101
142
@@ -115,6 +156,9 @@ export default class BinaryTreeNode {
115
156
return traverse ;
116
157
}
117
158
159
+ /**
160
+ * @return {string }
161
+ */
118
162
toString ( ) {
119
163
return this . traverseInOrder ( ) . toString ( ) ;
120
164
}
0 commit comments