CMSC 341 Data Structure
Asymptotic Analysis Review
These questions will help test your understanding of the asymptotic analysis material discussed in class and in the text. These questions are only a study guide. Questions found here may be on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam.
1. What is the purpose of asymptotic analysis?
2. Define “Big-Oh” using a formal, mathematical definition.
3. Let T1(x) = O(f(x)) and T2(x) = O(g(x)). Prove T1(x) + T2(x) = O (max(f(x), g(x))). 4. Let T(x) = O(cf(x)), where c is some positive constant. Prove T(x) = O(f(x)). 5. Let T1(x) = O(f(x)) and T2(x) = O(g(x)). Prove T1(x) * T2(x) = O(f(x) * g(x))
6. Prove 2n+1 = O(2n).
7. Prove that if T(n) is a polynomial of degree x, then T(n) = O(nx).
8. Number these functions in ascending (slowest growing to fastest growing) Big-Oh order:
- Number
- Big-Oh
O(n3)
O(n2 lg n)
O(1)
O(lg0.1 n) O(n1.01 O(n2.01 O(2n)
))
O(lg n) O(n)
O(n lg n) O (n lg5 n)
1
9. Determine, for the typical algorithms that you use to perform calculations by hand, the running time to:
a. Add two N-digit numbers b. Multiply two N-digit numbers
10. What is the asymptotic performance of each of the following?
Select among:
- a. O(n)
- b. O(n2)
- c. O(n lg n) d. O(n3)
- e. O(lg n)
- f. O(1)
- g. O(n!)
h. None of these
(a) ___________ Squaring each element of an NxN matrix (b) ___________ Finding the smallest value in a sorted array of N integers (c) ___________ Finding a value in a sorted array using binary search (d) ___________ Pushing N elements onto a stack, then popping them and printing them (e) ___________ Finding the largest 3 values in an unsorted array
11. What is the asymptotic performance of the following Java code fragment?
Justify your answer.
for (int i = 0; i < N; i++) {for (int j = 10; j >= 0; j--) {int count = 1; while (count < N) count *= 2;
}
}
2
CMSC 341 Data Structures
List Review Questions
Please refer to the textbook for List, ListNode, and ListIterator class definitions. These definitions are the same as those found in the class notes. You may assume that all member functions of these classes have been written and work properly when answering the questions below. These questions will help test your understanding of the list material discussed in class and in the text. These questions are only a study guide. Questions found here may be on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam.
1. Write a new member function of the List class named ReversePrint that uses an iterator to display the elements of the List in reverse order. The data elements should be enclosed
in angle brackets (“< >”) and separated by commas. Do not construct a copy of the list
that is in reverse order, just use iterators. The prototype for ReversePrint( ) is shown below
void ReversePrint ( );
2. Write a new function named Splice( ) whose prototype is shown below. Note that
Splice( )is not a member function of the List class. This function “splices” L2 into L1 at
the specified position (posis a ListIterator over L1). If posis past end, Splice( ) does
nothing. For example, suppose L1 = {1, 2, 3, 4, 5} and L2 = {10, 20, 30} and posis constructed as list1.iterator( ) and then advanced twice (pos.next()) so it is positioned
before the “3”. Then the function call Splice( L1, L2, pos ); causes L1 to
become { 1, 2, 10, 20, 30,3, 4, 5} and L2 is unchanged. What is the asymptotic performance of Splice( )? Bear in mind that there are two lists which may be of different lengths.
public static<T> void Splice( List<T> L1, const List<T> L2, ListIterator<T> pos);
3. Complete the following table of Big-Oh, worst-case asymptotic time performance for the given operations and implementations of the List ADT. Give your answers in terms of n, the number of elements in the list.
Operation
insert (index, x ) ind( x )
- Double Linked List
- ArrayList
remove( x ) makeEmpty add(x )
1
4. Suppose you are provided with a set of N random numbers which are to be inserted into a sorted List (smallest to largest). What would be the worst-case asymptotic time performance for building the entire list?
5. One advantage of using a double-linked list is the availability of bi-directional list
iterators – iterators that move “forward” and move “backward”. Suppose that in order to
save memory, you (or your boss) decide to use a single linked list. Can a single linked list still support bi-directional iterators? If so, briefly describe how the iterator would move forward and backward. Be sure to include the Big-Oh performance of these operations. If bi-directional iterators are not possible with a single linked list, explain why not.
6. Describe the advantages (if any) and disadvantages (if any) of each of the List ADT implementation – singly linked list, doubly linked list, and arraylist.
7.Write the code for a new LinkedList method that removed the node AFTER the one specified by the parameter. The signature of this new method is provided below.
private AnyType removeAfter( Node<AnyType> p )
2
CMSC 341 Data Structures Stack and Queue Review
These are some review questions on stacks. The class definitions for stack and queue are provided at the end of the questions. These questions will help test your understanding of the stack and queue material discussed in class and in the text. These questions are only a study guide. Questions found here may be on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam.
Stacks
1. Using only the operations of the stack, write a static function that determines if a
string is a palindrome (i.e. reads the same backward and forward; e.g. “level”). The
prototype for this function is given below.
public static bool isPalindrome(String theString);
2. What is the output of the following code?
int [] values = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; Stack<Integer> s = new Stack<Integer>( );
for (int 1 = 0; i < values.length; i++) s.push( values[ i ] );
int n = 25; for (int i = 0; i < 4; i++) {n += s.pop( );
}for (int i = 0; i < 2; i++) {n -= s.pop( );
}System.out.println( n );
3. Discuss the advantages and disadvantages if the text’s array implementation and the
lecture notes layered implementation of the stack ADT. At a minimum, consider the
asymptotic time performance of the isEmpty( ), pop( )and push( )
operations.
1
4. Using only the operations of the stack given in the class, write a Java function that returns a copy of the user specified stack. The prototype for the function is given below.
public static<AnyType> Stack<AnyType> CopyStack(Stack<AnyType> otherStack)
Queues
1. Using the operations of the stack and queue, write a static function that determines if
a string is a palindrome (i.e. reads the same backward and forward; e.g. “level”). The
prototype for this function is given below.
public static bool isPalindrome(String theString );
2. Suppose that Q is an initially empty array-based queue of size 5. Show the values of the data members front and back after each statement has been executed. Indicate any errors that might occur.
Queue<Character> Q( 5 ); front = _____ back = _____
Q.enqueue ( ‘A’ ); Q.enqueue ( ‘B’ ); Q.enqueue ( ‘C’ );
char c = Q.dequeue( );
Q.enqueue ( ‘A’ );
front = _____ back = _____ front = _____ back = _____ front = _____ back = _____ front = _____ back = _____ front = _____ back = _____
3. Discuss the advantages and disadvantages of the link list and array-based implementations of a queue.
4. Describe three “real life” applications of a queue.
5. Explain how to implement a queue using two stacks.
2
Definition of the Stack Class
This is the definition of the array based stack from the text:
public class Stack<AnyType> {public Stack ( int capacity ); public boolean isEmpty( ); public boolean isFull( ); public void makeEmpty( ); public AnyType pop( ); public AnyType top( ); public void push( AnyType element ); private ArrayList< AnyType > theArray; private int topOfStack;
}
Definition of the Queue Class
This is the definition of the array based queue from the text:
public class Queue<AnyType> {public Queue( int capacity ); public boolean isEmpty( ); public boolean isFull( ); public AnyType getFront( ); public void makeEmpty( ); public AnyType dequeue( ); public void enqueue( AnyType element ); private ArrayList< AnyType > the Array private int currentSize, front, back; private void increment( int x );
}
3
CMSC 341 Data Structures
General Tree Review
These questions will help test your understanding of the general tree material discussed in class and in the text. These questions are only a study guide. Questions found here may be on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam.
General Trees
1. Define tree.
2. Define k-ary tree.
3. For any tree, T, define the following a. path in T b. length of a path in T c. height of a node in T d. depth of a node in T e. height of T f. depth of T g. external node h. internal node i. leaf
4. Given the drawing of an arbitrary tree, draw the first-child, next-sibling representation of the tree.
5. Given the first-child, next-sibling representation of a tree, draw the tree. 6. Prove that there are n – 1 edges in any tree with n nodes. 7. What is the worst-case Big-Oh performance for the insert, find and remove operations in a general tree? Why is this so?
8. Write a recursive member function of the “static K-ary” tree class that counts the number
of nodes in the tree.
1
Binary Trees
1. Define binary tree, full binary tree, complete binary tree and perfect binary tree
2. Prove that a perfect binary tree of height h has 2h leaf nodes. 3. Prove that a perfect binary tree of height h has 2h+1 – 1 nodes. 4. Prove that a full binary tree with n internal nodes has n + 1 leaf nodes. 5. Prove that in any binary tree with n nodes there are n +1 “null pointers”. 6. Suppose that you have two traversals from the same binary tree. Draw the tree. pre-order: A D F G H K L P Q R W Z in-order: G F H K D L A W R Q P Z
7. Write a recursive member function of the BinaryTree class that counts the number of nodes in the tree.
8. Write a recursive member function of the BinaryTree class that counts the number of leaves in the tree.
9. Given the following binary tree containing integers, list the output from a pre-order
traversal, an in-order traversal, a post-order traversal, and a level-order traversal of the
tree.
87
114
56
93
66
42
123
39
17
29
2
CMSC 341 Data Structures Binary Search Tree Review
These questions will help test your understanding of the binary search tree material presented in class and in the text. These questions are only a study guide. Questions found here may be found on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam. The necessary class definitions will be provided with your exam. Please refer to the BST and BST node class definitions in the text.
1. Define binary search tree.
2. Write the syntactically correct Java code for the private BST member function find(T x)whose function prototype is found below and cannot be changed. This private function is initially called by the public find(T x)with a reference to the root. If x is in the tree, return element in the node; otherwise return NULL.
private T find(BinaryNode<T> root, T x)
3. Describe how deletion is performed in a BST. Be sure to mention all relevant cases. 4. The number of comparisons to build a BST of n elements is O(n2) in the worst case and
O(n lg n) in the best case. Describe the best and worst case and explain why this is so.
5. Insert the following Java reserved words into an initially empty BST in the order given:
break, operator, if, typedef, else, case, while, do, return, byte, for, true, double, void.
a. Show the output from the pre-order, post-order, in-order, and level-order traversals of the BST. b. Which word is the predecessor of while? c. Which word is the successor of for? d. Draw the new tree that results from each of the following operations. Each problem is separate and begins with the tree created above. i. Insert int and float, in that order.
ii. Delete double, case, and typedef, in that order.
6. Prove that if a node in a BST has two children, its successor has at most one child. 7. Suppose the result of a post-order traversal of a BST is 1, 3, 2, 8, 10, 5, 4. Draw the original BST.
1
CMSC 341 Data Structures
Splay Tree Review
These questions will help test your understanding of the splay tree material presented in
class. Note that the splay tree presented in class is a “bottom-up” splay tree. These questions are
only a study guide. Questions found here may be found on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam. The splay tree rules may be provided with your exam. Check with your instructor. Please refer to the Splay tree class definitions in the text.
1. Define splay tree.
2. Describe the splay operation. 3. Show the result of inserting the values 2, 1, 4, 5, 9, 3, 6, 7 into an empty splay tree. Show the tree at the end of each insertion. Show each rotation.
4. Explain, in English, how to insert a new element into a splay tree. Be sure to discuss the case in which a duplicate element is inserted.
5. Explain, in English, how to find an element in a splay tree. Bu sure to discuss the case in which the element is not found.
6. Explain, in English, how to remove an element from a splay tree. Be sure to discuss the case in which the element to be removed is not in the tree.
7. What does the following statement mean? In particular, what does “m” represent?
“A splay tree has O(m lg n) amoritized performance over a sequence of insert, remove
and find operations.”
8. For splay tree operations, we use amortized analysis rather than asymptotic analysis. a. What is amortized analysis? b. How is amortized analysis different than asymptotic analysis? c. Why must amortized analysis be used for splay trees?
CMSC 341 Data Structures
Red-Black Tree Review
These questions will help test your understanding of the Red-Black tree material discussed in class and in the text. These questions are only a study guide. Questions found here may be on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam. The rotation diagrams for red-black trees may be provided with your exam. Check with your instructor.
1. Define Red-Black tree. List all Red-Black tree properties 2. Define the black height of a node, x. 3. What is the “Big-Oh” performance (in terms of the number of nodes in the tree) for the operations find, insert and remove for a red-black tree in the best, worst and averages cases?
4. What property of red-black trees is most significant in explaining the “Big-Oh”
performance for the operations find, insert and remove?
5. Prove that in any red-black tree with root x, there are at least n = 2bh(x) – 1 internal nodes where bh(x) is the black-height of x.
6. Prove that in any red-black tree, at least half the nodes on any path from the root to a leaf must be black.
7. Prove that in any red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf.
8. Prove that if a black node has just one child, that child must be red. 9. Show the tree that results from inserting the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty red-black tree. Show the tree after each insertion.
1
10. Given the following Red-Black Tree, show the tree that results after deleting the node with value 68 using bottom-up deletion.
87
114
56
49
166
- 40
- 68
Represents a BLACK node Represents a RED node
2
CMSC 341 Data Structures
K-D Tree Review
These questions will help test your understanding of k-D trees. These questions are only a study guide. Some of these questions may appear on your exam, although perhaps in a different format. Question NOT found on this study guide may also appear on your exam.
1. What is the purpose of k-D trees?
2. Explain how a 2-D tree extends a binary search tree. 3. Insert the following pairs (in the order shown) into an initially empty 2-D tree.
(53, 14), (27, 28), (30, 11), (67, 51), (70, 3)
4. Insert the following triples (in the order shown) into an initially empty 3-D tree.
(16, 8, 12), (18, 7, 15), (10, 9, 3), (13, 8, 9), (20, 10, 6), (3, 10, 7), (12, 14, 6)
1
CMSC 341 Data Structures
Priority Queue Review
These questions will help test your understanding of the priority queue material discussed in class and in the text. These questions are only a study guide. Questions found here may be on your exam, although perhaps in a different format. Questions NOT found here may also be on your exam.
1. Define the following terms a. priority b. priority queue c. min binary heap d. partial ordering e. null path length in a binary tree f. leftist binary tree g. leftist heap
2. Insertion and deletion (of the minimum element) in a min binary heap are O(lg n) on average. Explain why this is so.
3. Finding the minimum element in a min binary heap is O(1) in the worst case. Explain why this is so.
4. Although a binary heap is conceptually a binary tree, it can be implemented as an array.
Explain why this is so.
5. In a min binary heap with N elements, what is the range of indices in which the largest element will be found?
6. Describe, in English, an algorithm to find the largest element in a min binary heap. What is the asymptotic worst-case performance of your algorithm?
7. Assume that the array representing a min binary heap contains the values 2, 8, 3, 10, 16,
7, 18, 13, 15. Show the contents of the array after inserting the value 4.
8. Assume that the array representing a min binary heap contains the values 2, 8, 3, 10, 16,
7, 18, 13, 15. Show the contents of the array after deleting the minimum element.
9. Show the array representing the min binary heap constructed using the initial values 18,
2, 13, 10, 15, 3, 7, 16, 8.
10. Prove that the largest element in a min binary heap is a leaf.
1
11. Prove that a complete binary tree is a leftist tree. 12. Prove for any leftist tree with N nodes, the number of nodes, R, on the rightmost path to a non-full node is given by R < lg(N + 1)
13. Given the drawing of a binary tree, determine if the tree is a leftist tree and if it is a leftist heap. Give reasons why or why not.
14. Given the drawings of the two leftist heaps, draw the leftist heap that results from merging them.
15. Describe how to perform the findMin, insert, and deleteMin operations on a leftist heap. 16. Describe a method for constructing a leftist heap from an initial set of N values. Your algorithm must run in O(N) time.