Esempio n. 1
0
 /**
  * @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;
 }
Esempio n. 2
0
 /**
  *
  * 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;
         }
     }
 }
Esempio n. 3
0
 public function testDequeueEmptyQueue()
 {
     self::assertFalse($this->queue->dequeue());
 }