public function test_removeRemovesElement()
 {
     $tree = new BinaryTree();
     $tree->insert(new types\NumericContainer(3));
     $tree->insert(new types\NumericContainer(2));
     $tree->insert(new types\NumericContainer(1));
     $tree->insert(new types\NumericContainer(5));
     $tree->insert(new types\NumericContainer(4));
     $tree->insert(new types\NumericContainer(7));
     $tree->insert(new types\NumericContainer(6));
     $tree->insert(new types\NumericContainer(8));
     $tree->remove(new types\NumericContainer(5));
     print_r($tree->toArray());
 }
        for ($j = 0; $j <= count($arr2); $j++) {
            if ($arr1[$i] == $arr2[$j]) {
                return $arr1[$i];
            }
        }
    }
}
$a = new Node(30);
$b = new Node(52);
$c = new Node(8);
$d = new Node(3);
$e = new Node(20);
$f = new Node(10);
$g = new Node(29);
$t = new BinaryTree();
$t->insert($a);
$t->insert($b);
$t->insert($c);
$t->insert($d);
$t->insert($e);
$t->insert($f);
$t->insert($g);
$fh = fopen($argv[1], "r");
while (!feof($fh)) {
    $test = trim(fgets($fh));
    $node_values = explode(" ", $test);
    if ($node_values[0] != "") {
        $parents1 = $t->search($node_values[0]);
        $parents2 = $t->search($node_values[1]);
        echo lewest_common($parents1, $parents2) . "\n";
    }
示例#3
0
     * Insert new node into tree
     *
     * @param TreeNode $node
     * @return $this
     */
    public function insert(TreeNode $node)
    {
        $this->_insert($node, $this->root);
        return $this;
    }
    /**
     * Check if tree is balanced
     *
     * @return bool True if tree is balanced
     */
    public function isBalanced()
    {
        return $this->_isBalanced($this->root);
    }
}
// Test cases
// Unbalanced Tree
$tree1 = new BinaryTree();
$tree1->insert(new TreeNode(3))->insert(new TreeNode(2))->insert(new TreeNode(4))->insert(new TreeNode(7))->insert(new TreeNode(6));
var_dump($tree1->isBalanced());
// bool -> false
// Balanced Tree
$tree2 = new BinaryTree();
$tree2->insert(new TreeNode(15))->insert(new TreeNode(10))->insert(new TreeNode(20))->insert(new TreeNode(8))->insert(new TreeNode(12))->insert(new TreeNode(16))->insert(new TreeNode(25));
var_dump($tree2->isBalanced());
// bool -> true
 function ranges()
 {
     $groups = array();
     $Indexes = new BinaryTree();
     if ($this->range === null) {
         return $groups;
     }
     foreach (func_get_args() as $number_set) {
         if (is_int($number_set)) {
             $number_set = intval($number_set);
             if ($number_set < 0) {
                 $assigned_range = ceil(abs($number_set) / $this->range) * -1;
             } else {
                 $assigned_range = ceil(abs($number_set) / $this->range);
             }
             $Indexes->insert($assigned_range);
             if (!isset($groups[$assigned_range])) {
                 $groups[$assigned_range] = new BinaryTree();
             }
             $groups[$assigned_range]->insert($number_set);
         } else {
             throw new InvalidArgumentException('This method only accpets integers:' . $number_set);
         }
     }
     $array_indexes = $Indexes->traverse();
     $returned_array = array();
     print_r($array_indexes);
     print_r(array_keys($groups));
     foreach ($array_indexes as $index) {
         $returned_array[] = $groups[$index]->traverse();
     }
     return $returned_array;
 }