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();
 }
 function test_toArray()
 {
     $tree = new BinaryTree(0);
     $tree->setLeft(new BinaryTree(-4));
     $tree->left()->setLeft(new BinaryTree(1));
     $tree->left()->setRight(new BinaryTree(2));
     $iterator = $this->instance($tree, 4);
     $expect = [0, -4, 1, 2];
     $actual = $iterator->toArray();
     $this->assertEquals($expect, $actual);
 }
Example #3
0
 /**
  * @param BinaryTree $node
  * @return BinaryTree
  */
 private function deleteNeitherChildIsNull(BinaryTree $node)
 {
     $value = $node->inOrderPredecessor()->value();
     $node->setLeft($this->removeRecursive($value, $node->left()));
     $node->setValue($value);
     return $node;
 }
Example #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());
 }
Example #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;
 }
Example #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);
 }