Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed `traverse`. I'd rather that they use an iterator. Also some g…

…eneral cleanup.
  • Loading branch information...
commit 180203f44721c96d63f6c3960afaf90c3a334ded 1 parent 77eec89
Levi Morrison authored

Showing 2 changed files with 4 additions and 53 deletions. Show diff stats Hide diff stats

  1. +4 23 src/Spl/BinarySearchTree.php
  2. +0 30 test/Spl/AvlTreeTest.php
27 src/Spl/BinarySearchTree.php
@@ -204,12 +204,10 @@ protected function containsNode($element, BinaryTree $node = NULL) {
204 204
205 205 if ($comparisonResult < 0) {
206 206 return $this->containsNode($element, $node->getLeft());
  207 + } elseif ($comparisonResult > 0) {
  208 + return $this->containsNode($element, $node->getRight());
207 209 } else {
208   - if ($comparisonResult > 0) {
209   - return $this->containsNode($element, $node->getRight());
210   - } else {
211   - return TRUE;
212   - }
  210 + return TRUE;
213 211 }
214 212
215 213 }
@@ -239,6 +237,7 @@ function getIterator($order = self::TRAVERSE_IN_ORDER) {
239 237 ? clone $this->root
240 238 : NULL
241 239 );
  240 +
242 241 switch ($order) {
243 242 case self::TRAVERSE_LEVEL_ORDER:
244 243 $iterator = new LevelOrderIterator($root);
@@ -262,24 +261,6 @@ function getIterator($order = self::TRAVERSE_IN_ORDER) {
262 261 }
263 262
264 263 /**
265   - * @param int $order
266   - * @param callable $callback
267   - *
268   - * @return void
269   - */
270   - function traverse($order, $callback) {
271   - if ($this->root === NULL) {
272   - return;
273   - }
274   -
275   - $iterator = $this->getIterator($order);
276   -
277   - foreach ($iterator as $item) {
278   - $callback($item);
279   - }
280   - }
281   -
282   - /**
283 264 * @link http://php.net/manual/en/countable.count.php
284 265 * @return int
285 266 */
30 test/Spl/AvlTreeTest.php
@@ -595,36 +595,6 @@ private function __testIterators(array $expectedSequences) {
595 595
596 596 }
597 597
598   - function testTraverse() {
599   - $sum = 0;
600   - $callback = function($item) use (&$sum) {
601   - $sum += $item;
602   - };
603   -
604   - $algorithms = array(
605   - AvlTree::TRAVERSE_IN_ORDER,
606   - AvlTree::TRAVERSE_PRE_ORDER,
607   - AvlTree::TRAVERSE_POST_ORDER,
608   - AvlTree::TRAVERSE_LEVEL_ORDER,
609   - );
610   -
611   - foreach ($algorithms as $algorithm) {
612   - $this->object->traverse($algorithm, $callback);
613   - }
614   -
615   - $this->assertEquals(0, $sum);
616   -
617   - $this->object->add(1);
618   - $this->object->add(2);
619   -
620   - foreach ($algorithms as $algorithm) {
621   - $this->object->traverse($algorithm, $callback);
622   - }
623   -
624   - $this->assertEquals(12, $sum);
625   -
626   - }
627   -
628 598 /**
629 599 * @covers \Spl\BinaryTree::getInOrderPredecessor
630 600 */

0 comments on commit 180203f

Please sign in to comment.
Something went wrong with that request. Please try again.