Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSize of the tree and parts of it #17

Merged
merged 1 commit into from
Jul 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Tree/Node/NodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ public function getHeight()
return max($heights) + 1;
}

/**
* Return the number of nodes in a tree
* @return int
*/
public function getSize()
{
$size = 1;
foreach ($this->getChildren() as $child) {
$size += $child->getSize();
}

return $size;
}

/**
* {@inheritdoc}
*/
Expand Down
28 changes: 28 additions & 0 deletions tests/Tree/Node/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,32 @@ public function testGetHeight()
$this->assertEquals(2, $root->getHeight());
$this->assertEquals(1, $child3->getHeight());
}


public function testGetSize()
{
$root = new Node;
$root
->addChild($child1 = new Node('child1'))
->addChild($child2 = new Node('child2'))
->addChild($child3 = new Node('child3'))
;

$child3
->addChild(new Node("a"))
->addChild($child4 = new Node("b"))
;

$child4->addChild($child5 = new Node("c"));
$child5
->addChild(new Node("d"))
->addChild(new Node("f"))
;

$this->assertEquals(9, $root->getSize());
$this->assertEquals(3, $child5->getSize());
$this->assertEquals(4, $child4->getSize());
$this->assertEquals(6, $child3->getSize());
$this->assertEquals(1, $child2->getSize());
}
}