```

```

Fall 2009

Instructions

1. Write your name clearly at the top of the exam

2. You must answer all the questions on the exam. The exam has 9 problems. Make sure that you have all 9 problems.

3. Write your answers on the exam pages and hand in the exam when you are finished.

4. You have until the end of the class period to finish the exam. When I call time you must immediately drop your pen/pencil and hand in your exam.

5. Ensure that you write clearly and legibly. Failure to do so may result in the deduction of points.

6. The exam is open note, open book, closed neighbor.

7. Good luck!

```

```
1. (12 points) For each of the following questions choose the best answer from the below list. You may have to use the same answer for more than one question:
``` O(1)			    O(log n)	            O(n)
O(n log n)		    O(n2)	    AVL tree
Splay tree	            Binary search tree      Hash table
Open addressing	    Separate chaining       Linear probing
Hash function              Hash Table Size	    Rotation
Collision		    Hash function           Even number
2n-1            Prime number            2n
```
• _______________ The average case time to find an item in a hash table

• _______________ The worst case time to find an item in an unbalanced binary search tree

• _______________ The fundamental operation used to re-balance an AVL tree

• _______________ A good tablesize to choose for a hash table

• _______________ A balanced tree scheme that guarantees that all insertions and finds will require O(log n) time

• _______________ A hashing scheme that uses lists to handle collisions

2. (4 points) For the following problem, give a one line statement that achieves the desired outcome: Multiply an integer, n, by 2 and assign the result to n. Your answer must use a bit expression, not multiplication.
```

```
3. (10 points) Show the hash table that results if the integers 22, 28, 17, 21, 30 are inserted into the following hash table using:
• Linear probing, and
• the hash function h(k) = k % 7
```           -----------------------
0    |                     |
|                     |
-----------------------
1	   |                     |
|                     |
-----------------------
2    |                     |
|                     |
-----------------------
3	   |                     |
|                     |
-----------------------
4	   |                     |
|                     |
-----------------------
5	   |                     |
|                     |
-----------------------
6	   |                     |
|                     |
-----------------------
```
4. (10 points) Show the hash table that results if the integers 22, 28, 17, 21, 32 are inserted into the following hash table using:
• separate chaining, and
• the hash function h(k) = k % 5
You should append integers to the end of your lists.
```           -----------------------
0    |                     |
|                     |
-----------------------
1	   |                     |
|                     |
-----------------------
2    |                     |
|                     |
-----------------------
3	   |                     |
|                     |
-----------------------
4	   |                     |
|                     |
-----------------------

```

5. (10 points) Show the binary search tree that results if 150 is deleted from the tree below:
```                           ---100---
/         \
50        -150-
/         /     \
25       125    -300-
/	    \
175	    400
\
250
/   \
200   275

```

6. (6 points) Show the result of doing a single right rotation about the node 175. Do not worry if the rotation increases the height of the tree. All I care about is whether you know how to perform a rotation.
```                            -300-
/     \
175	 400
\
250
/   \
200   275

```
7. (14 points) 12 has just been inserted into the following AVL tree, causing it to violate the AVL condition:
```                          20
/    \
10     40
/    \
8     16
/
12
```
• Identify the bottom-most node that violates the AVL condition and explain why that node violates the AVL condition.
```

```

• In order to rebalance the tree do we have to use the zig-zig case or the zig-zag case? Justify your answer.
```

```

• Use the proper rotation(s) to rebalance the above tree so that it becomes a legitimate AVL tree.
```

```

8. (12 points) Suppose you are given the following heap in array format:
```       14 19 16 26 21 19 68 65 31 32 44 23
```
1. Draw the heap represented by this array (i.e., draw the tree representation).
```

```
2. Show the heap that results from inserting 15. If you want partial credit then show your intermediate steps. Please draw the resulting heap as a tree.
```

```
3. Show the heap that results from performing a deleteMin on the original heap. If you want partial credit then show your intermediate steps. Please draw the resulting heap as a tree.
```

```

9. (25 points) A binary expression tree is one in which the interior nodes are operators, such as + or -, and the leaves are numbers. For example:
```                     +
/     \
-       +
/   \    / \
-     4  8  10
/ \
16  5
```
This expression tree represents the arithmetic expression:
```     ((16-5) - 4) + (8+10)
```
which evaluates to 25. An expression tree can be evaluated by evaluating the left subtree, then the right subtree, and finally performing the indicated operation on the values returned by evaluating the left and right subtrees. A leaf evaluates to its number. For example, in the above expression tree, the + node with the leaves 8 and 10 evaluates to 18 since its left subtree evaluates to 8 and its right subtree evaluates to 10. Similarly the root + node evaluates to 25 because its left subtree evaluates to 7 and its right subtree to 18.

Your task is to write a function named evaluate that takes a binary tree node as a parameter and returns the integer value of its subtree. If the node is a leaf, the function will return the number associated with the leaf. Otherwise the function will evaluate the left and right subtrees, perform the operation indicated by the node on the results returned by the left and right subtrees, and return this value. A binary tree node has the following declaration:

```       typedef struct bnode {
int type;   // 0 for a leaf, 1 for an operator node
union {
char operator;  // can be either '-' or '+'
int number;
};
struct bnode *left;
struct bnode *right;
} BNode;
```
If the type field indicates that the node is a leaf, then the number field in the union will be set. If the type field indicates that the node is an operator node, then the operator field in the union will be set.