Пример #1
0
 function testRemoveWhereInOrderPredecessorHasChild()
 {
     $object = new AvlTree();
     //          5
     //        /    \
     //       2      9
     //     /  \    / \
     //    1    4  8  11
     //        /
     //       3
     $object->add(5);
     $object->add(2);
     $object->add(9);
     $object->add(1);
     $object->add(4);
     $object->add(8);
     $object->add(11);
     $object->add(3);
     // build a test tree to validate that we are set up correctly
     $expectedRoot = new BinaryTree(5);
     $expectedRoot->setLeft(new BinaryTree(2));
     $expectedRoot->left()->setLeft(new BinaryTree(1));
     $expectedRoot->left()->setRight(new BinaryTree(4));
     $expectedRoot->left()->right()->setLeft(new BinaryTree(3));
     $expectedRoot->setRight(new BinaryTree(9));
     $expectedRoot->right()->setLeft(new BinaryTree(8));
     $expectedRoot->right()->setRight(new BinaryTree(11));
     $this->reCalculateHeights($expectedRoot);
     $actualRoot = $object->toBinaryTree();
     $this->assertEquals($expectedRoot, $actualRoot);
     // okay, now for the real test:
     $object->remove(5);
     //          4
     //        /    \
     //       2      9
     //     /  \    / \
     //    1    3  8  11
     $expectedRoot = new BinaryTree(4);
     $expectedRoot->setLeft(new BinaryTree(2));
     $expectedRoot->left()->setLeft(new BinaryTree(1));
     $expectedRoot->left()->setRight(new BinaryTree(3));
     $expectedRoot->setRight(new BinaryTree(9));
     $expectedRoot->right()->setLeft(new BinaryTree(8));
     $expectedRoot->right()->setRight(new BinaryTree(11));
     $this->reCalculateHeights($expectedRoot);
     $actualRoot = $object->toBinaryTree();
     $this->assertEquals($expectedRoot, $actualRoot);
 }