For credit you must turn in written (or typed) solutions to these problems in person on or before 2:00 pm on Wednesday, March 1. Please make sure your name is on your work and that you've signed the honor code pledge.
Name ____________________ Login _________ Section ________________ Pledge _________________
There are four questions here, the first two are repeated from the test. If you do the questions that are repeated, your new version can replace what you did on the test. The last two questions will be added to the test. If you don't do these other questions your test score will be affected (for the worse).
You are expected to work without consulting any human. You can use books, notes, and programs from class but you cannot search the web or ask for assistance from any human or machine.
Write the function swapCopy that creates a new tree that's a swapped copy of its parameter and returns a pointer to the root of the new tree. In a swapped copy, nodes with two-children are copied by interchanging the copies of the left and right subtrees. Nodes with no children or one child are simply copied (no swapping).
For the tree above, the swapped copy is shown below.
Use the header below.
A tree S is a subset of another tree T if every value of S is contained in T. There may be values in T that are not in S, but every value stored in S must also stored in T for S to be a subset of T. The empty tree is a subset of every tree (there are no values in the empty tree not in another tree). For search trees, the function below correctly determines if a single value is contained in a tree.
Part A
Write the function subset that returns true if search tree s is a subset of search tree t, and returns false otherwise.
Hint: traverse s in any order calling contains
What is the complexity of the code you wrote in Part A. In answering the question assume s contains n nodes and t contains m nodes, and that trees are roughly balanced (average case). The answer should be in terms of both n and m. Justify your answer briefly.
The function height below correctly satisfies its postcondition, returning a string that represents the height of its parameter t. For example, for the tree below the function will return the string "RLLR"
Write a different function heightII that returns (via a reference parameter) a tvector of pointers so that for the original call of heightII, t[0] is the root, t[t.size()-1] is a deepest leaf, and t[k] is the child of t[k-1] on the path that represents the height from the root to a deepest leaf.
The picture below shows the vector returned for the tree shown.
For full credit your function should run in time O(N) for an N-node tree. You can call height and you can write other functions and call them.
For each node N in the tree returned, the size (number of nodes) in N's left subtree should be roughly the same as the size of N's right subtree.
For full credit your function should run in O(N) time for an N-node tree.