Ejemplo n.º 1
0
 /**
  * Tests that the method testParent() works as expected.
  */
 public function testParent()
 {
     $nodeA = new Node('A');
     // Does not have parent.
     $nodeB = new Node('B');
     // Parent is A
     $nodeC = new Node('C');
     // Parent is B
     $nodeD = new Node('D');
     // Parent is C
     $nodeE = new Node('E');
     // Parent is D
     $nodeF = new Node('F');
     // Parent is B
     $nodeA->addChild($nodeB)->addChild($nodeC)->addChild($nodeD);
     $nodeD->addChild($nodeE);
     $nodeB->addChild($nodeF);
     $this->assertNull($nodeA->getParent());
     $this->assertSame($nodeB->getParent(), $nodeA);
     $this->assertSame($nodeC->getParent(), $nodeB);
     $this->assertSame($nodeD->getParent(), $nodeC);
     $this->assertSame($nodeE->getParent(), $nodeD);
     $this->assertSame($nodeF->getParent(), $nodeB);
     $this->assertNotSame($nodeB->getParent(), $nodeC);
     $this->assertNotSame($nodeC->getParent(), $nodeD);
     $this->assertNotSame($nodeD->getParent(), $nodeE);
     $this->assertNotSame($nodeE->getParent(), $nodeF);
     $this->assertNotSame($nodeF->getParent(), $nodeA);
     // It's OK.
 }
Ejemplo n.º 2
0
<?php

/**
 * This example illustrates various operations on nodes.
 */
include __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
use dbeurive\Tree\Tree;
use dbeurive\Tree\Node;
$root = new Node("root");
$A = new Node("A");
$AA = new Node("A");
$B = new Node("B");
$C = new Node("C");
$D = new Node("D");
$E = new Node("E");
$EE = new Node("EE");
$EEE = new Node("EEE");
$F = new Node("F");
$H = new Node("H");
$I = new Node("I");
$J = new Node("J");
$K = new Node("K");
$L = new Node("L");
$tree = new Tree($root);
$tree->getRoot()->addChild($A)->end()->addChild($B)->end()->addChild($C)->addChild($D)->addChild($E)->addChild($EE)->end()->addChild($EEE)->end()->end()->addChild($F)->end()->end()->addChild($AA)->addChild($H)->addChild($I)->addChild($J)->addChild($K)->end()->addChild($L)->end()->end()->end()->end()->end()->end();
// ---------------------------------------------------------------------------------------------------------------------
// Testing ancestry
// ---------------------------------------------------------------------------------------------------------------------
print "Is <" . $root->getData() . "> an ascendant of <" . $L->getData() . "> ? " . ($root->isAscendantOf($L) ? 'yes' : 'no') . "\n";
print "Is <" . $root->getData() . "> an ascendant of <" . $F->getData() . "> ? " . ($root->isAscendantOf($F) ? 'yes' : 'no') . "\n";
print "Is <" . $L->getData() . "> an ascendant of <" . $K->getData() . "> ? " . ($L->isAscendantOf($K) ? 'yes' : 'no') . "\n";