I was working on a binary tree and was testing to see if it was working.
I went in studio and it gave me an error which was something like “attempt to call a number value” and it was this block of code
local function insertbstnode(node, data)
if data < node:data() then -- here was the error so I switched it to (data < node.data) instead and it didn't throw me the error anymore
if node.left == nil then
node.left = Node.new(data)
return
elseif node.left ~= nil then
return insertbstnode(node.left, data) -- these functions I played around with a lot to no avail
end
elseif data >= node:data() then
if node.right == nil then
node.left = Node.new(data)
return
elseif node.right ~= nil then
return insertbstnode(node.right, data)
end
end
return insertbstnode(node)
end
Then when I went back to test it’s now giving me this error from requiring the module
local mod = require(script.Parent.ModuleScript)
local tree = mod.new()
tree:InsertNode(31) -- here is the source
attempt to call a nil value
I don’t know why it’s throwing me this error I thought it was something wrong with the module at first, so I went and changed the code but after 5 minutes of redundant changes I realized it’s(probably) not my code but it’s the script requiring the module.
So I made this function in the module
function Tree:Print()
print('printed')
end
and then tried to call it. It gave me the same error to my surprise. Then it told me that my module wasn’t recognized, even though it was clearly referenced. I wish I could’ve screenshotted the error it threw me but that is long gone.
I’m lost, I wanna know what the root(no pun intended) cause of this error is and I can’t seem to figure it out. Here is my code
-- Node Constructor
local Node = {}
Node.__index = Node
function Node.new(data)
local self = setmetatable({}, Node)
self.data = data
self.left = nil
self.right = nil
self.middle = nil -- if anyone asks why I have a middle value in a BST, just know I was working on a module to support different trees and started on the binary tree first
return self
end
function Node:data() -- ignore this
return self.data
end
-- Tree Constructor
local Tree = {}
Tree._index = Tree
function Tree.new()
local self = setmetatable({}, Tree)
self.root = nil
self.type = nil -- ignore this
return self
end
local function insertbstnode(node, data)
if data < node:data() then
if node.left == nil then
node.left = Node.new(data)
return
elseif node.left ~= nil then
return insertbstnode(node.left, data)
end
elseif data >= node:data() then
if node.right == nil then
node.left = Node.new(data)
return
elseif node.right ~= nil then
return insertbstnode(node.right, data)
end
end
return insertbstnode(node)
end
function Tree:InsertNode(data)
if self.root == nil then
local rootnode = Node.new(data)
self.root = rootnode
return self.root
end
self.root = insertbstnode(self.root, data)
end
return Tree
--[[
A side note:
since I am return the tree class and I do;
local tree = mod.new()
It's requiring the Tree constructor because if I were to put a parameter there
it gives me this;
Argument count mismatch. Function expects 0 arguments, but 1 is specified
]]
This is the main part of my code didn’t include the remove function cause well, I can’t implement it now can I?
Any help?