







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2011;
Typology: Exams
1 / 13
This page cannot be seen from the preview
Don't miss anything!








75 minutes permitted
Print your name, netID, and lab section day/time neatly in the space provided below; print your name at the upper right corner of every page.
(a) What was the “problem” with using path compression and union-by-height together? That is, what difficulty does using the two techniques together present? Please be specific. (The word “problem” is in quotes because we said it turned out that this “problem” didn’t actually affect things too badly, even if it seems like it would.)
(b) If you have a complete tree of 17 nodes, how many nodes are on the deepest level?
(c) Insert the integers 1 through 6 , in that order, into an AVL tree. Draw the resulting tree. How many rotation operations, total, did you perform? Count a “double rotation” operation as one rotation operation.
(c) After performing a combine operation during B-Tree removal, why is it that we need to check the parent for underflow? i.e. justify that such a combine operation could have caused the parent to underflow.
(d) Explain why we can implement a complete tree using an array – that is, explain why we don’t lose information when we get rid of the pointers, i.e. explain why it is that, given an array, we can always produce the corresponding complete tree.
(e) Explain the “repair case” of the Red-Black Tree removal algorithm (the “repair case” was case 2b, where the node we labelled “x” had a black sibling and that black sibling had a red child in the child position further from “x’). That is, explain what we do in this case and justify that it fixes the problems we have without causing new ones.
(b) Explain why it is that the rebalancing work performed by the AVL tree insert or remove is at most O(lg n) on a tree of height O(lg n). Your answer should be detailed enough to convince us you know what you are talking about. You don’t need to justify the steps of the algorithm here – simply indicate what those steps are and their running times – and indicate that those running times add up to what we claim they add up to.
You have the following two standard node classes (which are publicly accessible and not encapsulated in another class):
class ListNode { public: int element; ListNode* next; };
class TreeNode { public: int element; TreeNode* left; TreeNode* right; };
Write a function LevelOrderToTree. The function should take as parameter a pointer to a ListNode, which is the first element of a list that represents the level-order traversal of a perfect binary tree. This function should reproduce the binary tree from the level-order listing received as a parameter. That is, LevelOrderToTree should return a TreeNode pointer which will be the root of a perfect binary tree such that, if a level-order traversal is run on it, it will yield the same listing as the one received as parameter. If the parameter ListNode pointer is NULL, the the returned TreeNode pointer should also be NULL. You have one Queue available to you to use as a local variable, if you wish.
TreeNode* LevelOrderToTree(ListNode* head) { // your code goes here
You have the following node class available to you, which is publicly accessible and not encapsulated in another class:
class TreeNode { public: int element; TreeNode* left; TreeNode* right; };
Write a function CountLeaves that takes as a parameter, a pointer to a TreeNode, and returns the number of leaves in the tree whose root is that TreeNode. (Hint: Use recursion)
int CountLeaves(TreeNode* ptr) { // your code goes here
(Counting Leaves, continued)
(scratch paper)