Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…eneral cleanup.
  • Loading branch information...
commit 180203f44721c96d63f6c3960afaf90c3a334ded 1 parent 77eec89
@morrisonlevi authored
Showing with 4 additions and 53 deletions.
  1. +4 −23 src/Spl/BinarySearchTree.php
  2. +0 −30 test/Spl/AvlTreeTest.php
View
27 src/Spl/BinarySearchTree.php
@@ -204,12 +204,10 @@ protected function containsNode($element, BinaryTree $node = NULL) {
if ($comparisonResult < 0) {
return $this->containsNode($element, $node->getLeft());
+ } elseif ($comparisonResult > 0) {
+ return $this->containsNode($element, $node->getRight());
} else {
- if ($comparisonResult > 0) {
- return $this->containsNode($element, $node->getRight());
- } else {
- return TRUE;
- }
+ return TRUE;
}
}
@@ -239,6 +237,7 @@ function getIterator($order = self::TRAVERSE_IN_ORDER) {
? clone $this->root
: NULL
);
+
switch ($order) {
case self::TRAVERSE_LEVEL_ORDER:
$iterator = new LevelOrderIterator($root);
@@ -262,24 +261,6 @@ function getIterator($order = self::TRAVERSE_IN_ORDER) {
}
/**
- * @param int $order
- * @param callable $callback
- *
- * @return void
- */
- function traverse($order, $callback) {
- if ($this->root === NULL) {
- return;
- }
-
- $iterator = $this->getIterator($order);
-
- foreach ($iterator as $item) {
- $callback($item);
- }
- }
-
- /**
* @link http://php.net/manual/en/countable.count.php
* @return int
*/
View
30 test/Spl/AvlTreeTest.php
@@ -595,36 +595,6 @@ private function __testIterators(array $expectedSequences) {
}
- function testTraverse() {
- $sum = 0;
- $callback = function($item) use (&$sum) {
- $sum += $item;
- };
-
- $algorithms = array(
- AvlTree::TRAVERSE_IN_ORDER,
- AvlTree::TRAVERSE_PRE_ORDER,
- AvlTree::TRAVERSE_POST_ORDER,
- AvlTree::TRAVERSE_LEVEL_ORDER,
- );
-
- foreach ($algorithms as $algorithm) {
- $this->object->traverse($algorithm, $callback);
- }
-
- $this->assertEquals(0, $sum);
-
- $this->object->add(1);
- $this->object->add(2);
-
- foreach ($algorithms as $algorithm) {
- $this->object->traverse($algorithm, $callback);
- }
-
- $this->assertEquals(12, $sum);
-
- }
-
/**
* @covers \Spl\BinaryTree::getInOrderPredecessor
*/
Please sign in to comment.
Something went wrong with that request. Please try again.