CMSC 341 Data Structure - ecology...

26

Transcript of CMSC 341 Data Structure - ecology...

Page 1: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will
Page 2: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

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 lg

5 n)

Page 3: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

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(n

3) 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;

}

}

Page 4: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

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 (pos is a ListIterator over L1). If pos is past end, Splice( ) does

nothing. For example, suppose L1 = {1, 2, 3, 4, 5} and L2 = {10, 20, 30} and pos is

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 Double Linked List ArrayList

insert (index, x )

ind( x )

remove( x )

makeEmpty

add(x )

Page 5: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

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 )

Page 6: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

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.

Page 7: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

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’ ); front = _____ back = _____

Q.enqueue ( ‘B’ ); front = _____ back = _____

Q.enqueue ( ‘C’ ); front = _____ back = _____

char c = Q.dequeue( ); front = _____ back = _____

Q.enqueue ( ‘A’ ); 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.

Page 8: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

3

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 );

}

Page 9: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

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.

Page 10: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

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 42

39

66

123 17

29

Page 11: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

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.

Page 12: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

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?

Page 13: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

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.

Page 14: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

10. Given the following Red-Black Tree, show the tree that results after deleting the

node with value 68 using bottom-up deletion.

Represents a BLACK node

Represents a RED node

87

114 56

40 68

49

166

Page 15: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

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)

Page 16: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

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.

Page 17: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

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.

Page 18: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

CMSC 341 Data Structures

B-Tree Review

These questions will help test your understanding of the B-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.

1. Define B-Tree. List all B-Tree properties.

2. What does it mean to say a B-Tree is order M?

3. When describing a B-Tree, what does L represent?

4. Give the pseudo-code for finding a particular element in a B-Tree of order M.

5. Given the drawing of a B-Tree, show the new B-Tree after inserting a given element.

6. Given the drawing of a B-Tree, show the new B-Tree after deleting a given element.

7. Draw a valid B-Tree with M = 4 and L = 3 containing the integer values 1 – 25.

8. Show the result of inserting the elements 1, 3, 5, 7, 9, 11, 6 into an initially empty B-Tree

with M = 3 and L = 3. Show the tree at the end of each insertion.

9. Given the following characteristics of an external storage problem, design a suitable B-

Tree (i.e. calculate appropriate values of M and L).

a. The number of items to be stored

b. The size (in bytes) of the key for each item

c. The size (in bytes) of each item

d. The size (in bytes) of a disk block

10. What is the minimum and maximum number of leaves in a B-Tree of height h = 2 when

M = 3?

11. The average case performance of the dictionary operations insert, find and delete is

O(lg N) for balanced binary search trees like Red-Black trees. In a B-Tree, the average

asymptotic performance for the dictionary operations is O(logM N) where M is the order

of the B-Tree. Discuss the following.

Page 19: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

a. When M = 2, do the B-Tree and the RB Tree have equivalent asymptotic

performance for the dictionary operations? Are there advantages of one over the

other?

b. B-Tree height is proportional to logM N indicating that for a given N, a B-Tree of

higher order will be shorter than one of lower order. Is this true? If so, why not

always choose a very high value for M since the average asymptotic performance

of the dictionary operations is in O(height).

c. B-Trees find their greatest utility when data are stored externally (on disk rather

than in memory). Why is this so?

Page 20: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

CMSC 341

Treap Review Questions

1. Insert the following characters with their respective priorities (shown as ordered

pairs) into an empty treap, then answer the questions below.

(A, 17), (F, 22), (Q, 15), (K, 25), (R, 14), (B, 35),

(X, 27), (M, 24), (L, 42), (T, 30)

a. What is the height of the treap?

b. What character is at the root of the treap?

c. How many leaves does the treap have?

d. How many levels does the treap have?

e. Which character (if any) is the left child of M

2. Perform the following deletions from the treap created in question #1 above and

then answer questions 1.a. thru 1.e. for the new treap

a. Delete F

b. Delete A

c. Delete K

3. Some trees (such as binary heaps and up-trees) can be implemented in an array

instead of using nodes and left/right child references. Can a treap be implemented

as an array? If, describe how data would be stored and how the insert, delete and

find operations would work. If not, explain why not.

4. Explain how treaps can be used to divide a finite set into two subsets such that all

elements in one subset are less than a given key, K, and all values in the other

subset are greater than K.

5. Treaps are useful because they are (almost) always balanced. Why is this so?

6. Treaps are often suggested as an alternative to Red-Black trees because they are

simpler to implement and perform “reasonably well”, yet it is possible that a treap

will degenerate into a linked-list whereas a RBT will not. Describe the

circumstances under which a treap may become a linked-list.

Page 21: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

CMSC 341 Data Structures

Skiplist Review

These questions will help test your understanding of the Graph 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. The expected asymptotic time for Skiplist operations is O(lg n). There is a non-zero

probability that the performance could be as bad as O(n). Draw a 7-element Skiplist with

max node size of 4 that would have such poor performance.

2. What maximum node size is appropriate for a Skiplist suitable for storing 65,535

elements and with associate probability ¼?

3. Given the drawing of a Skiplist, indicate all comparisons done in searching for a

particular element.

4. Given a Skiplist with probability p and maximum node size M that contains N nodes,

show the expected distribution of node sizes (how many nodes of each size).

5. The following perfect Skiplist is valid for p = ½. Draw an equivalent figure for p = ¼.

What distribution of nodes (how many nodes of each size) do you expect in a long list of

this type?

• head

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 22: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

CMSC 341 Data Structures

Hashing Review

These questions will help test your understanding of the Graph 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 a hash function? Name two desirable properties of a hash function.

2. Define collision in a hash table.

3. What is the clustering problem in hash tables?

4. Describe the division method for generating hash values.

5. Describe the multiplication method for generating hash values.

6. Define Fibonacci hashing.

7. Describe the separate chaining collision resolution method.

8. Describe the open addressing collision resolution method.

9. Given a hash table of size 13, show the contents of your hash table after inserting the

values {8, 2, 7, 18, 15, 19, 23, 15, 20, 16} using open addressing with linear probing

( f(i) = i ) for collision resolution.

10. Repeat question 9, using open addressing with quadratic probing ( f(i) = i2 ) for collision

resolution.

11. Repeat question 9 using separate chaining for collision resolution.

12. The average time performance of the insertion and searching operations on a hash table is

O(1), which is much better than the performance of a binary search tree for the same

operations. Given this wonderful performance of hash tables as compared to binary

search trees, when would you want to use a binary search tree instead of a hash table?

Page 23: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

13. In a hash table using open addressing with linear probing, the average number of probes

for successful search, S, and unsuccessful search (or insertion), U, are

( 1 + )

where λ is the load factor of the table. Suppose you want a hash table that can hold at

least 1000 elements and you want successful searches to take no more than 4 probes on

average.

a. What is the maximum load factor you can tolerate in your hash table?

b. If the table size must be prime, what is the smallest table size you can use?

c. Based on your answers to (a) and (b), what is the average number of probes to

perform an insertion?

Page 24: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

CMSC 341 Data Structures

Disjoint Set Review

These questions will help test your understanding of the disjoint set 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 lg* (N). What is the value of lg* (1024)?

2. Define the Union-by-Weight heuristic.

3. Define the Path Compression heuristic.

4. When both Union-by-Weight and Path Compression are used on disjoint sets with a

universe of N elements, a sequence of M union-find operations can be done in

O(M lg* N) time. It is sometimes said that under these conditions, union-find is done in

constant time per operation. What does this mean? Why is it true?

5. In an uptree with root x, let R(x) be the length of the longest path and let N be the number

of nodes (including x). Assuming the uptree was created by means of multiple union

operations using the Union-by-Weight heuristic. Prove R(x) < lgN.

6. Perform the following Union-by-Weight operations on a universe of 10 elements (0-9,

each initially in their own set). Draw the forest of trees that result. U(1,5); U(3,7); U(1,4);

U(5,7); U(0,8); U(6,9); U(3,9). If the sets have equal weight, use the root with the smaller

value as the root of the new set.

7. Although uptrees are used to conceptualize disjoint sets, disjoint sets are generally

implemented in an array. Explain how this is possible.

8. Prove that if Union-by-Weight is used for all unions, the length of the deepest node is no

more than lg(N).

9. Given the following forest of uptrees,

a. show the array which represents them

b. show the result of find(6), using Path Compression

1

4

5

2

7

0 11

1

6 3

8

10 9

Page 25: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

1

CMSC 341 Data Structures

Graph Review

These questions will help test your understanding of the graph 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 the following terms

a. Graph

b. Weighted Graph

c. Directed Graph

d. Undirected Graph

e. Path

f. Length of a Path

g. Sparse Graph

h. Dense Graph

i. Connected Undirected Graph

j. Weakly Connected Directed Graph

k. Strongly Connected Directed Graph

l. Adjacency Matrix

m. Adjacency List

n. Directed Acyclic Graph

o. Topological Ordering

p. Cycle

2. Let G = (E, V) be an undirected graph. Let v1, v2, v3… vp be the members of V, and let

q = |E| (the cardinality of E). Prove that the sum of the degrees of all the vertices is equal

to 2q.

3. Write pseudo-code for the breadth-first and depth-first traversals of an undirected graph.

4. Given the drawing of a graph, list the breadth-first and depth-first traversals of the graph.

5. Describe, in English, an adjacency matrix graph implementation. How does an adjacency

matrix differ for directed and undirected graphs?

6. Describe, in English, an adjacency list graph implementation. How does an adjacency

matrix differ for directed and undirected graphs?

7. Given the drawing of a directed or undirected graph, show its representation in an

adjacency matrix or adjacency list.

Page 26: CMSC 341 Data Structure - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/exam_info/Fall_20… · CMSC 341 Data Structure Asymptotic Analysis Review These questions will

2

8. Draw the weighted directed graph represented by the adjacency matrix below. A non-

zero value at [row, column] indicates that the vertex in the row is adjacent to the vertex in

the column:

A B C D E

A 0 5 8 0 0

B 3 0 6 0 0

C 0 3 4 1 0

D 0 6 7 0 0

E 0 0 0 0 0

9. Given the drawing of a(n) (un)directed graph, show its representation in an adjacency list.

10. Draw the directed graph represented by the adjacency list below. Each element in a

vertices list is adjacent to the vertex.

11. Given the drawing of a graph, find all cycles.

12. Discuss the characteristics of the adjacency matrix and adjacency list implementations for

a graph. Include storage requirements and worst-case performance for all graph

operations.

13. Given a directed graph whose edges have positive weights, use Dijstrka’s algorithm to

find the shortest path between a given source and destination.

14. Explain why Dijstrka’s algorithm only works for graphs whose edges have positive

weights.

A

B

C

D

E

D

A

E

E

B

B