@@ -100,102 +100,62 @@ export default class AVLTree<T> extends BinarySearchTree<T> {
100100 protected insertNode ( node : Node < T > , key : T ) {
101101 if ( node == null ) {
102102 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 ) {
104105 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 ) ;
107106 } else {
108- return node ; // duplicated key
107+ node . right = this . insertNode ( node . right , key ) ;
109108 }
110109
111110 // 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 ) {
115113 if ( this . compareFn ( key , node . left . key ) === Compare . LESS_THAN ) {
116114 // Left left case
117115 node = this . rotationLL ( node ) ;
118116 } else {
119117 // Left right case
120- return this . rotationLR ( node ) ;
118+ node = this . rotationLR ( node ) ;
121119 }
122120 }
123-
124- if ( balanceState === BalanceFactor . UNBALANCED_RIGHT ) {
121+ if ( balanceFactor === BalanceFactor . UNBALANCED_RIGHT ) {
125122 if ( this . compareFn ( key , node . right . key ) === Compare . BIGGER_THAN ) {
126123 // Right right case
127124 node = this . rotationRR ( node ) ;
128125 } else {
129126 // Right left case
130- return this . rotationRL ( node ) ;
127+ node = this . rotationRL ( node ) ;
131128 }
132129 }
133-
134130 return node ;
135131 }
136132
137133 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 ;
200160 }
201161}
0 commit comments