Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i was provided this solution based on this question previously posed but i am getting these two errors are you able to assist me to

i was provided this solution based on this question previously posed but i am getting these two errors
are you able to assist me to recitfy these errors below using the file below.
Extend your program in main.lua to also do the following:
Opens a text file called graph.txt which contains values to be inserted in an AVL-tree data
structure in the following format
o Value (on a single line)
o For example the file defining the tree above contains the following:
A
E
I
O
U
B
C
D
You may assume that values are single letters and that there are no duplicates
Add support for the following two commands to your system:
Sub-question Description
Q2-A
Command: showchildren
Parameters: 1 letter
Behaviour: The system must read a letter from the standard input stream and output all of the
nodes which are children (or grand-children or great grand-children etc.) of that node (in alphabetic
order). If there are no children then None must be output.
Expected output on test data with input (B): A C D
Expected output on test data with input (C): D
Expected output on test data with input (O): I U
Expected output on test data with input (A): None
Q2-B
Command: insert
Parameters: 1 letter
Behaviour: The system must read a letter from the standard input stream, insert that value into the
tree, restore the AVL property, then output the nodes in the order they would be reached by a
breadth first traversal
Expected output on test data with input (Z):
E
B O
A C I U
D Z
Error one when calling showChildren function
Enter command: showchildren
Enter Node Value: a A
lua: main.lua:130: attempt to call global 'showChildren' (a nil value)
stack traceback:
main.lua:130: in main chunk
[C]: ?
Erro two when calling insert function
Enter command: insert
Enter Value to Insert: Z
lua: main.lua:75: attempt to index local 'avlTree' (a nil value)
stack traceback:
main.lua:75: in function 'insert'
main.lua:134: in main chunk
[C]: ?
function loadgrid(filename)
local file = io.open(filename ,"r")
if not file then
print("Error: unable to open file")
return
end
local grid ={}
for line in file:lines() do
local row ={}
for num in line:gmatch("%S+") do
table.insert(row, tonumber(num))
end
table.insert(grid, row)
end
file:close()
return grid
end
function readGraph(filename, avlTree)
local file = io.open('graph.txt',"r")
if file then
for line in file:lines() do
local value = line:match("%a")
if value then
avlTree:insert(value)
end
end
file:close()
else
print("Error could not open file name ".. filename)
end
end
--check if the cities are neighbours on the grid
function areneighbours(city1, city2, grid)
if city1>=0 and city1< #grid and city2>=0 and city2< #grid[city1+1] then
return grid[city1+1][city2+1]>0
else
return false
end
end
--check if the set of cities are connected by a valid path
function ispath(grid , cities)
-- local cities {}
for i =1, #cities -1 do
if grid[cities[i]][cities[i+1]]==0 then
return "No"
end
end
return "Yes"
end
--Function to show children
function showchildren(avlTree, nodeValue)
local children = avlTree:getChildren(nodevalue)
if #children >0 then
table.sort(children)
print(table.concat(children,""))
else
print("None")
end
end
--Function to insert into tree
function insert(avlTree, value)
avlTree:insert(value)
--Perform first traversal
local queue ={avlTree.root}
local result ={}
while #queue >0 do
local currentNode = table.remove(queue,1)
table.insert(result, currentNode.value)
if currentNode.left then
table.insert(queue, currentNode.left)
end
if currentNode.right then
table.insert(queue, currentNode.right)
end
end
print(table.concat(result,""))
end
AVLNode ={
value = nil,
left = nil,
right = nil,
height =1
}
AVLTree ={
root = nil
}
--Main Program
local grid = loadgrid("grid.txt")
--readGraph("graph.txt", avlTree)-- this errors if i leave this uncommented.
--Standard input commands
while true do
io.write("Enter command: ")
local command = io.read()
if command =="areneighbours" then
local city1, city2= io.read("*n","*n")
local result = areneighbours(city1, city2, grid) and "Yes" or "No"
print(result)
elseif command =="ispath" then
local cities ={}
for city in io.read():gmatch("%S+") do
table.insert(cities, tonumber(city))
end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions