Docsity

Final Exam Questions: Data Structures and Algorithms, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Spring'2009'C343'Final'
Name:'______________________'
Username:'______________________'
Question'1:'
How$many$nodes$can$there$be$in:$
a)$A$left‐complete$binary$tree$of$height$h?$
$
$
'
b)$A$perfect$binary$tree$of$height$h?$$(In$a$perfect$tree,$all$nodes$have$0$or$2$children,$and$all$leaves$are$
on$the$same$level.)$
'
'
'
c)$A$perfect$ternary$tree$of$height$h?$$(In$a$ternary$tree,$each$node$may$have$up$to$3$children.)$
$
$
'
'
Question'2:$$
Many$recursive,$and$even$non‐recursive,$algorithms$can$be$modeled$by$a$recurrence$relation$that$has$
the$following$form:$
$
f(n)$=$a*f(n/b)$+$c$for$all$n$>$1,$where$a,$b,$and$c$are$positive$constants$
$
a)$Give$the$closed$form$(solution)$of$this$recurrence,$simplifying$it$as$far$as$possible.$
$
'
'
'
'
'
'
b)$Why$do$we$not$need$to$specify$f(1)$to$know$the$growth$rate$of$the$recurrence?$
'
'
'
c)$What$happens$if$a=1?$
$
$
pf3
pf4
pf5

Partial preview of the text

Download Final Exam Questions: Data Structures and Algorithms and more Exams Data Structures and Algorithms in PDF only on Docsity!

Spring 2009 C343 Final Name: ______________________ Username: ______________________ Question 1: How many nodes can there be in: a) A left‐complete binary tree of height h? b) A perfect binary tree of height h? (In a perfect tree, all nodes have 0 or 2 children, and all leaves are on the same level.) c) A perfect ternary tree of height h? (In a ternary tree, each node may have up to 3 children.) Question 2: Many recursive, and even non‐recursive, algorithms can be modeled by a recurrence relation that has the following form: f(n) = a*f(n/b) + c for all n > 1, where a, b, and c are positive constants a) Give the closed form (solution) of this recurrence, simplifying it as far as possible. b) Why do we not need to specify f(1) to know the growth rate of the recurrence? c) What happens if a=1?

Question 3: a) Describe in 2‐3 sentences how mergesort works on an array‐based list of numbers. b) Give a recurrence for the number of comparisons mergesort performs in terms of the number of items in the list. Indicate the purpose of each part of the recurrence. Question 4: a) A worst‐case AVL tree, also known as a Fibonacci tree, is an AVL tree in which only the leaves have a balance factor of 0. Give a recurrence for the number of Fibonacci trees having height h. (You do not need to solve the recurrence.) b) Explain why performing an AVL single rotation on a binary search tree does not change the order of nodes visited by an inorder traversal of the tree.

d) Write a pseudocode algorithm to remove the smallest item from an array heap. Question 6: a) Consider the following binomial heap (i.e. a binomial forest that has the heap property): Show the result of inserting the value 2 into this heap, and all intermediate merges.

b) Explain why finding the minimum value in a binomial forest is O(log n), where n is the number of nodes in the heap. Question 7: a) Normally, iterating over a hash map returns the elements in an unpredictable order. John Q. Programmer needs to iterate over the elements in the order he inserted them. Explain how to modify a generic hash map to grant this capability. The modified structure MUST have constant lookup time; you may sacrifice performance elsewhere if necessary, but do so as little as possible. b) Give the growth rates for insertion and deletion on the new structure in terms of the number of nodes.