You are on page 1of 8

School of Economics

INFORMATION TECHNOLOGY
EXAM TITLE PAGE

To be filled in by the student:

Student Surname:
Student Firstname: Studentnumber: Group: To be filled in by the lecturer:

Exam for group(s): Exam Code:


Exam Title: Period: Date of Exam:

3 IT ALGDS01E09 Algorithms & Data Structures 1 1

28 January 2011 Time of Exam: Number of assigned questions: Number of pages: (titlepage excluded) Additional means to be allowed: Remarks / Particulars: Name of lecturer: start: 13:30 4 7 none Return all Exam papers: yes Mr. Gerard Koetsier finish: 15:30

Question 1. Binary Search Trees Given is a class BSTree, with an inner class TNode: public class BSTree { private TNode root; // constructors and several methods like insert, delete etc. follows here .. // inner class private class TNode { int data; TNode left, right; // constructor public TNode(int d, TNode l, TNode r) { data = d; left = l; right = r; } } } a) A non-recursive version of the insert method is given below, in pseudocode: public void insert(int value) { if ( < tree is empty > ) < create new node as the root > else { TNode parent = null; TNode current = root; while ( < current node exists > ) { < go left or right, depending on value, and update parent and value > } < create new node and attach to parent in the appropriate way > } } Implement this non-recursive method

b) Write a method swap that swaps the left and right child of a node. c) Give the recursive implementation of a method reflectTree that swaps every left and right child, so the result is a tree that is the mirror image of the original tree (reflected in a vertical axis through the root). see for answering sheet next page

Answer 1 a) [3 points] public void insert(int value) {

} b) [1 points] public void swap(TNode node) {

} c) [2 points] public void reflectTree(TNode curRoot ) {

Question 2. Red-Black trees a) Suppose that in a given RBT the minimum length of a path from root to leave is L min. Explain carefully why the maximum length of a path from root to leaf is at most 2 * Lmin + 1. b) Below you see a part of a (red-black) tree.

Draw the result after performing a right rotation around the node Y. c) After having inserted a (red) node C in a RBT, we have the following situation: - the grandparent node G is black - the parent P is red, and P is a left child of G - the child C is red, and C is a right child of P. Describe carefully the actions you have to perform in order to restore the tree into a correct RBT. Use pictures! a) [1 point]

b) [1 point]

c) [2 points]

Question 3. Heaps Given is (a part of) the class HeapArray below public class HeapArray { private final int MAX_SIZE; private int curSize; private int[] hpa; // heap will be implemented as an array public HeapArray(int maxSize) { MAX_SIZE = maxSize; hpa = new int[MAX_SIZE]; curSize = 0; } public void insert(int value) { // some code } public int remove() { // some code } } a) What is the time-complexity (i.e. how many steps do you need in the worst case) for searching (not deleting) the maximum (key-)value in a heap consisting of N nodes? And the minimum (key-)value? Give a clear explanation. b) Assuming that the methods insert and remove have been implemented, write a method heapSort that takes an unordered array as argument and sort this array by using a heap-object of type HeapArray. c) Explain why the heapSort algorithm in b) has time complexity O(n.lg(n)), given that the original array consists of n elements.

a) [1 point]

b) [3 points] public void heapSort(int[] a) {

} c) [2 points]

Question 4. Hash tables a) Given is a hash table t[0], t[1], ....., t[10] and a hash function h(x) = x % 11 . Fill the table with the following values: 15, 28, 37, 17 and 18. Use open addressing with double hashing. The secondary hash function is k(x) = 5 (x % 5). b) With double hashing the table size should be a prime number. Why? Answer 4 a) [2 points]

b) [1 point]

grade:

(#points +1)/2

You might also like