Complete Binary Tree - Data Structures - Exams, Exams of Data Structures and Algorithms

Main points of this exam paper are: Complete Binary Tree, Positional Tree, Positive Integers, Intervals Between, English Sentences, Closed Intervals, Algorithms, Data Structures, Pseudo-Code, Open Intervals

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

72 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61B, Spring 1997
Midterm #2
Professor Hilfinger
/* Definition of java.util.Enumeration */
public interface Enumeration {
/** True if this Enumeration has more elements to produce. */
public abstract boolean hasMoreElements();
/** Returns the next element to be produced by this Enumeration,
* and advances the Enumeration to the following element. */
public abstract Object nextElement();
}
class List {
public Object head; /* First element. */
public List tail; /* Rest of list. */
public List(Object head, List tail)
{
this.head = head; this.tail = tail;
}
}
Problem #1 [6 points]
For each of the following, either indicate that it is true and briefly tell why or give a counter-example.
a. A binary tree of height h contains O(2^(2h)) nodes.
b. A binary tree containing N nodes has height Big-Omega(lg N).
c. A complete binary tree of height h contains theta(2^(h-3)) nodes.
d. If I add K items to the beginning of a list of N items, and if those K new items are all smaller than the
remaining N, then the number of inversions in the list increases by O(K^2).
e. A theta(N) algorithm is always preferable to a theta(N^2) algorithm.
f. A certain hash table contains N integer keys, all distinct, and each of its bins contains at most K elements.
Assuming that the hashing function and the equality test require constant time, the time required to find all
keys in the hash table that are between L and U is O(K*(U-L)) in the worst case.
CS61B, midterm 2, Spring 1997
CS61B, Spring 1997course, semester/year Midterm #2exam # Professor Hilfingerprofessor (e.g., Professor J. Wawrzynek)1
pf3
pf4

Partial preview of the text

Download Complete Binary Tree - Data Structures - Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS61B, Spring 1997

Midterm

Professor Hilfinger

/* Definition of java.util.Enumeration / public interface Enumeration { /* True if this Enumeration has more elements to produce. / public abstract boolean hasMoreElements(); /* Returns the next element to be produced by this Enumeration,

  • and advances the Enumeration to the following element. */ public abstract Object nextElement(); }

class List { public Object head; /* First element. / public List tail; / Rest of list. */

public List(Object head, List tail) { this.head = head; this.tail = tail; } }

Problem #1 [6 points]

For each of the following, either indicate that it is true and briefly tell why or give a counter-example.

a. A binary tree of height h contains O (2^(2 h )) nodes.

b. A binary tree containing N nodes has height Big-Omega(lg N ).

c. A complete binary tree of height h contains theta(2^( h -3)) nodes.

d. If I add K items to the beginning of a list of N items, and if those K new items are all smaller than the remaining N , then the number of inversions in the list increases by O ( K ^2).

e. A theta( N ) algorithm is always preferable to a theta( N ^2) algorithm.

f. A certain hash table contains N integer keys, all distinct, and each of its bins contains at most K elements. Assuming that the hashing function and the equality test require constant time, the time required to find all keys in the hash table that are between L and U is O ( K *( U - L )) in the worst case.

CS61B, Spring 1997course, semester/year Midterm #2exam # Professor Hilfingerprofessor (e.g., Professor J. Wawrzyn 1

Problem #2 [1 point]

If a and m are relatively prime, and phi ( m ) denotes the number of positive integers less than and relatively prime to m , what is a ^ phi ( m ) mod m?

Problem #3 [6 points]

You are given a set of ordered pairs of real numbers, ( Ai , Bi ), satisfying 0 <= Ai <= Bi <= 1 for 0 <= i < N. Consider each pair as representing an interval on the real line: ( Ai , Bi ) represents [ Ai .. Bi ] = {x | Ai <= x <= Bi } As quickly as possible, I would like to find and print the endpoints of all intervals between 0 and 1 of numbers that are not contained in any of these intervals. For example, if the inputs are

(0.1, 0.25), (0.0, 0.2), (0.8, 0.9), (0.4, 0.6), (0.35, 0.65)

the output should be

(0.25, 0.35), (0.65, 0.8), (0.9, 1.0)

in any order. For the purposes of this problem, we'll ignore the distinction between closed intervals--those that include their endpoints--and open intervals--those that do not. Present the fastest algorithm you can think of to do this. Keep things at a resonably high level. You may assume that you have available to you implementations oof any of the data structures and algorithms we've discussed, without going into details of how they are implemented. You may use pseudo-code--that is, English sentences such as "store the Ai in has table H ..." or "heapify the array of Bi 's" without going into further detail.

Problem #4 [7 points]

The data type KTree is a positional tree in which all labels are at the leaves. It has the following (partial) definition:

class KTree { /** The number of non-empty children of this node. / public int degree() ... /* Child number K of this (null inddicates an empty child). / public KTree child(int k) ... /* The label of this node. Valid iff degree() == 0. / public Object label() ... /* An enumeration of all labels in this tree, in any order. */ public Enumeration allLabels() ...

Assume that degree, child, and label are already written. You are to supply a definition of allLabels. For example, if T is a KTree, I want to be able to write

Enumeration p; p = T.allLabels(); while (p.hasMoreElements()) System.out.println(p.nextElement().toString());

Problem #2 [1 point] 2

(where merge2 is the familiar procedure for merging two lists in time proportional to the total number of items in the two lists). What is the worst-case time bound on this implementation and why?

b. Fill in the body below to fulfill the comment. Keep your solution at as high a level as possible. You may assume that doubly-linked lists, queues, deques, stacks, hash tables, balanced binary search trees, and priority queues are provided, if any of these are useful to you; again, just state your assumptions. Again, your program must run in O ( N lg M ) time. Assume that the operation lt compares two items in the lists for /** Assumes that lists, L[0],... are sorted. Returns the result of * merging the lists L[0],...,L[M-1] into a single sorted list. * The operation may be destructive; it may destroy the original lists. */ static List mergeSet(List[] L) { // FILL IN }

Posted by HKN (Electrical Engineering and Computer Science Honor Society)

University of California at Berkeley

If you have any questions about these online exams

please contact [email protected].

Problem #5 [6 points] 4