---!strict
type Meta = typeof(setmetatable({}, {}));
local Node = {}
local BinaryTree = {}
BinaryTree.__index = BinaryTree
Node.__index = Node
function Node.new(Value) : Meta
return setmetatable({
Data = Value;
LeftNode = nil;
RightNode = nil;
}, Node)
end
function BinaryTree.new(Value) : Meta
return setmetatable({
Root = Node.new(Value);
}, BinaryTree)
end
function BinaryTree:Insert(Value) : nil
local CurrentNode : Meta = self.Root
local function Foo() : nil
if CurrentNode.LeftNode and CurrentNode.RightNode then
if CurrentNode.LeftNode.LeftNode and CurrentNode.LeftNode.RightNode then
CurrentNode = CurrentNode.RightNode;
Foo();
else
CurrentNode = CurrentNode.LeftNode;
Foo();
end
elseif not CurrentNode.LeftNode then
CurrentNode.LeftNode = Node.new(Value);
else
CurrentNode.RightNode = Node.new(Value);
end
return;
end
Foo();
return;
end
function BinaryTree:DisplayTree() : nil
print(self)
return;
end
return BinaryTree