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)); $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); }
/** * @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; }
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()); }
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; }
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); }