# Cen 254 Data Structure

Topics: Binary search tree, Binary tree, Tree Pages: 6 (924 words) Published: May 9, 2013
CS113 Review Questions

July 18, 2009
This is a collection of some interesting problems, carefully collected to help you prepare better for the midterm and, which is more important, help you in understanding of the key concepts.
Try to solve them independently or with your friends.
Review problem 1 How many di®erent binary search trees can store the keys f1;2;3g? The same question for f1;2;3;4g.
Solution: For the ¯rst: 5. The second: there are 5 possibilities each when 1 and 4 are the root of tree. If 2 is the root of the tree, there are 2 di®erent BSTs. The same when 3 is the root. In total, 14.

Review problem 2 Insert, into an empty binary search trees, entries with keys 30, 40, 24, 58, 48, 26, 11, 13 (in this order). Draw the tree after each insertion.
Review problem 3 By giving an example, prove that the following claim is false: the order in which a ¯xed set of entries is inserted into a BST does not matter- the same tree results every time.
Review problem 4 In this problem you will design a linear-time algorithm that clones a binary tree T. This algorithm should construct an exact copy of T. You are only allowed to use the methods of the Tree and Binary Tree ADTs (i.e. isEmpty, root, parent, children, isInternal, isExternal, isRoot, size, elements, positions, swapElements, replaceElement, leftChild, rightChild, sibling, expandExternal, removeAboveExternal).

1Hint: Do preorder traversal of the corresponding trees.
Review problem 5 Describe how to perform an operation removeAll(k), which removes all the entries whose keys equal k from a BST T. Review problem 6 Give a recursive method for removing all the elements in a stack.

Solution: If the stack is empty return empty. Otherwise, pop() and recur. Review problem 7 Describe the output of the following series of stack op- erations: push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7), push(6), pop(), pop(), push(4), pop(), pop(). Review problem 8 R.6.7. from the textbook.

Review problem 9 R.6.9. from the textbook.
Review problem 10 R.6.11. from the textbook.
Solution: The minimum is 0, since we could have accessed each element exactly k times. The maximum is n ¡ 1, since we could have accessed just one element kn times.
Review problem 11 R.7.2. from the textbook.
Review problem 12 R.7.5. from the textbook.
Review problem 13 Consider implementing a queue with e generic linked list by removing from the head and inserting at the tail. Why would it be bad to insert at the head and remove at the tail?
Review problem 14 Given a BST T, sketch an algorithm to ¯nd the max- imum and minimum key values stored in T. Estimate time complexity of your algorithm.
Review problem 15 Given an arbitrary binary tree T with integer keys stored at the nodes, design an e±cient algorithm which determines whether or not T is a binary search tree. What is the time complexity of your algorithm? 2Review problem 16 Suppose you are asked to use two stacks, YinStack and YangStack, as your only instance variables to implement the Queue ab- stract data type. Describe in English or pseudo-code how you would imple- ment the methods enqueue() and dequeue(). Analyze the complexity in terms of n, the number of elements in the queue.

Review problem 17 Problem C-3.6.
Solution: Let us de¯ne a method reverse(L; n), which reverses the ¯rst n • L:size() nodes in L, and returns a pointer end to the node just af- ter the nth node in L (end = null if n = L:size()). If L:size() • 1, we are done, so let us assume L has at least 2 nodes. If n = 1, then we re- turn L:¯rst():next(). Otherwise, we recursively call reverse(L; n ¡ 1), and let end denote the returned pointer to the nth node in L. We then set ret to end.next() if n < L:size(), and to null otherwise. We then insert the node pointed to by end at the front of L and we return ret. The total running time is O(n).

Review problem 18 Problem C-3.7.
Solution: Simply...