Ejemplo n.º 1
0
 public function testLinkedList()
 {
     $list = new LinkedList();
     $this->assertEquals(0, $list->getSize());
     $this->assertTrue($list->isEmpty());
     $this->assertNull($list->peekFirst());
     $this->assertNull($list->peekLast());
     $this->assertNull($list->removeFirst());
     $this->assertNull($list->removeLast());
     $count = 0;
     foreach ($list as $key => $value) {
         $count++;
     }
     $this->assertEquals(0, $count);
     $list->add("one");
     $this->assertEquals(1, $list->getSize());
     $this->assertFalse($list->isEmpty());
     $list->add("two");
     $this->assertEquals(2, $list->getSize());
     $this->assertFalse($list->isEmpty());
     $list->add("three");
     $this->assertEquals(3, $list->getSize());
     $this->assertFalse($list->isEmpty());
     $expected = ["one", "two", "three"];
     $i = 0;
     foreach ($list as $key => $value) {
         $this->assertEquals($i, $key);
         $this->assertEquals($expected[$i], $value);
         $i++;
     }
     $this->assertEquals(3, $i);
     $this->assertEquals("one", $list->peekFirst());
     $this->assertEquals("one", $list->removeFirst());
     $this->assertEquals(2, $list->getSize());
     $this->assertEquals("three", $list->peekLast());
     $this->assertEquals("three", $list->removeLast());
     $this->assertEquals(1, $list->getSize());
     $this->assertEquals("two", $list->peekFirst());
     $this->assertEquals("two", $list->removeFirst());
     $this->assertEquals(0, $list->getSize());
     $this->assertTrue($list->isEmpty());
     $list->add("four");
     $this->assertEquals(1, $list->getSize());
     $this->assertFalse($list->isEmpty());
     foreach ($list as $key => $value) {
         $this->assertEquals(0, $key);
         $this->assertEquals("four", $value);
     }
     $this->assertEquals("four", $list->peekFirst());
     $this->assertEquals("four", $list->removeLast());
     $this->assertEquals(0, $list->getSize());
     $this->assertTrue($list->isEmpty());
 }
 public static function getDepths(BinaryTreeNode $n)
 {
     $depths = [];
     $nodes = new LinkedList();
     $nodes->add($n);
     while ($nodes !== null) {
         $childNodes = new LinkedList();
         foreach ($nodes as $node) {
             $left = $node->getLeft();
             if ($left !== null) {
                 $childNodes->add($left);
             }
             $right = $node->getRight();
             if ($right !== null) {
                 $childNodes->add($right);
             }
         }
         $depths[] = $nodes;
         $nodes = !$childNodes->isEmpty() ? $childNodes : null;
     }
     return $depths;
 }