Returns self after removing interval and balancing. If interval doesn't exist, raise ValueError. This method may set done to [1] to tell all callers that rebalancing has completed. See Eternally Confuzzled's jsw_remove_r function (lines 1-32) in his
(self, interval, done, should_raise_error)
| 240 | return self.remove_interval_helper(interval, done, should_raise_error=False) |
| 241 | |
| 242 | def remove_interval_helper(self, interval, done, should_raise_error): |
| 243 | """ |
| 244 | Returns self after removing interval and balancing. |
| 245 | If interval doesn't exist, raise ValueError. |
| 246 | |
| 247 | This method may set done to [1] to tell all callers that |
| 248 | rebalancing has completed. |
| 249 | |
| 250 | See Eternally Confuzzled's jsw_remove_r function (lines 1-32) |
| 251 | in his AVL tree article for reference. |
| 252 | """ |
| 253 | #trace = interval.begin == 347 and interval.end == 353 |
| 254 | #if trace: print('\nRemoving from {} interval {}'.format( |
| 255 | # self.x_center, interval)) |
| 256 | if self.center_hit(interval): |
| 257 | #if trace: print('Hit at {}'.format(self.x_center)) |
| 258 | if not should_raise_error and interval not in self.s_center: |
| 259 | done.append(1) |
| 260 | #if trace: print('Doing nothing.') |
| 261 | return self |
| 262 | try: |
| 263 | # raises error if interval not present - this is |
| 264 | # desired. |
| 265 | self.s_center.remove(interval) |
| 266 | except: |
| 267 | self.print_structure() |
| 268 | raise KeyError(interval) |
| 269 | if self.s_center: # keep this node |
| 270 | done.append(1) # no rebalancing necessary |
| 271 | #if trace: print('Removed, no rebalancing.') |
| 272 | return self |
| 273 | |
| 274 | # If we reach here, no intervals are left in self.s_center. |
| 275 | # So, prune self. |
| 276 | return self.prune() |
| 277 | else: # interval not in s_center |
| 278 | direction = self.hit_branch(interval) |
| 279 | |
| 280 | if not self[direction]: |
| 281 | if should_raise_error: |
| 282 | raise ValueError |
| 283 | done.append(1) |
| 284 | return self |
| 285 | |
| 286 | #if trace: |
| 287 | # print('Descending to {} branch'.format( |
| 288 | # ['left', 'right'][direction] |
| 289 | # )) |
| 290 | self[direction] = self[direction].remove_interval_helper(interval, done, should_raise_error) |
| 291 | |
| 292 | # Clean up |
| 293 | if not done: |
| 294 | #if trace: |
| 295 | # print('Rotating {}'.format(self.x_center)) |
| 296 | # self.print_structure() |
| 297 | return self.rotate() |
| 298 | return self |
| 299 |
no test coverage detected