Professional Documents
Culture Documents
• Answer all the questions on the question paper based on the algorithms seen in class. Make
sure your student number appears clearly on all booklets if you use a second one.
• Answer in pen.
Student Number:
Name:
ID Number:
Number of booklets:
1
Question 1 Binary Search Trees [33 Marks]
1. Define the height of a tree. [1]
2. In the best case what is the height of a binary search tree with n items? Give your answer in
Big-O notation. [1]
3. In the worst case what is the exact height of a binary search tree with n items? [1]
4. Given a binary tree with n items, how many edges are there in total? [1]
5. Draw a binary tree of height 3 that is complete but neither perfect nor full. [3]
6. The following numbers should be inserted into a binary search tree: 0,1,2,3,4,5,6. Ar-
range the numbers in order so that when inserted into an empty BST we achieve a search tree
with the best case height. [1]
2
7. Create a binary search tree using the following data. Insert the items into the tree in the order
that they appear in the list below: i.e. t.insert(42), t.insert(30)... Draw only
the final tree.
42,30,12,80,20,45,90,42 [6]
3
53
42 80
21 53 94
37
11. List the nodes that are visited when searching for the number 65 in the tree in Figure 1. [2]
12. Provide any number, that when inserted into Figure 1 causes the least amount of work. [1]
13. Provide any number, that when inserted into Figure 1 causes the most amount of work. [1]
14. Which searching for a number that is inside the tree, which number results in the least amount
of work? [1]
4
50
40 60
32 45 55 70
35 42 57 65
17. Delete node 40 from the BST in Figure 2. Draw the resulting tree below. [4]
5
Question 2 Balanced Trees [24 Marks]
1. If an AVL tree has n nodes, in Big-O notation, what is its height? [1]
2. What benefit does an AVL tree provide over a normal Binary Search Tree? [1]
B C
D E
F G
4. Perform a single Left Rotation about node A in Figure 3. Draw the resulting tree below. [5]
6
5. Next to each node in Figure 4 below, write the height of that node. [5]
6. Above each node in Figure 4 below, indicate the balance of the node (balanced, left/right heavy,
double left/right heavy). Lines/Arrows are sufficient. [5]
5 9
7. Insert the number 3 into the tree in Figure 4 and perform the necessary rotation(s) to restore the
AVL property. Indicate about what node and in which direction you are rotating. If you need to
perform more than one rotation, then you must draw the intermediate tree. [6]
7
Question 3 Binary Heaps [13 Marks]
1. What is the min heap condition? [1]
2. What are the minimum number of leaves in a binary heap of height h? [1]
3. What are the maximum number of leaves in a binary heap of height h? [1]
4. Figure 5, on the next page, contains a min binary heap. Insert the number 3 and perform the
necessary steps to fix the heap condition. You must draw all intermediate trees. [5]
8
0
6 5
8 10 5 42
5. Based on the original heap in Figure 5, remove the minimum number and perform the necessary
steps to fix the heap condition. You must draw all intermediate trees and indicate what swaps
are taking place. [5]
9
Extra Space
10