Ejemplo n.º 1
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));
     $iterator = $this->instance($tree, 4);
     $expect = [0, -4, 1, 2];
     $actual = $iterator->toArray();
     $this->assertEquals($expect, $actual);
 }
Ejemplo n.º 2
0
 protected function assertBinaryTree(BinaryTree $root)
 {
     $x = $root->value();
     $left = $root->left();
     if ($left !== null) {
         $y = $root->left()->value();
         $this->assertLessThan($x, $y);
         $this->assertBinaryTree($root->left());
     }
     $right = $root->right();
     if ($right !== null) {
         $y = $right->value();
         $this->assertLessThan($y, $x);
         $this->assertBinaryTree($root->right());
     }
 }
Ejemplo n.º 3
0
 private function next_valueNotNull()
 {
     $right = $this->value->right();
     if ($right !== null) {
         $this->stack->push($right);
     }
     $this->next_push($this->value->left());
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
 private function reCalculateHeights(BinaryTree $root = null)
 {
     if ($root === null) {
         return;
     }
     $this->reCalculateHeights($root->left());
     $this->reCalculateHeights($root->right());
     $root->recalculateHeight();
 }