예제 #1
0
 function test_current_badInnerIterator_throwsException()
 {
     $this->setExpectedException('\\Collections\\TypeException');
     $tree = new BinaryTree(0);
     $tree->setLeft(new BinaryTree(-1));
     $tree->setRight(new BinaryTree(1));
     $iterator = new SortedMapIterator(new InOrderIterator($tree, 0), 3);
     $iterator->current();
 }
예제 #2
0
 function test_toArray()
 {
     $tree = new BinaryTree(0);
     $tree->setLeft(new BinaryTree(-4));
     $tree->left()->setLeft(new BinaryTree(1));
     $tree->left()->setRight(new BinaryTree(2));
     $tree->setRight(new BinaryTree(4));
     $iterator = $this->instance($tree, 5);
     $expect = [0, -4, 4, 1, 2];
     $actual = iterator_to_array($iterator);
     $this->assertEquals($expect, $actual);
 }
예제 #3
0
 /**
  * @param BinaryTree $root
  *
  * @return BinaryTree
  */
 protected function rotateLeft(BinaryTree $root)
 {
     $rightNode = $root->right();
     $diff = $rightNode->leftHeight() - $rightNode->rightHeight();
     if ($diff >= 0) {
         // Right-Left case
         $pivot = $rightNode->left();
         $rightNode->setLeft($pivot->right());
         $pivot->setRight($rightNode);
         $root->setRight($pivot);
     }
     $pivot = $root->right();
     $root->setRight($pivot->left());
     $pivot->setLeft($root);
     return $pivot;
 }
예제 #4
0
 function testConstructor()
 {
     $avl = new AvlTree(function ($a, $b) {
         if ($a < $b) {
             return 1;
         } elseif ($b < $a) {
             return -1;
         } else {
             return 0;
         }
     });
     $avl->add(4);
     $avl->add(3);
     $avl->add(5);
     $root = new BinaryTree(4);
     $root->setLeft(new BinaryTree(5));
     $root->setRight(new BinaryTree(3));
     $this->reCalculateHeights($root);
     $this->assertEquals($root, $avl->toBinaryTree());
 }
예제 #5
0
 private function copyNode(SplayNode $n = null)
 {
     if ($n === null) {
         return null;
     }
     $new = new BinaryTree($n->value);
     $new->setLeft($this->copyNode($n->left));
     $new->setRight($this->copyNode($n->right));
     return $new;
 }
예제 #6
0
 function testContains()
 {
     $object = new SplayTree();
     $this->assertFalse($object->contains(1));
     $object->add(0);
     $this->assertFalse($object->contains(1));
     $object->add(2);
     $this->assertFalse($object->contains(1));
     $object->add(1);
     $this->assertTrue($object->contains(1));
     $expected = new BinaryTree(1);
     $expected->setLeft(new BinaryTree(0));
     $expected->setRight(new BinaryTree(2));
     $actual = $object->toBinaryTree();
     $this->assertEquals($expected, $actual);
 }