replacing yourusername with your CNet ID. Press p to accept the certificate at the prompt, and then enter your password.
You will need to create a lab8 directory in your repository and save in your lab8 directory the following file:
lab8.rkt, the Lab 8 Racket file.
lab8.rkt also defines a variable named world-tree which stores the most populous cities of the world organized in a hierarchy of continents, countries, provinces, and cities. Take a look at the Racket code for the variable and understand how it corresponds to the tree below. (Only Africa is shown in detail in the interest of space.)
Write the following two mutually recursive functions to search for a place name in a tree:
that takes in a tnode (like world-tree) and a place name, and returns true if the place is in the tree, and false if not. This function should make a call to search-forest which you will write as well.
takes in a list of tnodes (which we informally call a forest, since it is a collection of trees), and a place name, and returns true if the place is in at least one of the trees in the forest. This function should make a recursive call to itself, as well as to search-tree.
Save and commit your work for Part 1.(check-expect (search-tree world-tree "Nairobi") true) (check-expect (search-tree world-tree "United States") true) (check-expect (search-tree world-tree "South America") true) (check-expect (search-tree world-tree "Maharashtra") true) (check-expect (search-tree world-tree "Middle Earth") false)
(list "World" "Africa" "Kenya" "Nairobi Region" "Nairobi")
Write two mutually recursive functions to return the path from the root of the tree to the place if it exists in the tree, and the empty list if not. These functions will be similar in spirit to the ones you wrote in Part 1, and may need to call search-tree.
takes in a tnode and a place name, and returns the path from the tnode to that place if it exists, or empty if not.
takes in a list of tnodes and a place name as a string. If the place exists in the forest, this function should return the path from the appropriate tnode to that place. If the place does not exist under any of the tnodes, the function should return empty.
(check-expect (get-path-from-tree world-tree "Los Angeles") (list "World" "North America" "United States" "California" "Los Angeles")) (check-expect (get-path-from-tree world-tree "Colombia") (list "World" "South America" "Colombia")) (check-expect (get-path-from-tree world-tree "South America") (list "World" "South America")) (check-expect (get-path-from-tree world-tree "Bangalore") (list "World" "Asia" "India" "Karnataka" "Bangalore")) (check-expect (get-path-from-tree world-tree "Beijing") (list "World" "Asia" "China" "Beijing")) (check-expect (get-path-from-tree world-tree "Middle Earth") empty)
You are finished with the lab. Make sure to save and commit your work. As usual, you will have the opportunity to finish your lab for this week's homework.
Write a function
closest-ancestor : string string tnode-> string or false
that takes in two place names (represented as two strings) and a tree and returns the place (as a string) that is their nearest common ancestor in the input tree. If either node is not in the input tree, your function should return false.
Check-expects:
(check-expect (closest-ancestor "Los Angeles" "New York City" world-tree) "United States") (check-expect (closest-ancestor "Bogota" "Lima" world-tree) "South America") (check-expect (closest-ancestor "Seoul" "Sindh" world-tree) "Asia") (check-expect (closest-ancestor "Singapore" "Mexico City" world-tree) "World") (check-expect (closest-ancestor "Lima" "Middle Earth" world-tree) false)
Helpful tips
How to Check your SVN Submission