




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: LAB: Data Structures; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;
Typology: Assignments
1 / 8
This page cannot be seen from the preview
Don't miss anything!





CS230 Data Structures Handout # 24 Prof. Lyn Turbak Friday, April 30 Wellesley College
Overview: In this problem set, you will get experience with recurrence equations, running times, and asymptotic notations; with sorting; with heaps; and with 2-3 trees. All of the problems on this assignment are pencil-and-paper problems; this problem set requires no programming.
Working Together: You may work with a partner on this assignment. As usual, you are expected to work on all of the problems together.
Submission: For each problem, your hardcopy submission should include answers to the pencil and paper problems. There are no softcopy submissions for any problem. Remember to include a signed cover sheet (found at the end of this problem set description) at the beginning of your hardcopy submission.
Problem 1 [10]: Asymptotic Notation Recall the following from class:
There are two other relevant notations for comparing asymptotic running times:
Here is an informal summary of the notation:
Notation Pronunciation Loosely f ∈ ω(g) f is way bigger than g f > g f ∈ Ω(g) f is at least as big as g f ≥ g f ∈ Θ(g) f is about the same as g f = g f ∈ O(g) f is at most as big as g f ≤ g f ∈ o(g) f is way smaller than g f < g
w consider the following six functions:
For each of the following five sets, indicate which of the above functions are members of the set:
Problem 2 [15]: Running Times of Membership Below is a table containing brief descriptions of various data structures that hold collections of integers. For each such data structure, think of the best algorithm for determining if an element is a contained in such a data structure. Then indicate the asymptotic worst-case running time (using Θ notation) of the algorithm in the second column of the table. You should phrase your answer in terms of the number n that appears in each description. Unless the description indicates otherwise, you should not make any assumptions about the arrangement of elements in the data structure.
Description Running Time An unordered array of n elements. An array of n elements ordered from smallest to largest. An array of n elements ordered from largest to smallest. An n x n two-dimensional array of unordered elements. An unordered linked list of n elements. A linked list of n elements ordered from smallest to largest. A linked list of n elements ordered from largest to smallest. A binary tree with n elements. A binary search tree with n elements. A balanced binary search tree with n elements. A complete max heap of n elements. A complete min heap of n elements. A binary tree of height n. A balanced binary search tree of height n. A linked list of n balanced binary search trees, each with n elements.
public static int two1 (int n) { if (n == 0) { return 1; } else { return 2 * two1(n - 1); } }
public static int two2 (int n) { if (n == 0) { return 1; } else { return two2(n - 1) + two2(n - 1); } }
public static int two3 (int n) { if (n == 0) { return 1; } else { return two3(n - 1) + two1(n - 1); // Yes, the second call is to two1, not two3. } }
public static int two4 (int n) { // To simplify analysis, assume that the input to two4 is a power of 2. if (n == 0) { return 1; } else if (n % 2 == 0) { int x = two4(n / 2); return x * x; } else { return 2 * two4(n - 1); } }
public static int two5 (int n) { // To simplify analysis, assume that the input to two5 is a power of 2. if (n == 0) { return 1; } else if (n % 2 == 0) { two5(n / 2) * two5(n / 2) } else { return 2 * two5(n - 1); } }
public static int two6 (int n) { if (n == 0) { return 1; } else { return two6(n - 1) + two4(n - 1) // Yes, the second call is to two4, not two6. } }
Figure 1: Six different method for raising 2 to the nth power.
b. [4] Based on the table from part a, determine for each of Bud’s four routines which of the four sorting algorithms it uses. Briefly explain your reasoning.
c. [3] Answer the following for the four sorting algorithms that Bud has implemented:
Problem 5 [17]: 2-3 Trees In the following parts, you are asked to draw some 2-3 trees. As a simple check to avoid errors, verify that every tree you draw is a legal 2-3 tree.
a. [8] Draw the sequence of 2-3 trees T 0 , T 1 ,... , T 17 that results from inserting the following letters into the empty tree T 0 : T H E Q U I C K B R O W N Y A M S You need only show the final tree that results from each insertion; you need not show the indi- vidual steps that lead to each intermediate tree.
b. [9] Draw the sequence of 2-3 trees T 17 , T 18 ,... T 34 that results from deleting the following letters one-by-one from the initial tree T 17 : T H E Q U I C K B R O W N Y A M S You need only show the final tree that results from each deletion; you need not show the individual steps that lead to each intermediate tree. When deleting a value from a non-terminal node, you may replace it with either its in-order predecessor or in-order successor, whichever is easier.
Problem 6 [25]: A Heap o’ Heaps In the following parts, you are asked to draw some complete min heaps (not max heaps) and leftist min heaps (not max heaps). As a simple check to avoid errors, verify that every binary tree you draw is a legal complete min heap or leftist min heap.
a. [5] Draw as trees the sequence of complete min heaps C 0 , C 1 ,... , C 17 that results from inserting the following letters into the empty heap C 0 : T H E Q U I C K B R O W N Y A M S For example, C 1 should be the result of inserting T into the empty heap C 0 ; C 2 should be the result of inserting H into C 1 ; and so on. Assume that letters appearing earlier in the alphabet are “less than” those appearing later. For example, the root node of C 17 should be A. You need only show the final tree that results from each insertion; you need not show the individual ”bubble up” steps that lead to the final tree.
b. [2] One advantage of complete min heaps is that they can be represented as arrays. Show how the complete min heap C 17 would be represented as an array.
c. [4] Draw as trees the sequence of complete min heaps C 18 , C 19 , C 20 , C 21 that result from dequeuing the four smallest items of C 17. You need only show the final tree that results from each deletion; you need not show the individual ”bubble down” steps that lead to the final tree.
d. [4] Draw as trees the sequence of leftist min heaps L 10 , L 11 ,... , L 18 that results from inserting the following letters into the empty heap L 10 :
T H E Q U I C K
You need not show the intermediate results of individual merge steps.
e. [4] Draw as trees the sequence of leftist min heaps L 20 , L 21 ,... , L 29 that results from inserting the following letters into the empty heap L 20 :
B R O W N Y A M S
You need not show the intermediate results of individual merge steps.
f. [2] Draw as a tree the leftist min heap L 0 that results from merging the two heaps L 18 and L 29.
g. [4] Draw as trees the sequence of leftist min heaps L 1 , L 2 , L 3 , L 4 that results from dequeuing the four smallest items of L 0. You need only show the final tree that results from each deletion; you need not show the individual merge steps that lead to the final tree.
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.