Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Asymptotic Analysis - Data Structures - Exams, Exams of Data Structures and Algorithms

Main points of this exam paper are: Asymptotic Analysis, Same Input, Numerical Keys, Four-Node Tree, Node Tree, Binary Search, Search Tree, Valid Binary, Binary Heap, Possible Height

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name: ____________________________ Login: _______ (CS61B Spring 2005 Midterm 2, Shewchuk) 2
Problem 1. (8 points) Quickies.
a. (1 point) The worst-case time to find the second-smallest key in a 2-3-4 tree with n keys (and no
duplicate keys) is Θ (______________________), if the operation that finds it is implemented in an
intelligent way.
b. (1 point) Suppose Algorithm A runs in Ο (n log n) worst-case time, and Algorithm B runs in Ω(n2)
worst-case time. Is it always true that, given the same input, Algorithm A takes less time than Algorithm
B? If “yes,” explain why. If “no,” give a counterexample.
c. (1 point) Suppose the variable e references an Exception. Suppose the method that declared this
variable executes the line System.out.println(e);. Is this an acceptable thing to do in Java? If
so, what does Java do when it executes the line? (We aren't asking what string gets printed; instead, tell
us what Java does internally to print an exception.) If not, explain why it's not acceptable, and describe
the error we get.
d. (1 point) Draw a four-node tree, with numerical keys (one in each node), that could be either a valid
binary heap or a valid binary search tree.
e. (2 points) Draw the binary search tree that results if you start with an empty binary search tree and
insert the following keys in the following order: 0, 8, 2, 5, 3, 7, 1; then remove 8.
f. (2 points) Your friend tells you that his binary search tree implementation rebalances the tree after every
insert and delete operation, so that it always has the minimum possible height. This seems like a good
idea, because the height of the search tree never exceeds log2 n. But it's really a terrible idea, because
(unlike with a 2-3-4 tree) it cannot be done quickly. Give an argument why your friend's
implementation must sometimes take Ω(n) time per operation. (You get to pick the tree and the
sequence of insert/delete operations.)
g. (1 bonus point) What is the central tenet of Britney Theory?
Conversion to PDF by Arpad Kovacs using OpenOffice.org 2.0.4, 2007
pf3
pf4

Partial preview of the text

Download Asymptotic Analysis - Data Structures - Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!

Problem 1. (8 points) Quickies. a. (1 point) The worst-case time to find the second-smallest key in a 2-3-4 tree with n keys (and no duplicate keys) is Θ (______________________), if the operation that finds it is implemented in an intelligent way. b. (1 point) Suppose Algorithm A runs in Ο ( n log n ) worst-case time, and Algorithm B runs in Ω( n^2 ) worst-case time. Is it always true that, given the same input, Algorithm A takes less time than Algorithm B? If “yes,” explain why. If “no,” give a counterexample. c. (1 point) Suppose the variable e references an Exception. Suppose the method that declared this variable executes the line System.out.println(e);. Is this an acceptable thing to do in Java? If so, what does Java do when it executes the line? (We aren't asking what string gets printed; instead, tell us what Java does internally to print an exception.) If not, explain why it's not acceptable, and describe the error we get. d. (1 point) Draw a four-node tree, with numerical keys (one in each node), that could be either a valid binary heap or a valid binary search tree. e. (2 points) Draw the binary search tree that results if you start with an empty binary search tree and insert the following keys in the following order: 0, 8, 2, 5, 3, 7, 1; then remove 8. f. (2 points) Your friend tells you that his binary search tree implementation rebalances the tree after every insert and delete operation, so that it always has the minimum possible height. This seems like a good idea, because the height of the search tree never exceeds log 2 n. But it's really a terrible idea, because (unlike with a 2-3-4 tree) it cannot be done quickly. Give an argument why your friend's implementation must sometimes take Ω( n ) time per operation. (You get to pick the tree and the sequence of insert/delete operations.) g. (1 bonus point ) What is the central tenet of Britney Theory?

Problem 2. (7 points) Trees. a. (2 points) Draw the height-2 binary tree whose postorder traversal is X R L T M S W. b. (2 points) Draw the following 2-3-4 tree after you execute remove(24) on it. c. (3 points) The input to the following BinaryTreeNode constructor is an array entries of Entry objects, and two array indices first and last. The constructor creates a binary tree that contains all the entries from entries[first] to entries[last] in an “inorder” traversal. The tree should be balanced as perfectly as possible. Fill in the blanks. class BinaryTreeNode { Entry entry; BinaryTreeNode parent; BinaryTreeNode leftChild, rightChild; public BinaryTreeNode (Entry[] entries, int first, int last) { parent = null; leftChild = null; rightChild = null; int mid = ______________________________; entry = entries[mid]; if (mid > first) { leftChild = new BinaryTreeNode (entries, __________, __________); leftChild.parent = _________________________________________; } if (mid < last) { rightChild = new BinaryTreeNode (entries, __________, __________); rightChild.parent = _________________________________________; } } }

Problem 4. (6 points) Undirected Graphs. Consider the following adjacency matrix for a weighted undirected graph. Hyphens represent missing edges. Draw the graph next to the matrix. Label each vertex with a letter from A to F, where A is represented by the first row and column, and F by the last row and column.

[

]

a. (1 point) In what order are the vertices visited by a depth-first search (DFS), starting at vertex A? When you have a choice of edges to follow, order them alphabetically by destination. b. (1 point) In what order are the vertices visited by a breadth-first search (BFS), starting at vertex E? When you have a choice of edges to follow, order them alphabetically by destination. c. (2 points) Draw the minimum spanning tree of the graph. d. (2 points) A binary search tree is a special instance of a graph. Describe (in English) an algorithm that takes as input a binary search tree, and produces as output an adjacency list representing it as an undirected graph. Your algorithm must run in Ο ( n ) time on an n -node tree.