/** * @return array */ public function breadthFirst() { $queue = new Queue(); $queue->enqueue($this->root); $listToReturn = []; while (!$queue->isEmpty()) { /** * @var BinaryTreeNode $dequeue */ $dequeue = $queue->dequeue(); if ($dequeue->getLeft() !== null) { $queue->enqueue($dequeue->getLeft()); } if ($dequeue->getRight() !== null) { $queue->enqueue($dequeue->getRight()); } array_push($listToReturn, $dequeue->getData()); } return $listToReturn; }
/** * * Similar to bredth first traversal * * @return Ambigous <NULL, DataStructureNode> */ public function findFirstAvaliableNode() { $queue = new Queue(); $queue->enqueue($this->root); while (!$queue->isEmpty()) { $dequeue = $queue->dequeue(); if ($dequeue->left !== null) { $queue->enqueue($dequeue->left); } else { return dequeue; } if ($dequeue->right != null) { $queue->enqueue($dequeue->right); } else { return dequeue; } } }
public function testDequeueEmptyQueue() { self::assertFalse($this->queue->dequeue()); }