NAME: ______________________________ STUDENT ID: ________________ COMP 250 – Midterm #2. October 31st 2007 This exam has 5 pages. This is an open book exam. No electronic equipment is allowed. 1) Questions with short answers (30 points; 5 points each) a) What would be the result of the partition algorithm on the following array? 5 2 8 4 7 2 6 b) In a hash table, why is it bad to use a hash function that would map all possible keys into the same bucket? c) Consider a sorted linked list containing n nodes, each storing an integer. On such a list, why is it not possible to do a binary search that runs in worst-case time O(log n)? d) Name the Abstract Data Type would be the most appropriate to represent each of the following situations? No justifications are needed. 1) A computer stores a collection of files, each with its own name. 2) A student has a collection of assignments to work on for different classes. He starts with the assignment with the earliest due date. e) What is the maximal number of nodes that can be contained in a binary tree of height h? Justify your answer. 2) Tree traversal algorithms (20 points) What would the following algorithm print when executed on the root of the tree shown on the right? No justification is needed. Algorithm WeirdTraversal(treeNode n) if (n!= null) then if (n.key is even) then WeirdTraversal( n.leftChild ) WeirdTraversal( n.rightChild ) Else WeirdTraversal( n.rightChild ) WeirdTraversal( n.leftChild ) Question 3. Heaps (20 points) Consider the following heap. a) Draw the Heap after the insert(4) operation has been performed, as seen in class. b) Draw the Heap after the removeMin() operation has been performed on the original Heap (not the one you got in (a)). Question 4 (20 points) Consider a binary tree in which each node stores an integer key. Write an algorithm to verify that this tree satisfies the requirements of a valid Binary Search Tree. More specifically, your algorithm should return TRUE when called on the root of a binary tree that satisfies these requirements, and FALSE otherwise. Algorithm isBinarySearchTree(treeNode n) Input: A treeNode n Output: TRUE if n is the root of a valid binary search tree. FALSE otherwise /* WRITE YOUR PSEUDOCODE HERE */ Question 5 (20 points). Java implementation of Binary Search Trees Write the “insert” method in the following Binary Search Tree Java class, in which keys are integers. Assume that the method will never be called on an empty tree. The item inserted should go to the proper location of the tree. Your program must be written in Java, not pseudocode! Class BSTNode { public int key; public Object value; public BSTNode leftChild, rightChild; public BSTNode parent; /* Constructor */ public BSTNode(int k, Object o, BSTNode l, BSTNode r, BSTNode p) { key = k; value = o; leftChild = l; rightChild = r; parent = p; } /* Insertion into a binary search tree */ /* Arguments: n: the root of the binary search tree. Assume that n is never null. k: the key of the object to be added o: the information associated to the key */ public void insert( BSTNode n, int k, Object o) { /* WRITE YOUR INSERT METHOD HERE */ } } This page was left blank intentionally.