public function insert($val) { parent::insert($val); $rotationAmt = $this->balance($this->elements[$val]); $this->updateCoordinate(); return $rotationAmt; }
$this->pO($this->root); return $this; } private function pO($node) { if ($node) { $this->pO($node->left); echo $node->val . " "; $this->pO($node->right); } } } class Node { public $val, $left, $right; public function __construct($val, $left = null, $right = null) { $this->val = $val; $this->right = $right; $this->left = $left; } } $bst = new BST(); $time1 = microtime(); for ($n = 0; $n < 10000; $n++) { $val = rand(0, 1000); $bst->insert($val); } $bst->printOut(); $time2 = microtime(); echo "<br/>Here are the results, it took " . ($time2 - $time1) . " microseconds to process them";
/** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on success; non-zero on failure. */ public static function main($args) { printf("BinarySearchTree main program.\n"); $root = new BST(); foreach ($args as $row) { $root->insert($row); } return $root; }
public static function testStructure() { $bst = new BST(); if ($bst->getCount() !== 0) { self::printError("problem creating a new BST - count not 0"); } $existingKeys = array(); $bst->insert(40, '40'); $bst->insert(20, '20'); $bst->insert(60, '60'); $bst->insert(10, '10'); $bst->insert(30, '30'); $bst->insert(50, '50'); $bst->insert(70, '70'); $bst->insert(90, '90'); $bst->insert(15, '15'); $bst->insert(45, '45'); $bst->insert(55, '55'); $bst->insert(35, '35'); $bst->insert(25, '25'); $bst->insert(75, '75'); $bst->insert(5, '5'); // for ($i = 1; $i <= 15; $i++) { // do { // // keep trying until we have a unique key // $key = rand(1,99); // } while (in_array($key, $existingKeys)); // $bst->insert($key); // $existingKeys[] = $key; // } if ($bst->getCount() !== 15) { self::printError("problem with insertion. should be 15 keys"); } // display... for now $bst->display(); // test remove $result = $bst->remove(50); $result = $bst->remove(70); // display... for now $bst->display(); }