Tree
The Tree class provides APIs for a simple tree data structure with a single root node that can
have any number of child nodes. Each child node can then also have children (recursively). Each
node in the tree can data associated with it.
Example
One common use-case for a temp table is for an API which returns a set of values which it needs to build in an iterative fasion.
local MyModule = select(2, ...).MyModule
local Tree = MyModule:From("LibTSMUtil"):IncludeClassType("Tree")
local tree = Tree.Create("strValue", "numValue")
local nodeA = tree:Insert(nil, "a", 1)
local nodeAA = tree:Insert(nodeA, "aa", 2)
local nodeAB = tree:Insert(nodeA, "ab", 3)
local nodeAAA = tree:Insert(nodeAA, "aaa", 4)
for _, child in ipairs({tree:GetChildren(nodeA)}) do
print(tree:GetData(child, "strValue"), tree:GetData(child, "numValue"))
end
-- aa 2
-- ab 3
API
-
class Tree:
Class -
staticmethod Create(...:
string):Tree Creates a new tree object.
- Parameters:
... (
string) – The data field names, in order
-
SetData(self:
Tree, node:number, fieldName:string, value:any) Sets a data field for a node.
- Parameters:
node (
number) – The nodefieldName (
string) – The name of the field to setvalue (
any) – The data field value
-
GetData(self:
Tree, node:number, fieldName:string): (...:any) Gets the data stored for the specified node.
- Parameters:
node (
number) – The nodefieldName (
string) – The name of the field to get
-
Insert(self:
Tree, parent?:number, ...:any):number Inserts a node into the tree
- Parameters:
parent? (
number) – The parent node... (
any) – The data fields for the node
-
MoveUp(self:
Tree, node:number) Moves a node up in the tree, replacing its parent.
- Parameters:
node (
number) – The node to move up
-
GetNumChildren(self:
Tree, node:number):number Gets the number of children.
- Parameters:
node (
number) – The node
-
GetChildren(self:
Tree, node:number): (...:number) Gets all the children of a node.
- Parameters:
node (
number) – The node- Returns:
... (
number) – The children
-
SetChildren(self:
Tree, node:number, ...:number) Sets the children for a given node.
- Parameters:
node (
number) – The node... (
number) – The new children
-
ChildrenIterator(self:
Tree, node:number): fun():number,table,number Iterates over all the children of a node.
- Parameters:
node (
number) – The node- Returns:
_1 (fun():
number) – Iterator with fields: child
-
staticmethod Create(...: