public function testGetDepths()
 {
     $firstLevel1 = new BinaryTreeNode("first level 1");
     $secondLevel1 = new BinaryTreeNode("second level 1");
     $secondLevel2 = new BinaryTreeNode("second level 2");
     $thirdLevel1 = new BinaryTreeNode("third level 1");
     $thirdLevel2 = new BinaryTreeNode("third level 2");
     $thirdLevel3 = new BinaryTreeNode("third level 3");
     $fourthLevel1 = new BinaryTreeNode("fourth level 1");
     $firstLevel1->setLeft($secondLevel1);
     $firstLevel1->setRight($secondLevel2);
     $secondLevel1->setRight($thirdLevel1);
     $secondLevel2->setLeft($thirdLevel2);
     $secondLevel2->setRight($thirdLevel3);
     $thirdLevel2->setLeft($fourthLevel1);
     $firstLevel = new LinkedList();
     $firstLevel->add($firstLevel1);
     $secondLevel = new LinkedList();
     $secondLevel->add($secondLevel1);
     $secondLevel->add($secondLevel2);
     $thirdLevel = new LinkedList();
     $thirdLevel->add($thirdLevel1);
     $thirdLevel->add($thirdLevel2);
     $thirdLevel->add($thirdLevel3);
     $fourthLevel = new LinkedList();
     $fourthLevel->add($fourthLevel1);
     $expected = [$firstLevel, $secondLevel, $thirdLevel, $fourthLevel];
     $this->assertEquals($expected, DFSDepthLister::getDepths($firstLevel1));
     $this->assertEquals($expected, BFSDepthLister::getDepths($firstLevel1));
 }
 public function setLeft(BinaryTreeNode $left)
 {
     if (!$left instanceof BinaryTreeNodeWithParent) {
         throw new InvalidArgumentException('When building a binary tree with parent references, all nodes must be instances of BinaryTreeNodeWithParent');
     }
     parent::setLeft($left);
     $left->setParent($this);
 }
 public function testGetSequencesWithFiveNodes()
 {
     $n1 = new BinaryTreeNode(1);
     $n2 = new BinaryTreeNode(2);
     $n3 = new BinaryTreeNode(3);
     $n4 = new BinaryTreeNode(4);
     $n5 = new BinaryTreeNode(5);
     $n2->setLeft($n1);
     $n2->setRight($n3);
     $n3->setLeft($n4);
     $n4->setRight($n5);
     $expected = [[2, 1, 3, 4, 5], [2, 3, 1, 4, 5], [2, 3, 4, 1, 5], [2, 3, 4, 5, 1], [2, 1, 4, 3, 5], [2, 4, 1, 3, 5], [2, 4, 3, 1, 5], [2, 4, 3, 5, 1], [2, 1, 4, 5, 3], [2, 4, 1, 5, 3], [2, 4, 5, 1, 3], [2, 4, 5, 3, 1], [2, 1, 3, 5, 4], [2, 3, 1, 5, 4], [2, 3, 5, 1, 4], [2, 3, 5, 4, 1], [2, 1, 5, 3, 4], [2, 5, 1, 3, 4], [2, 5, 3, 1, 4], [2, 5, 3, 4, 1], [2, 1, 5, 4, 3], [2, 5, 1, 4, 3], [2, 5, 4, 1, 3], [2, 5, 4, 3, 1]];
     $this->assertEquals($expected, BinarySearchTreeSequenceCalculator::getSequences($n2));
 }
 public function setLeft(BinaryTreeNode $left = null)
 {
     if ($left !== null && !$left instanceof BinaryTreeNodeWithParent) {
         throw new InvalidArgumentException('When building a binary tree with parent references, all nodes must be instances of BinaryTreeNodeWithParent');
     }
     $oldLeft = $this->getLeft();
     if ($oldLeft !== null) {
         $oldLeft->setParent(null);
     }
     parent::setLeft($left);
     if ($left !== null) {
         $left->setParent($this);
     }
 }
 public static function build(array $a, $startIndex = 0, $endIndex = null)
 {
     if ($endIndex === null) {
         $endIndex = count($a) - 1;
     }
     $nodeIndex = self::getMidPoint($startIndex, $endIndex);
     $node = new BinaryTreeNode($a[$nodeIndex]);
     if ($nodeIndex - $startIndex > 0) {
         $node->setLeft(self::build($a, $startIndex, $nodeIndex - 1));
     }
     if ($endIndex - $nodeIndex > 0) {
         $node->setRight(self::build($a, $nodeIndex + 1, $endIndex));
     }
     return $node;
 }
 public function testIsBinarySearchTree()
 {
     $n1 = new BinaryTreeNode(1);
     $n2 = new BinaryTreeNode(2);
     $n3 = new BinaryTreeNode(3);
     $n4 = new BinaryTreeNode(4);
     $n5 = new BinaryTreeNode(5);
     $n6 = new BinaryTreeNode(6);
     $n7 = new BinaryTreeNode(7);
     $n8 = new BinaryTreeNode(8);
     $n9 = new BinaryTreeNode(9);
     $n10 = new BinaryTreeNode(10);
     $n11 = new BinaryTreeNode(11);
     $n12 = new BinaryTreeNode(12);
     $n13 = new BinaryTreeNode(13);
     $n14 = new BinaryTreeNode(14);
     $n15 = new BinaryTreeNode(15);
     $n16 = new BinaryTreeNode(16);
     $n8->setLeft($n4);
     $n8->setRight($n12);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n4->setLeft($n2);
     $n4->setRight($n6);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n12->setLeft($n10);
     $n12->setRight($n14);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n2->setLeft($n16);
     $this->assertFalse(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n2->setLeft($n1);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n2->setRight($n15);
     $this->assertFalse(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n2->setRight($n3);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n10->setRight($n9);
     $this->assertFalse(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n10->setRight($n11);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n14->setLeft($n5);
     $this->assertFalse(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n14->setLeft($n13);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
     $n6->setLeft($n5);
     $n6->setRight($n7);
     $this->assertTrue(BinarySearchTreeChecker::isBinarySearchTree($n8));
 }
 public function testBinaryTreeNode()
 {
     $node1 = new BinaryTreeNode('Dizzy');
     $this->assertEquals('Dizzy', $node1->getData());
     $this->assertEquals('Dizzy', (string) $node1);
     $node1->setData('Miles');
     $this->assertEquals('Miles', $node1->getData());
     $this->assertEquals('Miles', (string) $node1);
     $this->assertNull($node1->getLeft());
     $this->assertNull($node1->getRight());
     $node2 = new BinaryTreeNode('Cannonball');
     $node3 = new BinaryTreeNode('Coltrane');
     $node1->setLeft($node2);
     $node1->setRight($node3);
     $this->assertSame($node2, $node1->getLeft());
     $this->assertSame($node3, $node1->getRight());
 }
 public function testIsBalanced()
 {
     $n1 = new BinaryTreeNode("one");
     $n2 = new BinaryTreeNode("two");
     $n3 = new BinaryTreeNode("three");
     $n4 = new BinaryTreeNode("four");
     $n5 = new BinaryTreeNode("five");
     $n6 = new BinaryTreeNode("six");
     $n7 = new BinaryTreeNode("seven");
     $n8 = new BinaryTreeNode("eight");
     $n9 = new BinaryTreeNode("nine");
     $n10 = new BinaryTreeNode("ten");
     $n11 = new BinaryTreeNode("eleven");
     $n12 = new BinaryTreeNode("twelve");
     $n13 = new BinaryTreeNode("thirteen");
     $n14 = new BinaryTreeNode("fourteen");
     $n15 = new BinaryTreeNode("fifteen");
     $n16 = new BinaryTreeNode("sixteen");
     $n1->setLeft($n2);
     $this->assertTrue(BalancedTreeChecker::isBalanced($n1));
     $n1->setRight($n3);
     $this->assertTrue(BalancedTreeChecker::isBalanced($n1));
     $n2->setLeft($n4);
     $this->assertTrue(BalancedTreeChecker::isBalanced($n1));
     $n2->setRight($n5);
     $this->assertTrue(BalancedTreeChecker::isBalanced($n1));
     $n3->setLeft($n6);
     $n3->setRight($n7);
     $this->assertTrue(BalancedTreeChecker::isBalanced($n1));
     $n6->setLeft($n8);
     $n8->setLeft($n9);
     $this->assertFalse(BalancedTreeChecker::isBalanced($n1));
     $n6->setRight($n10);
     $this->assertFalse(BalancedTreeChecker::isBalanced($n1));
     $n10->setLeft($n11);
     $this->assertFalse(BalancedTreeChecker::isBalanced($n1));
     $n4->setLeft($n12);
     $n4->setRight($n13);
     $this->assertFalse(BalancedTreeChecker::isBalanced($n1));
     $n5->setLeft($n14);
     $n5->setRight($n15);
     $this->assertFalse(BalancedTreeChecker::isBalanced($n1));
     $n7->setLeft($n16);
     $this->assertTrue(BalancedTreeChecker::isBalanced($n1));
 }
 public function testCountPathsMoreInterestingExample()
 {
     $n1 = new BinaryTreeNode(7);
     $n2 = new BinaryTreeNode(-2);
     $n3 = new BinaryTreeNode(4);
     $n4 = new BinaryTreeNode(1);
     $n5 = new BinaryTreeNode(-3);
     $n6 = new BinaryTreeNode(2);
     $n7 = new BinaryTreeNode(2);
     $n8 = new BinaryTreeNode(5);
     $n4->setLeft($n2);
     $n4->setRight($n6);
     $n2->setLeft($n1);
     $n2->setRight($n3);
     $n6->setLeft($n5);
     $n6->setRight($n7);
     $n5->setRight($n8);
     $this->assertEquals(4, PathsWithSumCounterBruteForce::countPathsWithSum($n4, 5));
     $this->assertEquals(4, PathsWithSumCounter::countPathsWithSum($n4, 5));
 }
 public function testFindFirstCommonAncestorWithFourLevelTree()
 {
     $n1 = new BinaryTreeNode(1);
     $n2 = new BinaryTreeNode(2);
     $n3 = new BinaryTreeNode(3);
     $n4 = new BinaryTreeNode(4);
     $n5 = new BinaryTreeNode(5);
     $n6 = new BinaryTreeNode(6);
     $n7 = new BinaryTreeNode(7);
     $n8 = new BinaryTreeNode(8);
     $n9 = new BinaryTreeNode(9);
     $n10 = new BinaryTreeNode(10);
     $n11 = new BinaryTreeNode(11);
     $n12 = new BinaryTreeNode(12);
     $n13 = new BinaryTreeNode(13);
     $n14 = new BinaryTreeNode(14);
     $n15 = new BinaryTreeNode(15);
     $n8->setLeft($n4);
     $n8->setRight($n12);
     $n4->setLeft($n2);
     $n4->setRight($n6);
     $n12->setLeft($n10);
     $n12->setRight($n14);
     $n2->setLeft($n1);
     $n2->setRight($n3);
     $n6->setLeft($n5);
     $n6->setRight($n7);
     $n10->setLeft($n9);
     $n10->setRight($n11);
     $n14->setLeft($n13);
     $n14->setRight($n15);
     $this->assertSame($n8, FirstCommonAncestorFinder::findFirstCommonAncestor($n8, $n1, $n15));
     $this->assertSame($n8, FirstCommonAncestorFinder::findFirstCommonAncestor($n8, $n4, $n12));
     $this->assertSame($n4, FirstCommonAncestorFinder::findFirstCommonAncestor($n8, $n2, $n5));
     $this->assertSame($n4, FirstCommonAncestorFinder::findFirstCommonAncestor($n8, $n6, $n1));
     $this->assertSame($n4, FirstCommonAncestorFinder::findFirstCommonAncestor($n8, $n1, $n6));
     $this->assertSame($n4, FirstCommonAncestorFinder::findFirstCommonAncestor($n8, $n1, $n4));
     $this->assertSame($n6, FirstCommonAncestorFinder::findFirstCommonAncestor($n8, $n6, $n7));
 }
 public function testIsSubTree()
 {
     $t1n1 = new BinaryTreeNode(1);
     $t1n2 = new BinaryTreeNode(2);
     $t1n3 = new BinaryTreeNode(3);
     $t1n4 = new BinaryTreeNode(5);
     $t1n5 = new BinaryTreeNode(6);
     $t1n1->setLeft($t1n2);
     $t1n1->setRight($t1n3);
     $t1n3->setLeft($t1n4);
     $t1n3->setRight($t1n5);
     $t2n1 = new BinaryTreeNode(3);
     $t2n2 = new BinaryTreeNode(5);
     $t2n3 = new BinaryTreeNode(6);
     $t2n1->setLeft($t2n2);
     $t2n1->setRight($t2n3);
     $this->assertTrue(SubTreeChecker::isSubTree($t1n1, $t2n1));
     $this->assertTrue(SubTreeChecker::isSubTree($t1n1, $t1n1));
     $t3n1 = new BinaryTreeNode(3);
     $t3n2 = new BinaryTreeNode(4);
     $t3n1->setLeft($t3n2);
     $this->assertFalse(SubTreeChecker::isSubTree($t1n1, $t3n1));
     $t3n3 = new BinaryTreeNode(5);
     $t3n1->setRight($t3n3);
     $this->assertFalse(SubTreeChecker::isSubTree($t1n1, $t3n1));
     $t4n1 = new BinaryTreeNode(10);
     $t4n2 = new BinaryTreeNode(11);
     $t4n1->setLeft($t4n2);
     $this->assertFalse(SubTreeChecker::isSubTree($t1n1, $t4n1));
     $t5n1 = new BinaryTreeNode(3);
     $t5n2 = new BinaryTreeNode(5);
     $t5n1->setLeft($t5n2);
     $this->assertFalse(SubTreeChecker::isSubTree($t1n1, $t5n1));
     $t5n3 = new BinaryTreeNode(6);
     $t5n4 = new BinaryTreeNode(7);
     $t5n1->setRight($t5n3);
     $t5n2->setLeft($t5n4);
     $this->assertFalse(SubTreeChecker::isSubTree($t1n1, $t5n1));
 }