Problem 1. Binary Search Trees (36 points) a) (12 points) Assume that the following numbers are inserted into an (initially empty) binary search tree in the order shown below (from left to right): 42 36 15 11 4 59 82 8 25 1 Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect). 1
(b) (12 points) For this problem, please note that when you remove a node that has two children, you have two ways to go about it. If you choose one way, you must be consistent and choose the same way throughout this problem. Remove the following nodes from the above Binary Search Tree in the specified order: 17, 20 26 Draw the resulting binary search tree after these removals are done. Be sure to show intermediate steps if you want partial credit (in case your final tree is incorrect). 2
c) (12 points) Write the code for a templated member function of the class BinarySearchTree to insert a new value into a binary search tree (this includes the code for finding where to insert the node as well as inserting it). Be sure to check if the user enters a duplicate value. Feel free to add whatever other member functions or member variables you need (if any). The next page is blank if you need more room to work. You are only responsible for writing the actual insert function and you may assume that the following code declarations already exist: template <typename T> class BTNode{ public: BTNode(T& d) : data(d) { left = NULL; right = NULL; } T data; BTNode<T>* left; BTNode<T>* right; }; template <typename T> class BinarySearchTree { private: BTNode<T>* root; public: void insert(t& value); }; 3
(This problem intentionally left blank so you can use this page for whatever you want.) 4
Problem 2. AVL & Balance Trees(14 points) a) (8 points) Calculate the balance factor for each node in the tree 5
b) (3 points) Is this tree balanced? If not, which nodes would need to be balanced? c) (3 points) Why is balance important for binary search trees? How does it affect Big O? 6
Problem 3. Heaps (20 points) a) (12 points) A binary heap is an array-based data structure that can be viewed as a nearly-complete binary tree structure. You are given an array with the following elements (in sequence): 83 24 46 13 20 69 7 35 The elements currently do not satisfy the Max Heap Property. Please make the array into a Max Heap by performing a sequence of Max-Heapify operations, starting from the bottom of the tree. Please draw all intermediate trees whenever elements are swapped in order to receive full credit. Feel free to add additional text and/or drawings if you want to help explain what you are doing. Please also show what the resulting array would look like. 7
b) (8 points) Explain the algorithm for the Extract Max operation (removing and returning the maximum value node) from a Priority Queue that is based on a Max Heap (including the details of how the tree is rearranged to make sure it still obeys the max heap property). You may use your answer for the Max Heap above as an example. 8
Problem 4. Graphs (20 points) a) (5 points) Draw the Adjacency Matrix for the above graph 9
b) (5 points) Draw the Adjacency List for the above graph c) (5 points) Write out the order in which the vertexes in the graph would be visited if a Breadth-First Search were started from vertex C given the above adjacency list representation of the graph. (NOTE: There is only one correct answer since you must use the given adjacency list and follow the rules of accessing such a data structure.) d) (5 points) Write out the order in which the vertexes in the graph would be visited if a Depth-First Search were started from vertex C given the above adjacency list representation of the graph. (NOTE: There is only one correct answer since you must use the given adjacency list and follow the rules of accessing such a data structure.) 10
Problem 5. Short Answers (20 points) a) (5 points) What are the two most common strategies for handling collisions in a hash table? Name them and explain how each of them works. b) (5 points) What is Big O for searching, inserting, and removing for a hash table with a perfect/ideal hashing function? What about with the worst possible hash function? 11
c) (5 points) What is Big O for searching, inserting, and removing for a binary search tree? Why is it sometimes better to use a Red/Black Tree or AVL tree instead of a normal Binary Search Tree? d) (5 points) A binary heap can be viewed as a nearly-complete binary tree structure. What is the Max Heap Property for a binary heap? What is Big O for searching, inserting, and removing for a binary heap? 12
EXTRA CREDIT (10 points) A Priority Queue is represented by the following Min Heap: 4 10 22 40 12 36 55 87 61 29 33 48 a) (3 points) Draw the Min Heap b) (7 points) Perform Extract Min on the Priority Queue (removing and returning the minimum value node) and draw the resulting Min Heap. Please show intermediate steps for partial credit. 13