Answered step by step
Verified Expert Solution
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 AVLtree 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:
Subquestion Description
QA
Command: showchildren
Parameters: letter
Behaviour: The system must read a letter from the standard input stream and output all of the
nodes which are children or grandchildren or great grandchildren 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
QB
Command: insert
Parameters: 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:: attempt to call global 'showChildren' a nil value
stack traceback:
main.lua:: in main chunk
C:
Erro two when calling insert function
Enter command: insert
Enter Value to Insert: Z
lua: main.lua:: attempt to index local 'avlTree' a nil value
stack traceback:
main.lua:: in function 'insert'
main.lua:: in main chunk
C:
function loadgridfilename
local file ioopenfilename r
if not file then
printError: unable to open file"
return
end
local grid
for line in file:lines do
local row
for num in line:gmatchS do
table.insertrow tonumbernum
end
table.insertgrid row
end
file:close
return grid
end
function readGraphfilename avlTree
local file ioopengraphtxtr
if file then
for line in file:lines do
local value line:matcha
if value then
avlTree:insertvalue
end
end
file:close
else
printError could not open file name filename
end
end
check if the cities are neighbours on the grid
function areneighbourscity city grid
if city and city #grid and city and city #gridcity then
return gridcitycity
else
return false
end
end
check if the set of cities are connected by a valid path
function ispathgrid cities
local cities
for i #cities do
if gridcitiesicitiesi then
return No
end
end
return "Yes"
end
Function to show children
function showchildrenavlTree nodeValue
local children avlTree:getChildrennodevalue
if #children then
table.sortchildren
printtableconcatchildren
else
printNone
end
end
Function to insert into tree
function insertavlTree value
avlTree:insertvalue
Perform first traversal
local queue avlTreeroot
local result
while #queue do
local currentNode table.removequeue
table.insertresult currentNode.value
if currentNode.left then
table.insertqueue currentNode.left
end
if currentNode.right then
table.insertqueue currentNode.right
end
end
printtableconcatresult
end
AVLNode
value nil,
left nil,
right nil,
height
AVLTree
root nil
Main Program
local grid loadgridgridtxt
readGraphgraphtxt avlTree this errors if i leave this uncommented.
Standard input commands
while true do
iowriteEnter command:
local command ioread
if command "areneighbours" then
local city city ioreadnn
local result areneighbourscity city grid and "Yes" or No
printresult
elseif command "ispath" then
local cities
for city in ioread:gmatchS do
table.insertcities tonumbercity
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started