Sample Exam 2 Problems - Data Structures | CS 225, Exams of Data Structures and Algorithms

Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2011;

Typology: Exams

2010/2011

Uploaded on 06/14/2011

koofers-user-kuc
koofers-user-kuc 🇺🇸

4.7

(3)

7 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Illinois at Urbana-Champaign
Department of Computer Science
Second Examination
CS 225 Data Structures and Software Principles
Sample Exam 2
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.
Name:
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. In addition, you are not allowed to use any
electronic aides of any kind.
Do all 5 problems in this booklet. Read each question very carefully.
You should have 7 sheets total (the cover sheet, plus numbered pages 1-12). The last sheet is
scratch paper; you may detach it while taking the exam, but must turn it in with the exam
when you leave.
Unless otherwise stated in a problem, assume the best possible design of a particular imple-
mentation is being used.
Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any
compiler error is an exam typo (though hopefully there are not any typos), and (2) assume
you are NOT allowed to write any helper methods to help solve the problem, nor are you
allowed to use additional arrays, lists, or other collection data structures unless we have said
you can.
Problem Points Score Grader
1 12
2 30
3 18
4 15
5 15
Total 90
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Sample Exam 2 Problems - Data Structures | CS 225 and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of Illinois at Urbana-Champaign

Department of Computer Science

Second Examination

CS 225 Data Structures and Software Principles

Sample Exam 2

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.

Name:

NetID:

Lab Section (Day/Time):

  • This is a closed book and closed notes exam. In addition, you are not allowed to use any electronic aides of any kind.
  • Do all 5 problems in this booklet. Read each question very carefully.
  • You should have 7 sheets total (the cover sheet, plus numbered pages 1-12). The last sheet is scratch paper; you may detach it while taking the exam, but must turn it in with the exam when you leave.
  • Unless otherwise stated in a problem, assume the best possible design of a particular imple- mentation is being used.
  • Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), and (2) assume you are NOT allowed to write any helper methods to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can.

Problem Points Score Grader

Total 90

  1. [Short Answer – 12 points (4 points each)].

(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.

  1. [List to tree – 15 points].

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

  1. [Counting Leaves – 15 points].

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)