Skip to content

Commit

Permalink
Added NaryTree root node getter support
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanklick committed May 7, 2014
1 parent 321078c commit 35974dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tjsd/collections/NaryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class NaryTree implements Tree {
public function insert(types\Comparable $element) {
if($this->isEmpty()) {
$this->rootNode = new types\NaryTreeNode($element);

return $this->rootNode;
} else {
$this->rootNode->insert($element);
return $this->rootNode->insert($element);
}
}

Expand All @@ -24,6 +26,10 @@ public function __toString() {
public function clear() {
$this->rootNode = null;
}

public function root() {
return $this->rootNode;
}

public function contains($element) {
if($element instanceof types\Comparable) {
Expand Down
2 changes: 2 additions & 0 deletions tjsd/collections/types/NaryTreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function insert(Comparable $element) {
$item = new self($element);
$item->setParent($this);
$this->children[] = $item;

return $item;
} else {
throw new DuplicateEntryException('Value is already present - cannot insert two elements with same value.');
}
Expand Down

0 comments on commit 35974dc

Please sign in to comment.