/** * Search recursively a node by its key into the BST. * @param integer The key to search */ public function search($key) { // Getting the top root of the entire tree $treeRoot = $this->root(); // Initializing a binary node iterator in in-fix search mode $searchIterator = new BinaryIterator($treeRoot, BinaryIterator::IN_ORDER); // Scanning all the results, returning the first node matching foreach ($searchIterator->items() as $item) { if ($item->key() === $key) { return $item; } } // If nothing found, return false return false; }
/** * Get an iterator for the current node. * @param $method int The iteration method (0 = pre-order, 1 = in-order, 2 = post-order). * @return NodeIterator */ public function iterator($method = BinaryIterator::PRE_ORDER) { $result = new BinaryIterator($this, $method); return $result->items(); }