(leaf int)
| 448 | } |
| 449 | |
| 450 | func (tree *B2DynamicTree) RemoveLeaf(leaf int) { |
| 451 | if leaf == tree.M_root { |
| 452 | tree.M_root = B2_nullNode |
| 453 | return |
| 454 | } |
| 455 | |
| 456 | parent := tree.M_nodes[leaf].Parent |
| 457 | grandParent := tree.M_nodes[parent].Parent |
| 458 | sibling := 0 |
| 459 | if tree.M_nodes[parent].Child1 == leaf { |
| 460 | sibling = tree.M_nodes[parent].Child2 |
| 461 | } else { |
| 462 | sibling = tree.M_nodes[parent].Child1 |
| 463 | } |
| 464 | |
| 465 | if grandParent != B2_nullNode { |
| 466 | // Destroy parent and connect sibling to grandParent. |
| 467 | if tree.M_nodes[grandParent].Child1 == parent { |
| 468 | tree.M_nodes[grandParent].Child1 = sibling |
| 469 | } else { |
| 470 | tree.M_nodes[grandParent].Child2 = sibling |
| 471 | } |
| 472 | tree.M_nodes[sibling].Parent = grandParent |
| 473 | tree.FreeNode(parent) |
| 474 | |
| 475 | // Adjust ancestor bounds. |
| 476 | index := grandParent |
| 477 | for index != B2_nullNode { |
| 478 | index = tree.Balance(index) |
| 479 | |
| 480 | child1 := tree.M_nodes[index].Child1 |
| 481 | child2 := tree.M_nodes[index].Child2 |
| 482 | |
| 483 | tree.M_nodes[index].Aabb.CombineTwoInPlace(tree.M_nodes[child1].Aabb, tree.M_nodes[child2].Aabb) |
| 484 | tree.M_nodes[index].Height = 1 + MaxInt(tree.M_nodes[child1].Height, tree.M_nodes[child2].Height) |
| 485 | |
| 486 | index = tree.M_nodes[index].Parent |
| 487 | } |
| 488 | } else { |
| 489 | tree.M_root = sibling |
| 490 | tree.M_nodes[sibling].Parent = B2_nullNode |
| 491 | tree.FreeNode(parent) |
| 492 | } |
| 493 | |
| 494 | // //Validate(); |
| 495 | } |
| 496 | |
| 497 | // Perform a left or right rotation if node A is imbalanced. |
| 498 | // Returns the new root index. |
no test coverage detected