-
Notifications
You must be signed in to change notification settings - Fork 17
/
bintree.lua
146 lines (126 loc) · 3.27 KB
/
bintree.lua
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- bintree.lua
-- Class representing the binary tree
local Bintree = {}
Bintree.__index = Bintree
function Bintree.new(data, left, right)
local node = {
data = data,
left = left,
right = right,
}
return setmetatable(node,Bintree)
end
function Bintree:addLeft(child)
if self ~= nil then
self.left = child
return self.left
end
end
function Bintree:addRight(child)
if self ~= nil then
self.right = child
return self.right
end
end
function Bintree:find(data)
if data == self.data then
return self
end
local output = nil
if type(self.left) == "table" then
output = self.left:find(data)
end
if type(self.right) == "table" then
output = output or self.right:find(data) or nil
end
return output
end
-- remove leaf and replace parent by sibling
function Bintree:removeLeaf(data)
local output = nil
if self.left ~= nil then
if self.left.data == data then
local newSelf = {
data = self.right.data,
left = self.right.left,
right = self.right.right
}
self.data = newSelf.data
self.left = newSelf.left
self.right = newSelf.right
return true
else
output = self.left:removeLeaf(data) or nil
end
end
if self.right ~= nil then
if self.right.data == data then
local newSelf = {
data = self.left.data,
left = self.left.left,
right = self.left.right
}
self.data = newSelf.data
self.left = newSelf.left
self.right = newSelf.right
return true
else
return output or self.right:removeLeaf(data) or nil
end
end
end
function Bintree:getSibling(data)
if data == self.data then
return nil
end
local output = nil
if type(self.left) == "table" then
if self.left.data == data then
return self.right
end
output = self.left:getSibling(data) or nil
end
if type(self.right) == "table" then
if self.right.data == data then
return self.left
end
output = output or self.right:getSibling(data) or nil
end
return output or nil
end
function Bintree:getParent(data)
local output = nil
if type(self.left) == "table" then
if self.left.data == data then
return self
end
output = self.left:getParent(data) or nil
end
if type(self.right) == "table" then
if self.right.data == data then
return self
end
return output or self.right:getParent(data) or self
end
end
function Bintree:swapLeaves(data1, data2)
local leaf1 = self:find(data1)
local leaf2 = self:find(data2)
local temp = nil
if leaf1 and leaf2 then
temp = leaf1.data
leaf1.data = leaf2.data
leaf2.data = temp
end
end
function Bintree.show(node, level)
if level == nil then
level = 0
end
if node ~= nil then
print(string.rep(" ", level) .. "Node[" .. node.data .. "]")
Bintree.show(node.left, level + 1)
Bintree.show(node.right, level + 1)
end
end
return Bintree