/** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on succes; non-zero on failure. */ public static function main($args) { printf("Demonstration program number 5.\n"); $status = 0; GeneralTree::main($args); BinaryTree::main($args); NaryTree::main($args); BinarySearchTree::main($args); AVLTree::main($args); MWayTree::main($args); BTree::main($args); return $status; }
$values[] = $value; $i++; } } return $values; } $practiceDB = new Database('algorithm_practice'); $query = $practiceDB->db->prepare("SELECT * FROM sorting WHERE id < {$limit}"); try { $query->execute(); $unsortedArray = $query->fetchAll(PDO::FETCH_COLUMN, 1); } catch (PDOException $e) { echo 'Query error: ' . $query->errorCode(); exit; } $searchTree = new BinarySearchTree($unsortedArray); echo "Checking for values that should be present<br>"; $present = selectPresentValues($unsortedArray, $limit / 2); $present[] = 3685; // the first number in the DB, make sure the root is OK; foreach ($present as $valueToFind) { $foundPresent = $searchTree->search($valueToFind); if (!$foundPresent) { echo "Problem: couldn't find {$valueToFind}<br/>"; } } echo "Checking for values that should not be present<br>"; $notPresent = selectNotPresentValues($unsortedArray, $limit / 2); foreach ($notPresent as $shouldNotFind) { $foundNotPresent = $searchTree->search($shouldNotFind); if ($foundNotPresent) {
/** * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return SortedSetIterator */ function getIterator() { return new SortedSetIterator($this->bst->getIterator(), $this->count()); }
/** * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return SortedMapIterator */ function getIterator() { return new SortedMapIterator(new InOrderIterator($this->avl->toBinaryTree(), $this->avl->count()), $this->avl->count()); }
<?php spl_autoload_register(function ($class) { $file = str_replace('\\', '/', $class); require "../{$file}.php"; }); $bst = new BinarySearchTree(); $m = new Key('m'); $bst->put($m, 99); $a = new Key('a'); $bst->put($a, 99); $x = new Key('x'); $bst->put($x, 88); $c = new Key('c'); $bst->put($c, 77); $z = new Key('z'); $bst->put($z, 66); $j = new Key('j'); $bst->put($j, 52); echo "length: {$bst->length()}\n";
} else { $this->detachKey(); } } else { if ($diff < 0) { $this->getLeft()->withdraw($obj); } else { $this->getRight()->withdraw($obj); } } $this->balance(); } //}>e /** * 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"); $status = 0; $bst = new BinarySearchTree(); AbstractSearchTree::test($bst); return $status; } } if (realpath($argv[0]) == realpath(__FILE__)) { exit(BinarySearchTree::main(array_slice($argv, 1))); }
/** * Detaches the key from this node; making it the empty node. */ public function detachKey() { $this->height = -1; return parent::detachKey(); }
} } public function search($val) { $node = $this->_root; while ($node !== $this->_sentinel && $node->val() !== $val) { if ($val < $node->val()) { $node = $node->left(); } else { $node = $node->right(); } } return $node; } } $BST = new BinarySearchTree(); $BST->insert(15); $BST->insert(6); $BST->insert(18); $BST->insert(3); $BST->insert(7); $BST->insert(17); $BST->insert(20); $BST->insert(2); $BST->insert(4); $BST->insert(13); $BST->insert(9); function test_searching($num) { global $BST; $node = $BST->search($num);