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 node

  • fieldName (string) – The name of the field to set

  • value (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 node

  • fieldName (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

GetRoot(self: Tree): number

Gets the root node.

GetParent(self: Tree, node: number): number?

Gets the parent node.

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

RemoveAllChildren(self: Tree, node: number)

Removes all children for a given node.

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

DepthFirstIterator(self: Tree, rootNode?: number): fun(): number, table

Iterates over the tree in depth-first order.

Parameters:

rootNode? (number) – The rootNode to iterate from (defaults to the tree’s root node)

Returns:

_1 (fun(): number) – Iterator with fields: node

GetDepth(self: Tree, node: number): number

Gets the depth of a node in the tree.

Wipe(self: Tree)

Wipes the tree.

Dump(self: Tree, rootNode?: number)

Prints out the tree for debugging.

Parameters:

rootNode? (number) – The root node to dump from