@@ -100,102 +100,62 @@ export default class AVLTree<T> extends BinarySearchTree<T> {
100
100
protected insertNode ( node : Node < T > , key : T ) {
101
101
if ( node == null ) {
102
102
return new Node ( key ) ;
103
- } else if ( this . compareFn ( key , node . key ) === Compare . LESS_THAN ) {
103
+ }
104
+ if ( this . compareFn ( key , node . key ) === Compare . LESS_THAN ) {
104
105
node . left = this . insertNode ( node . left , key ) ;
105
- } else if ( this . compareFn ( key , node . key ) === Compare . BIGGER_THAN ) {
106
- node . right = this . insertNode ( node . right , key ) ;
107
106
} else {
108
- return node ; // duplicated key
107
+ node . right = this . insertNode ( node . right , key ) ;
109
108
}
110
109
111
110
// verify if tree is balanced
112
- const balanceState = this . getBalanceFactor ( node ) ;
113
-
114
- if ( balanceState === BalanceFactor . UNBALANCED_LEFT ) {
111
+ const balanceFactor = this . getBalanceFactor ( node ) ;
112
+ if ( balanceFactor === BalanceFactor . UNBALANCED_LEFT ) {
115
113
if ( this . compareFn ( key , node . left . key ) === Compare . LESS_THAN ) {
116
114
// Left left case
117
115
node = this . rotationLL ( node ) ;
118
116
} else {
119
117
// Left right case
120
- return this . rotationLR ( node ) ;
118
+ node = this . rotationLR ( node ) ;
121
119
}
122
120
}
123
-
124
- if ( balanceState === BalanceFactor . UNBALANCED_RIGHT ) {
121
+ if ( balanceFactor === BalanceFactor . UNBALANCED_RIGHT ) {
125
122
if ( this . compareFn ( key , node . right . key ) === Compare . BIGGER_THAN ) {
126
123
// Right right case
127
124
node = this . rotationRR ( node ) ;
128
125
} else {
129
126
// Right left case
130
- return this . rotationRL ( node ) ;
127
+ node = this . rotationRL ( node ) ;
131
128
}
132
129
}
133
-
134
130
return node ;
135
131
}
136
132
137
133
protected removeNode ( node : Node < T > , key : T ) {
138
- if ( node == null ) {
139
- return null ;
140
- }
141
-
142
- if ( this . compareFn ( key , node . key ) === Compare . LESS_THAN ) {
143
- // The key to be deleted is in the left sub-tree
144
- node . left = this . removeNode ( node . left , key ) ;
145
- } else if ( this . compareFn ( key , node . key ) === Compare . BIGGER_THAN ) {
146
- // The key to be deleted is in the right sub-tree
147
- node . right = this . removeNode ( node . right , key ) ;
148
- } else {
149
- // node is the node to be deleted
150
- if ( node . left == null && node . right == null ) {
151
- node = null ;
152
- } else if ( node . left == null && node . right != null ) {
153
- node = node . right ;
154
- } else if ( node . left != null && node . right == null ) {
155
- node = node . left ;
156
- } else {
157
- // node has 2 children, get the in-order successor
158
- const inOrderSuccessor = this . minNode ( node . right ) ;
159
- node . key = inOrderSuccessor . key ;
160
- node . right = this . removeNode ( node . right , inOrderSuccessor . key ) ;
161
- }
162
- }
163
-
164
- if ( node == null ) {
165
- return node ;
166
- }
167
-
168
- // verify if tree is balanced
169
- const balanceState = this . getBalanceFactor ( node ) ;
170
-
171
- if ( balanceState === BalanceFactor . UNBALANCED_LEFT ) {
172
- // Left left case
173
- if (
174
- this . getBalanceFactor ( node . left ) === BalanceFactor . BALANCED ||
175
- this . getBalanceFactor ( node . left ) === BalanceFactor . SLIGHTLY_UNBALANCED_LEFT
176
- ) {
177
- return this . rotationLL ( node ) ;
178
- }
179
- // Left right case
180
- if ( this . getBalanceFactor ( node . left ) === BalanceFactor . SLIGHTLY_UNBALANCED_RIGHT ) {
181
- return this . rotationLR ( node . left ) ;
182
- }
183
- }
184
-
185
- if ( balanceState === BalanceFactor . UNBALANCED_RIGHT ) {
186
- // Right right case
187
- if (
188
- this . getBalanceFactor ( node . right ) === BalanceFactor . BALANCED ||
189
- this . getBalanceFactor ( node . right ) === BalanceFactor . SLIGHTLY_UNBALANCED_RIGHT
190
- ) {
191
- return this . rotationRR ( node ) ;
192
- }
193
- // Right left case
194
- if ( this . getBalanceFactor ( node . right ) === BalanceFactor . SLIGHTLY_UNBALANCED_LEFT ) {
195
- return this . rotationRL ( node . right ) ;
196
- }
197
- }
198
-
199
- return node ;
134
+ node = super . removeNode ( node , key ) ; // {1}
135
+ if ( node == null ) {
136
+ return node ;
137
+ }
138
+
139
+ // verify if tree is balanced
140
+ const balanceFactor = this . getBalanceFactor ( node ) ;
141
+ if ( balanceFactor === BalanceFactor . UNBALANCED_LEFT ) {
142
+ if ( this . compareFn ( key , node . left . key ) === Compare . LESS_THAN ) {
143
+ // Left left case
144
+ node = this . rotationLL ( node ) ;
145
+ } else {
146
+ // Left right case
147
+ node = this . rotationLR ( node ) ;
148
+ }
149
+ }
150
+ if ( balanceFactor === BalanceFactor . UNBALANCED_RIGHT ) {
151
+ if ( this . compareFn ( key , node . right . key ) === Compare . BIGGER_THAN ) {
152
+ // Right right case
153
+ node = this . rotationRR ( node ) ;
154
+ } else {
155
+ // Right left case
156
+ node = this . rotationRL ( node ) ;
157
+ }
158
+ }
159
+ return node ;
200
160
}
201
161
}
0 commit comments