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); }
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()); } }
private function next_valueNotNull() { $right = $this->value->right(); if ($right !== null) { $this->stack->push($right); } $this->next_push($this->value->left()); }
/** * @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; }
private function reCalculateHeights(BinaryTree $root = null) { if ($root === null) { return; } $this->reCalculateHeights($root->left()); $this->reCalculateHeights($root->right()); $root->recalculateHeight(); }