Module wonderful.element.node
Tree node classes.
Classes and inheritance
Node | The tree node class. |
wonderful.element.node.ListMixin | A mixin class that adds the methods appendChild and prependChild. |
Class Node
Node:__new__() | Construct a new node. |
Node:lbfsWalk(func) | Perform left-to-right breadth-first tree traversal. |
Node:nlrWalk(func) | Perform left-to-right pre-order depth-first traversal. |
Node:rlnWalk(func) | Perform right-to-left post-order depth-first traversal. |
Node:flagWalk(func, key) | Perform left-to-right pre-order depth-first traversal of flagged nodes. |
Node:insertChild(index, node) | Insert a node at a given index. |
Node:removeChild(index) | Remove a node. |
Node:replaceChild(index, child) | Replace a node at a given index. |
Node:hasChildren() | Check whether the node has children. |
Node:getParent() | Get the parent node. |
Node:getChildren() | Get the table of the node's children. |
Node:getRoot() | Get the root node. |
Node:hasParent() | Check whether the node has a parent. |
Node:getLevel() | Calculate the depth level of the node in the tree. |
Node:getIndex(child) | Get the index of a child in the list. |
Class ListMixin
ListMixin:prependChild(...) | Prepend a node. |
ListMixin:appendChild(...) | Append a node. |
Classes and inheritance
- Node
- The tree node class.
- wonderful.element.node.ListMixin
-
A mixin class that adds the methods appendChild and prependChild.
If you extend from this class, you have to implement methods insertChild and getChildren yourself.
Class Node
- Node:__new__()
- Construct a new node.
- Node:lbfsWalk(func)
-
Perform left-to-right breadth-first tree traversal.
If the function returns a non-
nil
value, traversal is stopped, and the returned value is returned.Parameters:
- func function(node) the function to call for each node
- Node:nlrWalk(func)
-
Perform left-to-right pre-order depth-first traversal.
If the function returns a non-
nil
value, traversal is stopped, and the returned value is returned.Parameters:
- func function(node) the function to call for each node
- Node:rlnWalk(func)
-
Perform right-to-left post-order depth-first traversal.
If the function returns a non-
nil
value, traversal is stopped, and the returned value is returned.Parameters:
- func function(node) the function to call for each node
- Node:flagWalk(func, key)
-
Perform left-to-right pre-order depth-first traversal of flagged nodes.
All nodes in the tree must have a flag, which is used to determine whether to descend and process:
- The walker only descends if the flag it raised or its trap is set off.
- The callback is called if the node or its descendant has its flag raised.
Parameters:
- func function(node) the function to call for each node to be processed
- key the key by which index nodes to get the flag
- Node:insertChild(index, node)
-
Insert a node at a given index.
If the node already has a parent, removes that node from its parent beforehand.
Parameters:
- index int the index
- node the node
- Node:removeChild(index)
-
Remove a node.
Parameters:
- index int or Node the index or the node
Returns:
-
the removed node
Or
-
`false`
the node is not a child
- Node:replaceChild(index, child)
-
Replace a node at a given index.
Parameters:
- index int or Node the index or the node
- child the node
Returns:
-
the replaced node
Or
-
`false`
the node is not a child
- Node:hasChildren()
-
Check whether the node has children.
Returns:
-
boolean
- Node:getParent()
-
Get the parent node.
Returns:
-
the parent node
Or
-
`nil`
the node has no parent
- Node:getChildren()
-
Get the table of the node's children.
Returns:
- Node:getRoot()
-
Get the root node.
Returns:
-
the root node
- Node:hasParent()
-
Check whether the node has a parent.
Returns:
-
boolean
- Node:getLevel()
-
Calculate the depth level of the node in the tree.
The root's level is
0
; its children have the level of1
, and so on.Returns:
-
int
- Node:getIndex(child)
-
Get the index of a child in the list.
Parameters:
- child the child
Returns:
-
int
the index
Or
-
nil
the node has no parent