| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | =head1 CONTACT | ||
| 21 | |||
| 22 | Please email comments or questions to the public Ensembl | ||
| 23 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 24 | |||
| 25 | Questions may also be sent to the Ensembl help desk at | ||
| 26 | <http://www.ensembl.org/Help/Contact>. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | Represents a node in the mutable interval tree pure perl implementation. | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | package Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node; | ||
| 43 | $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node::VERSION = '114.0.0_50'; # TRIAL | ||
| 44 | $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node::VERSION = '114.0.050'; | ||
| 45 | use strict; | ||
| 46 | |||
| 47 | use Scalar::Util qw(looks_like_number weaken); | ||
| 48 | use List::Util qw(max); | ||
| 49 | use Bio::EnsEMBL::Utils::Scalar qw(assert_ref); | ||
| 50 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 51 | |||
| 52 | =head1 METHODS | ||
| 53 | |||
| 54 | =head2 new | ||
| 55 | |||
| 56 | Arg [1] : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP | ||
| 57 | The tree to which the node belongs | ||
| 58 | Arg [2] : Bio::EnsEMBL::Utils::Interval | ||
| 59 | Description : Constructor. Creates a new mutable tree instance node | ||
| 60 | associated with the given interval | ||
| 61 | Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node | ||
| 62 | Exceptions : none | ||
| 63 | Caller : general | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub new { | ||
| 68 | my $caller = shift; | ||
| 69 | my $class = ref($caller) || $caller; | ||
| 70 | |||
| 71 | my ($tree, $interval) = @_; | ||
| 72 | throw 'Node constructor takes (tree, interval) as arguments' | ||
| 73 | unless $tree and $interval; | ||
| 74 | |||
| 75 | my $self = bless({ tree => $tree, | ||
| 76 | intervals => [ $interval ], # the array of all records with the same key | ||
| 77 | key => $interval->start, | ||
| 78 | max => $interval->end, | ||
| 79 | parent => undef, | ||
| 80 | height => 0, | ||
| 81 | left => undef, | ||
| 82 | right => undef }, $class); | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 tree | ||
| 88 | |||
| 89 | Arg [] : none | ||
| 90 | Example : my $tree = $node->root; | ||
| 91 | Description : Returns the tree to which the node belongs | ||
| 92 | Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP | ||
| 93 | Exceptions : none | ||
| 94 | Caller : general | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub tree { | ||
| 99 | return shift->{tree}; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 key | ||
| 103 | |||
| 104 | Arg [] : none | ||
| 105 | Example : my $key = $node->key; | ||
| 106 | Description : Returns the key associated with the node | ||
| 107 | Returntype : scalar | ||
| 108 | Exceptions : none | ||
| 109 | Caller : general | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub key { | ||
| 114 | my $self = shift; | ||
| 115 | $self->{key} = shift if( @_ ); | ||
| 116 | |||
| 117 | return $self->{key}; | ||
| 118 | } | ||
| 119 | |||
| 120 | =head2 intervals | ||
| 121 | |||
| 122 | Arg [] : none | ||
| 123 | Example : my $intervals = $node->intervals; | ||
| 124 | Description : Returns the intervals associated with the node | ||
| 125 | Returntype : Arrayref of Bio::EnsEMBL::Utils::Interval | ||
| 126 | Exceptions : none | ||
| 127 | Caller : general | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub intervals { | ||
| 132 | return shift->{intervals}; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head add_interval | ||
| 136 | |||
| 137 | Arg [] : none | ||
| 138 | Description : Add an interval to the node's set of intervals | ||
| 139 | Returntype : none | ||
| 140 | Exceptions : none | ||
| 141 | Caller : general | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub add_interval { | ||
| 146 | push @{shift->{intervals}}, shift; | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 parent | ||
| 150 | |||
| 151 | Arg [] : none | ||
| 152 | Description : Return the parent of the node in the tree | ||
| 153 | Returntype : none | ||
| 154 | Exceptions : none | ||
| 155 | Caller : general | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub parent { | ||
| 160 | my $self = shift; | ||
| 161 | if (@_) { | ||
| 162 | $self->{parent} = shift; | ||
| 163 | weaken($self->{parent}); | ||
| 164 | } | ||
| 165 | |||
| 166 | return $self->{parent}; | ||
| 167 | } | ||
| 168 | |||
| 169 | =head2 height | ||
| 170 | |||
| 171 | Arg [] : none | ||
| 172 | Description : Return the height of the node | ||
| 173 | Returntype : scalar, positive or 0 | ||
| 174 | Exceptions : none | ||
| 175 | Caller : general | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub height { | ||
| 180 | my $self = shift; | ||
| 181 | $self->{height} = shift if( @_ ); | ||
| 182 | |||
| 183 | return $self->{height}; | ||
| 184 | } | ||
| 185 | |||
| 186 | =head2 left | ||
| 187 | |||
| 188 | Arg [] : none | ||
| 189 | Description : Return the node's left child | ||
| 190 | Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node | ||
| 191 | Exceptions : none | ||
| 192 | Caller : general | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub left { | ||
| 197 | my $self = shift; | ||
| 198 | $self->{left} = shift if( @_ ); | ||
| 199 | |||
| 200 | return $self->{left}; | ||
| 201 | } | ||
| 202 | |||
| 203 | =head2 right | ||
| 204 | |||
| 205 | Arg [] : none | ||
| 206 | Description : Return the node's right child | ||
| 207 | Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node | ||
| 208 | Exceptions : none | ||
| 209 | Caller : general | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | sub right { | ||
| 214 | my $self = shift; | ||
| 215 | $self->{right} = shift if( @_ ); | ||
| 216 | |||
| 217 | return $self->{right}; | ||
| 218 | } | ||
| 219 | |||
| 220 | =head2 search | ||
| 221 | |||
| 222 | Arg [1] : Bio::EnsEMBL::Utils::Interval | ||
| 223 | The interval to search for overlaps in the tree | ||
| 224 | Description : Search the node and its successors for overlapping | ||
| 225 | intervals with the query | ||
| 226 | Returntype : Arrayref of Bio::EnsEMBL::Utils::Interval | ||
| 227 | Exceptions : none | ||
| 228 | Caller : general | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub search { | ||
| 233 | my ($self, $i) = @_; | ||
| 234 | |||
| 235 | # if interval is to the right of the rightmost point of any interval in this node and | ||
| 236 | # all its children, there won't be any matches | ||
| 237 | return [] if $i->start > $self->{max}; | ||
| 238 | |||
| 239 | my $results = []; | ||
| 240 | |||
| 241 | # search left subtree | ||
| 242 | if ($self->left and $self->left->{max} >= $i->start) { | ||
| 243 | push @{$results}, @{$self->left->search($i)}; | ||
| 244 | } | ||
| 245 | |||
| 246 | # search this node | ||
| 247 | push @{$results}, @{$self->_overlapping_intervals($i)}; | ||
| 248 | |||
| 249 | # if interval is to the left of the start of this interval, then | ||
| 250 | # it can't be in any child to the right | ||
| 251 | return $results if $i->end < $self->key; | ||
| 252 | |||
| 253 | # search right subtree | ||
| 254 | push @{$results}, @{$self->right->search($i)} if $self->right; | ||
| 255 | |||
| 256 | return $results; | ||
| 257 | } | ||
| 258 | |||
| 259 | =head2 search_by_key | ||
| 260 | |||
| 261 | Arg [1] : scalar, $key | ||
| 262 | The key to search the node for | ||
| 263 | Description : Searches for a node by a 'key' value | ||
| 264 | Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node | ||
| 265 | Exceptions : none | ||
| 266 | Caller : general | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub search_by_key { | ||
| 271 | my ($self, $key) = @_; | ||
| 272 | |||
| 273 | if ($self->key == $key) { | ||
| 274 | return $self; | ||
| 275 | } elsif ($key < $self->key) { | ||
| 276 | return $self->left->search_by_key($key) if $self->left; | ||
| 277 | } else { | ||
| 278 | return $self->right->search_by_key($key) if $self->right; | ||
| 279 | } | ||
| 280 | |||
| 281 | } | ||
| 282 | |||
| 283 | =head2 insert | ||
| 284 | |||
| 285 | Arg [1] : Bio::EnsEMBL::Utils::Interval | ||
| 286 | The interval to insert | ||
| 287 | Description : Insert an interval in the node or in its successors | ||
| 288 | Returntype : none | ||
| 289 | Exceptions : none | ||
| 290 | Caller : general | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub insert { | ||
| 295 | my ($self, $i) = @_; | ||
| 296 | |||
| 297 | if ($i->start < $self->key) { | ||
| 298 | # insert into left subtree | ||
| 299 | unless (defined $self->left) { | ||
| 300 | $self->left(Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node->new($self->tree, $i)); | ||
| 301 | $self->left->parent($self); | ||
| 302 | } else { | ||
| 303 | $self->left->insert($i); | ||
| 304 | } | ||
| 305 | } else { | ||
| 306 | # insert into right subtree | ||
| 307 | unless (defined $self->right) { | ||
| 308 | $self->right(Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node->new($self->tree, $i)); | ||
| 309 | $self->right->parent($self); | ||
| 310 | } else { | ||
| 311 | $self->right->insert($i); | ||
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | # update max value if needed | ||
| 316 | $self->{max} = $i->end if $self->{max} < $i->end; | ||
| 317 | |||
| 318 | # update node's height | ||
| 319 | $self->_update_height; | ||
| 320 | |||
| 321 | # rebalance to ensure O(logn) time operations | ||
| 322 | $self->_rebalance; | ||
| 323 | } | ||
| 324 | |||
| 325 | =head2 remove | ||
| 326 | |||
| 327 | Arg [1] : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node | ||
| 328 | The node to remove from the tree | ||
| 329 | Description : Remove a node from the tree | ||
| 330 | Returntype : none | ||
| 331 | Exceptions : none | ||
| 332 | Caller : general | ||
| 333 | |||
| 334 | =cut | ||
| 335 | |||
| 336 | sub remove { | ||
| 337 | my ($self, $node) = @_; | ||
| 338 | |||
| 339 | return unless $node; | ||
| 340 | |||
| 341 | my $parent = $self->parent; | ||
| 342 | |||
| 343 | if ($node->key < $self->key) { | ||
| 344 | # node to be removed is in left subtree | ||
| 345 | return $self->left->remove($node) if $self->left; | ||
| 346 | return; | ||
| 347 | } elsif ($node->key > $self->key) { | ||
| 348 | # node to be removed is in right subtree | ||
| 349 | return $self->right->remove($node) if $self->right; | ||
| 350 | return; | ||
| 351 | } else { | ||
| 352 | if ($self->left and $self->right) { | ||
| 353 | # node has two children | ||
| 354 | my $lowest = $self->right->_lowest; | ||
| 355 | $self->key($lowest->key); | ||
| 356 | $self->{intervals} = $lowest->{intervals}; | ||
| 357 | return $self->right->remove($self); | ||
| 358 | } elsif ($parent->left == $self) { | ||
| 359 | # one child or no child case on left side | ||
| 360 | if ($self->right) { | ||
| 361 | $parent->left($self->right); | ||
| 362 | $self->right->parent($parent); | ||
| 363 | } else { | ||
| 364 | $parent->left($self->left); | ||
| 365 | $self->left->parent($parent) if $self->left | ||
| 366 | } | ||
| 367 | $parent->_update_parents_max; | ||
| 368 | $parent->_update_height; | ||
| 369 | $parent->_rebalance; | ||
| 370 | |||
| 371 | return $self; | ||
| 372 | |||
| 373 | } elsif ($parent->right == $self) { | ||
| 374 | # one child or no child case on right side | ||
| 375 | if ($self->right) { | ||
| 376 | $parent->right($self->right); | ||
| 377 | $self->right->parent($parent); | ||
| 378 | } else { | ||
| 379 | $parent->right($self->left); | ||
| 380 | $self->left->parent($parent) if $self->left; | ||
| 381 | } | ||
| 382 | $parent->_update_parents_max; | ||
| 383 | $parent->_update_height; | ||
| 384 | $parent->_rebalance; | ||
| 385 | |||
| 386 | return $self; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | =head1 PRIVATE METHODS | ||
| 392 | |||
| 393 | =head2 _height | ||
| 394 | |||
| 395 | Not a method since code could invoke method on undefined instances, e.g. _rebalance | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | sub _height { | ||
| 400 | my $node = shift; | ||
| 401 | |||
| 402 | return -1 unless $node; | ||
| 403 | return $node->height; | ||
| 404 | } | ||
| 405 | |||
| 406 | =head2 _lowest | ||
| 407 | |||
| 408 | Returns the 'smallest' node in the tree | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | sub _lowest { | ||
| 413 | my $self = shift; | ||
| 414 | |||
| 415 | return $self unless $self->left; | ||
| 416 | return $self->left->_lowest; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub _highest_end { | ||
| 420 | my $self = shift; | ||
| 421 | |||
| 422 | my $high = $self->{intervals}[0]->end; | ||
| 423 | map { $high = $_->end if $high < $_->end } @{$self->{intervals}}; | ||
| 424 | |||
| 425 | return $high; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub _update_height { | ||
| 429 | my $self = shift; | ||
| 430 | |||
| 431 | $self->height(List::Util::max $self->left?$self->left->height:0, $self->right?$self->right->height:0 + 1); | ||
| 432 | } | ||
| 433 | |||
| 434 | sub _update_parents_max { | ||
| 435 | my $self = shift; | ||
| 436 | # updates the max value of all the parents after inserting into already existing node, as well as | ||
| 437 | # removing the node completely or removing the record of an already existing node. Starts with | ||
| 438 | # the parent of an affected node and bubbles up to root | ||
| 439 | my $high = $self->_highest_end; | ||
| 440 | if ($self->left and $self->right) { | ||
| 441 | $self->{max} = max $self->left->{max}, $self->right-{max}, $high; | ||
| 442 | } elsif ($self->left and !$self->right) { | ||
| 443 | $self->{max} = max $self->left->{max}, $high; | ||
| 444 | } elsif (!$self->left and $self->right) { | ||
| 445 | $self->{max} = max $self->right->{max}, $high; | ||
| 446 | } else { | ||
| 447 | $self->{max} = $high; | ||
| 448 | } | ||
| 449 | |||
| 450 | $self->parent->_update_parents_max if $self->parent; | ||
| 451 | } | ||
| 452 | |||
| 453 | =head2 _rebalance | ||
| 454 | |||
| 455 | Rebalances the tree if the height value between two nodes of the same parent is greater than two. | ||
| 456 | There are 4 cases that can happen: | ||
| 457 | |||
| 458 | Left-Left case: | ||
| 459 | z y | ||
| 460 | / \ / \ | ||
| 461 | y T4 Right Rotate (z) x z | ||
| 462 | / \ - - - - - - - - -> / \ / \ | ||
| 463 | x T3 T1 T2 T3 T4 | ||
| 464 | / \ | ||
| 465 | T1 T2 | ||
| 466 | |||
| 467 | Left-Right case: | ||
| 468 | z z x | ||
| 469 | / \ / \ / \ | ||
| 470 | y T4 Left Rotate (y) x T4 Right Rotate(z) y z | ||
| 471 | / \ - - - - - - - - -> / \ - - - - - - - -> / \ / \ | ||
| 472 | T1 x y T3 T1 T2 T3 T4 | ||
| 473 | / \ / \ | ||
| 474 | T2 T3 T1 T2 | ||
| 475 | |||
| 476 | Right-Right case: | ||
| 477 | z y | ||
| 478 | / \ / \ | ||
| 479 | T1 y Left Rotate(z) z x | ||
| 480 | / \ - - - - - - - -> / \ / \ | ||
| 481 | T2 x T1 T2 T3 T4 | ||
| 482 | / \ | ||
| 483 | T3 T4 | ||
| 484 | |||
| 485 | Right-Left case: | ||
| 486 | z z x | ||
| 487 | / \ / \ / \ | ||
| 488 | T1 y Right Rotate (y) T1 x Left Rotate(z) z y | ||
| 489 | / \ - - - - - - - - -> / \ - - - - - - - -> / \ / \ | ||
| 490 | x T4 T2 y T1 T2 T3 T4 | ||
| 491 | / \ / \ | ||
| 492 | T2 T3 T3 T4 | ||
| 493 | |||
| 494 | =cut | ||
| 495 | |||
| 496 | sub _rebalance { | ||
| 497 | my $self = shift; | ||
| 498 | |||
| 499 | my ($left, $right) = ($self->left, $self->right); | ||
| 500 | |||
| 501 | if (_height($left) - _height($right) >= 2) { | ||
| 502 | if (_height($left->left) >= _height($left->right)) { | ||
| 503 | # Left-Left case | ||
| 504 | $self->_right_rotate; | ||
| 505 | $self->_update_max_right_rotate; | ||
| 506 | } else { | ||
| 507 | # Left-Right case | ||
| 508 | $left->_left_rotate; | ||
| 509 | $self->_right_rotate; | ||
| 510 | $self->_update_max_right_rotate; | ||
| 511 | } | ||
| 512 | } elsif (_height($right) - _height($left) >= 2) { | ||
| 513 | if (_height($right->right) >= _height($right->left)) { | ||
| 514 | # Right-Right case | ||
| 515 | $self->_left_rotate; | ||
| 516 | $self->_update_max_left_rotate; | ||
| 517 | } else { | ||
| 518 | # Right-Left case | ||
| 519 | $right->_right_rotate; | ||
| 520 | $self->_left_rotate; | ||
| 521 | $self->_update_max_left_rotate; | ||
| 522 | } | ||
| 523 | } | ||
| 524 | } | ||
| 525 | |||
| 526 | sub _left_rotate { | ||
| 527 | my $self = shift; | ||
| 528 | |||
| 529 | my $right = $self->right; | ||
| 530 | |||
| 531 | $right->parent($self->parent); | ||
| 532 | unless (defined $right->parent) { | ||
| 533 | $self->tree->root($right); | ||
| 534 | } else { | ||
| 535 | if ($right->parent->left == $self) { | ||
| 536 | $right->parent->left($right); | ||
| 537 | } elsif ($right->parent->right == $self) { | ||
| 538 | $right->parent->right($right); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | $self->right($right->left); | ||
| 543 | $self->right->parent($self) if $self->right; | ||
| 544 | |||
| 545 | $right->left($self); | ||
| 546 | $self->parent($right); | ||
| 547 | |||
| 548 | $self->_update_height; | ||
| 549 | $right->_update_height; | ||
| 550 | |||
| 551 | } | ||
| 552 | |||
| 553 | sub _update_max_left_rotate { # handles Right-Right case and Right-Left case in rebalancing AVL tree | ||
| 554 | my $self = shift; | ||
| 555 | |||
| 556 | # update max of left sibling (x in first case, y in second) | ||
| 557 | my $parent = $self->parent; | ||
| 558 | my $parent_right = $parent->right; | ||
| 559 | if ($parent_right) { | ||
| 560 | my $parent_right_high = $parent_right->_highest_end; | ||
| 561 | if (!$parent_right->left and $parent_right->right) { | ||
| 562 | $parent_right->{max} = max $parent_right_high, $parent_right->right->{max}; | ||
| 563 | } elsif ($parent_right->left and !$parent_right->right) { | ||
| 564 | $parent_right->{max} = max $parent_right_high, $parent_right->left->{max}; | ||
| 565 | } elsif (!$parent_right->left and !$parent_right->right) { | ||
| 566 | $parent_right->{max} = $parent_right_high; | ||
| 567 | } else { | ||
| 568 | $parent_right->{max} = max $parent_right_high, $parent_right->left->{max}, $parent_right->right->{max}; | ||
| 569 | } | ||
| 570 | } | ||
| 571 | |||
| 572 | # update max of itself (z) | ||
| 573 | my $high = $self->_highest_end; | ||
| 574 | if (!$self->left and $self->right) { | ||
| 575 | $self->{max} = max $high, $self->right->{max}; | ||
| 576 | } elsif ($self->left and !$self->right) { | ||
| 577 | $self->{max} = max $high, $self->left->{max}; | ||
| 578 | } elsif (!$self->left and !$self->right) { | ||
| 579 | $self->{max} = $high; | ||
| 580 | } else { | ||
| 581 | $self->{max} = max $high, $self->left->{max}, $self->right->{max}; | ||
| 582 | } | ||
| 583 | |||
| 584 | # update max of parent (y in first case, x in second) | ||
| 585 | $parent->{max} = max $parent->left?$parent->left->{max}:0, $parent->right?$parent->right->{max}:0, $parent->_highest_end | ||
| 586 | if $parent; | ||
| 587 | |||
| 588 | } | ||
| 589 | |||
| 590 | sub _right_rotate { | ||
| 591 | my $self = shift; | ||
| 592 | |||
| 593 | my $left = $self->left; | ||
| 594 | |||
| 595 | $left->parent($self->parent); | ||
| 596 | unless (defined $left->parent) { | ||
| 597 | $self->tree->root($left); | ||
| 598 | } else { | ||
| 599 | if ($left->parent->left == $self) { | ||
| 600 | $left->parent->left($left); | ||
| 601 | } elsif ($left->parent->right == $self) { | ||
| 602 | $left->parent->right($left); | ||
| 603 | } | ||
| 604 | } | ||
| 605 | |||
| 606 | $self->left($left->right); | ||
| 607 | $self->left->parent($self) if $self->left; | ||
| 608 | |||
| 609 | $left->right($self); | ||
| 610 | $self->parent($left); | ||
| 611 | |||
| 612 | $self->_update_height; | ||
| 613 | $left->_update_height; | ||
| 614 | } | ||
| 615 | |||
| 616 | sub _update_max_right_rotate { # handles Left-Left case and Left-Right case after rebalancing AVL tree | ||
| 617 | my $self = shift; | ||
| 618 | |||
| 619 | # update max of left sibling (x in first case, y in second) | ||
| 620 | my $parent = $self->parent; | ||
| 621 | my $parent_left = $parent->left; | ||
| 622 | if ($parent_left) { | ||
| 623 | my $parent_left_high = $parent_left->_highest_end; | ||
| 624 | if (!$parent_left->left and $parent_left->right) { | ||
| 625 | $parent_left->{max} = max $parent_left_high, $parent_left->right->{max}; | ||
| 626 | } elsif ($parent_left->left and !$parent_left->right) { | ||
| 627 | $parent_left->{max} = max $parent_left_high, $parent_left->left->{max}; | ||
| 628 | } elsif (!$parent_left->left and !$parent_left->right) { | ||
| 629 | $parent_left->{max} = $parent_left_high; | ||
| 630 | } else { | ||
| 631 | $parent_left->{max} = max $parent_left_high, $parent_left->left->{max}, $parent_left->right->{max}; | ||
| 632 | } | ||
| 633 | } | ||
| 634 | |||
| 635 | # update max of itself (z) | ||
| 636 | my $high = $self->_highest_end; | ||
| 637 | if (!$self->left and $self->right) { | ||
| 638 | $self->{max} = max $high, $self->right->{max}; | ||
| 639 | } elsif ($self->left and !$self->right) { | ||
| 640 | $self->{max} = max $high, $self->left->{max}; | ||
| 641 | } elsif (!$self->left and !$self->right) { | ||
| 642 | $self->{max} = $high; | ||
| 643 | } else { | ||
| 644 | $self->{max} = max $high, $self->left->{max}, $self->right->{max}; | ||
| 645 | } | ||
| 646 | |||
| 647 | # update max of parent (y in first case, x in second) | ||
| 648 | $parent->{max} = max $parent->left?$parent->left->{max}:0, $parent->right?$parent->right->{max}:0, $parent->_highest_end | ||
| 649 | if $parent; | ||
| 650 | } | ||
| 651 | |||
| 652 | sub _overlapping_intervals { | ||
| 653 | my ($self, $i) = @_; | ||
| 654 | |||
| 655 | my $results = []; | ||
| 656 | if ($self->key <= $i->end and $i->start <= $self->_highest_end) { | ||
| 657 | # node's interval overlap: check individual intervals | ||
| 658 | map { push @{$results}, $_ if $i->start <= $_->end } @{$self->{intervals}} | ||
| 659 | } | ||
| 660 | |||
| 661 | return $results; | ||
| 662 | } | ||
| 663 | |||
| 664 | 1; | ||
| 665 |