Docsity

Binary Heap - Data Structures - Exams, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
University of California at Berkeley
Dept. of Electrical Engineering and Computer Sciences
Computer Science Division
Spring 2000 Jonathan Shewchuk
CS61B: Midterm Exam II
This is an open book, open notes exam. Do not open your exam until you are told to do so!
Name:_____________________________
Login:_____________________________
Lab TA:___________________________
Lab day and time:____________________
Do not write in these boxes.
Problem # Possible Score
1. Quickies 7
2. Data Structures 6
3. Merging Trees 4
4. Heap Insertion 8
Total 25
Name:_____________________________________________ Login:
______________________________ [Page 2]
Problem 1. (7 points) Quickies.
a. (1 point) What kind of tree traversal does the minimax algorithm use? ________________________
b. (1 point) What is the maximum height of an n-node binary search tree? _______________________
c. (1 point) Can the minimax algorithm with alpha-beta pruning produce a different answer than minimax
without pruning? _______________
d. (2 points) In the following method, let x be the length of xArray, y be the length of yArray, and z be
the length of zArray. This method's running time is theta(_________________)
public void futz(int[] xArray, int[] yArray, int[] zArray) {
for (int i = 0; i < xArray.length; i++) {
for (int j = 0; j < zArray.length; j++) {
for (int k = 0; k < i; k++) {
yArray[j] = yArray[j] + zArray[j] * (xArray[j] * (xArray[i] - xArray[k]);
}
}
for (int j = 0; j < yArray.length; j++) {
yArray[j] = 2 * yArray[j];
Spring 2000 CS61B Exam II
CS61B: Midterm Exam II 1
pf3
pf4
pf5

Partial preview of the text

Download Binary Heap - Data Structures - Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of California at Berkeley Dept. of Electrical Engineering and Computer Sciences Computer Science Division Spring 2000 Jonathan Shewchuk CS61B: Midterm Exam II

This is an open book, open notes exam. Do not open your exam until you are told to do so!

Name:_____________________________

Login:_____________________________

Lab TA:___________________________

Lab day and time:____________________

Do not write in these boxes.

Problem # Possible Score

  1. Quickies 7
  2. Data Structures 6
  3. Merging Trees 4
  4. Heap Insertion 8 Total 25

Name:_____________________________________________ Login: ______________________________ [Page 2]

Problem 1. (7 points) Quickies.

a. (1 point) What kind of tree traversal does the minimax algorithm use? ________________________

b. (1 point) What is the maximum height of an n -node binary search tree? _______________________

c. (1 point) Can the minimax algorithm with alpha-beta pruning produce a different answer than minimax without pruning? _______________

d. (2 points) In the following method, let x be the length of xArray, y be the length of yArray, and z be the length of zArray. This method's running time is theta (_________________)

public void futz(int[] xArray, int[] yArray, int[] zArray) { for (int i = 0; i < xArray.length; i++) { for (int j = 0; j < zArray.length; j++) { for (int k = 0; k < i; k++) { yArray[j] = yArray[j] + zArray[j] * (xArray[j] * (xArray[i] - xArra } } for (int j = 0; j < yArray.length; j++) { yArray[j] = 2 * yArray[j];

e. (1 point) Suppose you own a collection of books, and you catalogue them with some data structure(s) of your choice. You may spend as much time generating the catalogue as you like. But when you buy a new book, you want to find out as quickly as possible whether or not you own any other book by the same author or by the same publisher. Describe the data structure(s) you would choose.

f. (1 point) The method volatileMethod() is declared "throws MyException". The exceptions MyException and OddException both extend Exception. What exceptions, if any, must be added with a "throws" clause in the prototype for the yut() method below?

public void yut() { try { volatileMethod(); } catch (Throwable t) { throw new OddException(); } finally { throw new NullPointerException(); } }

Name:_____________________________________________ Login: ______________________________ [Page 3]

Problem 2. (6 points) Data Structures.

a. (2 points) What does the following binary heap look like after removeMinElement()? (fill the second table)

X 1 4 3 6 4 11 8 12 7

X

b. (2 points) What does the following 2-3-4 tree look like after remove(1)?

Let R and S be two binary search trees, and suppose that every key in R is less than every key in S. We want to merge R and S into a single binary search tree T (so that T will contain all the items that were previously in R and S). The final tree T must satisfy the binary search tree invariant. It is acceptable to destroy R and S to create T.

Describe (in English, not code) a simple algorithm for performing this merge. You may use operations described in class, and you may invent your own. For full credit, your algorithm should run in O( n ) time, where n is the number of items in T. Your answer will also be graded on correctness, speed and shortness. (Our answer is 28 words long; please keep yours as short as possible.)

Name:_____________________________________________ Login: ______________________________ [Page 5]

Problem 4. (8 points) Inserting a Key into a Binary Heap.

Write a method called insertItem in the IntHeap class below. An IntHeap is a binary heap that stores only keys (no associated elements), and those keys are ints. Remember that the int at index zero is not used, and that key i 's parent is floor ( i /2) and its children are 2 i and 2 i + 1. Assume that the heap will never grow larger than 100 items, so there's no need ever to resize the array. Your solution should not call any other method. Our solution is sever lines long.

public class IntHeap { int[] heapArray; int size;

public IntHeap() {

heapArray = new int[101]; size = 0; }

public void insertItem(int key) {

Posted by HKN (Electrical Engineering and Computer Science Honor Society)

University of California at Berkeley

If you have any questions about these online exams

please contact [email protected].

Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley If y 5