-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swapped the Node's constructor parameters
children links are (arguably) more important than the parent link
- Loading branch information
Showing
2 changed files
with
24 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,27 +10,28 @@ | |
use JsonSerializable; | ||
|
||
/** | ||
* Basic data node implementation. | ||
* Flexible data node implementation. | ||
* | ||
* Note: Iterating over a node will iterate over the whole subtree in pre-order DFS. | ||
* | ||
* @author Andrej Rypak <[email protected]> | ||
*/ | ||
class Node implements TreeNodeContract, DataNodeContract, MovableNodeContract, IteratorAggregate, JsonSerializable | ||
{ | ||
/** | ||
* Create a data node. | ||
* | ||
* Note that passing children or parent here does NOT make a link the other way around. Use the Tree utility for that. | ||
* @see Tree::link() | ||
* @see Tree::linkChildren() | ||
*/ | ||
public function __construct( | ||
protected mixed $data, | ||
protected ?TreeNodeContract $parent = null, | ||
protected array $children = [], | ||
protected ?TreeNodeContract $parent = null, | ||
) { | ||
} | ||
|
||
/** | ||
* Get the node's parent, if any. | ||
*/ | ||
public function parent(): ?TreeNodeContract | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
/** | ||
* Get the node's children. | ||
* | ||
|
@@ -41,6 +42,14 @@ public function children(): array | |
return $this->children; | ||
} | ||
|
||
/** | ||
* Get the node's parent, if any. | ||
*/ | ||
public function parent(): ?TreeNodeContract | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
/** | ||
* Discover whether the given node is one of this node's children (or the given key points to one of them). | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters