@@ -91,8 +91,6 @@ def insert! node, target_parent = nil, direction = nil
9191 raise ArgumentError , "Target parent already has #{ direction } child" if ( child = target_parent [ direction ] ) && child . valid?
9292 end
9393
94- opp_direction = opposite_direction direction if direction
95-
9694 node . parent = nil
9795 node . left = LeafNode . new
9896 node . left . parent = node
@@ -114,17 +112,16 @@ def insert! node, target_parent = nil, direction = nil
114112 node . parent . parent . red!
115113 node = node . parent . parent
116114 else
117- if node . position == direction
118- node = node . parent
119- rotate_sub_tree! node , opp_direction
115+ opp_direction = node . opposite_position
116+ if node . parent . position == opp_direction
117+ rotate_sub_tree! node . parent , opp_direction
118+ node = node [ opp_direction ]
120119 end
121120
121+ opp_direction = node . opposite_position
122+ rotate_sub_tree! node . parent . parent , opp_direction
122123 node . parent . black!
123-
124- if node . parent . parent
125- node . parent . parent . red!
126- rotate_sub_tree! node . parent . parent , direction
127- end
124+ node . parent [ opp_direction ] . red!
128125 end
129126
130127 @root . black!
@@ -146,6 +143,8 @@ def insert! node, target_parent = nil, direction = nil
146143 def delete! node
147144 raise ArgumentError , "cannot delete leaf node" if node . instance_of? LeafNode
148145
146+ original_node = node
147+
149148 if node . children_are_valid?
150149 successor = node . left
151150 successor = successor . left until successor . left . leaf?
@@ -166,59 +165,54 @@ def delete! node
166165 if is_root? node
167166 @root = nil
168167 elsif node . red?
169- leaf = LeafNode . new
170- node . swap_position_with! leaf
168+ node . swap_position_with! LeafNode . new
171169 else
172- direction = node . position
173- opp_direction = opposite_direction direction
174-
175170 loop do
176- if node . sibling . valid? && node . sibling . red?
171+ if node . sibling && node . sibling . valid? && node . sibling . red?
177172 node . parent . red!
178173 node . sibling . black!
179- rotate_sub_tree! node . parent , direction
180-
181- next
174+ rotate_sub_tree! node . parent , node . position
182175 end
183176
184177 if node . distant_nephew && node . distant_nephew . valid? && node . distant_nephew . red?
185- unless node . sibling . leaf?
186- case node . parent . colour
187- when Node ::RED then node . sibling . red!
188- when Node ::BLACK then node . sibling . black!
189- end
178+ case node . parent . colour
179+ when Node ::RED then node . sibling . red!
180+ when Node ::BLACK then node . sibling . black!
190181 end
191182 node . parent . black!
192183 node . distant_nephew . black!
193- rotate_sub_tree! node . parent , direction
184+ rotate_sub_tree! node . parent , node . position
194185
195186 break
196187 end
197188
198189 if node . close_nephew && node . close_nephew . valid? && node . close_nephew . red?
199190 node . sibling . red! unless node . sibling . leaf?
200191 node . close_nephew . black!
201- rotate_sub_tree! node . sibling , opp_direction
192+ rotate_sub_tree! node . sibling , node . opposite_position
202193
203194 next
204195 end
205196
206- if node . parent . red?
197+ if node . parent && node . parent . red?
207198 node . sibling . red! unless node . sibling . leaf?
208199 node . parent . black!
209200
210201 break
211202 end
212203
213- break
204+ if node . sibling && !node . sibling . leaf?
205+ node . sibling . red!
206+ end
207+
208+ break unless node = node . parent
214209 end
215210
216- leaf = LeafNode . new
217- node . swap_position_with! leaf
211+ original_node . swap_position_with! LeafNode . new
218212 end
219213 end
220214
221- node . validate_free!
215+ original_node . validate_free!
222216
223217 decrement_size!
224218 update_left_most_node!
0 commit comments