-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiway_tree_node.lua
More file actions
70 lines (60 loc) · 1.44 KB
/
multiway_tree_node.lua
File metadata and controls
70 lines (60 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
--[[
MultiwayTreeNode class
Node for MultiwayTree. Has a reference to its tree
and a table for children.
]]
local MultiwayTreeNode = {}
MultiwayTreeNode.__index = MultiwayTreeNode
local children_mt = {
__tostring = function(children)
local items = {}
for i = 1, #children do
items[#items+1] = tostring(children[i])
end
return "[" .. table.concat(items, ", ") .. "]"
end
}
-- new function
function MultiwayTreeNode:new(item)
return MultiwayTreeNode:init(item)
end
-- init function
function MultiwayTreeNode:init(item)
local obj = {}
setmetatable(obj, MultiwayTreeNode)
obj._tree = nil
obj._item = item
obj._parent = nil
obj._children = {}
setmetatable(obj._children, children_mt)
return obj
end
--[[
tostring(x) method
Represents a Multiway Tree Node as a string
]]
function MultiwayTreeNode:__tostring()
return tostring(self._item)
end
--[[
x:children() method
Returns a table of current node's children
]]
function MultiwayTreeNode:children()
return self._children
end
--[[
total_children() method
Gets total amount of children that this
node and its children have using recursion
]]
function MultiwayTreeNode:total_children()
local count = 0
for i = 1, #self._children do
local child = self._children[i]
count = count + 1
count = count + self.total_children(child)
end
return count
end
return MultiwayTreeNode