/** * 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; }
/** * Reads a dictionary and then translates the words in an input text * word-by-word, printing the result on the output stream. * The dictionary consists of pairs of words. * The first element of each pair is a word in source language. * The second element of each pair is the word in the target language. * Uses a search tree. * * @param resource $dictionary An input stream * from which the pairs of words are read. * @param resource $in The input stream to be translated. * @param resource $out The output stream on which to print the translation. */ public static function translate($dictionary, $in, $out) { $searchTree = new AVLTree(); while (($line = fgets($dictionary)) != false) { $line = preg_replace('/\\n$/', '', $line); list($key, $value) = preg_split('/[ \\t]+/', $line, 2); $searchTree->insert(new Association(box($key), box($value))); } while (($line = fgets($in)) != false) { $words = preg_split('/[ \\t\\n]+/', $line, -1, PREG_SPLIT_NO_EMPTY); foreach ($words as $word) { $obj = $searchTree->find(new Association(box($word))); if ($obj === NULL) { fprintf($out, "%s ", $word); } else { fprintf($out, "%s ", str($obj->getValue())); } } } }
$this->height = 0; } //}>f //{ /** * Detaches the key from this node; making it the empty node. */ public function detachKey() { $this->height = -1; return parent::detachKey(); } //}>g /** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on success; non-zero on failure. */ public static function main($args) { printf("AVLTree main program.\n"); $status = 0; $bst = new AVLTree(); AbstractSearchTree::test($bst); return $status; } } if (realpath($argv[0]) == realpath(__FILE__)) { exit(AVLTree::main(array_slice($argv, 1))); }