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

Recursive Calls - Data Structures - Solved Exam, Exams of Data Structures and Algorithms

Main points of this past exam are: Recursive Calls, More Efficient, Data Structures, Running Time, Syntax Errors, Binary Tree, Rooted at Node

Typology: Exams

2012/2013

Uploaded on 04/07/2013

seshu_lin3
seshu_lin3 🇮🇳

4

(3)

59 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
22c:021 Computer Science II: Data Structures Name:
Midterm Exam, Fall 2010
Open books and notes; no computers
1. [25 points] FindTriples returns true if there are three equal integers in the array A.
public static boolean FindTriples( int[] A ) {
for (int i = 0; i < A.length; i++)
for (int j = i+1; j < A.length; j++)
for (int k = j+1; k < A.length; k++)
if (A[i] == A[j] && A[i] == A[k] && A[j] == A[k]) return true;
return false;
}
(a) What is the running time of this method?
O(n
3
)
(b) Describe a more efficient algorithm than the one above and give its running time. No code is
needed in your answer.
We may first sort the array using mergesort and then check if A[i-2] == A[i] for i from 2 to
A.length. Sorting takes O(n lg n) and the checking takes O(n). The total time is O(n lg n).
2. [25 points] Suppose the class Link is used to define a linked list:
class Link { public long data;
Link next;
public Link (long d) { data = d; }}
class LinkList {
private Link first; …
public LinkList reverse() { … }
… }
Please complete the method reverse that reverses a linked list without destroying the original list.
Syntax errors are allowed. What is the running time of your implementation?
public LinkList reverse() {
Link current = this.first;
LinkList rev = new LinkList();
while (current != null) {
Link node = new Link(current.data);
node.next = rev.first;
rev.first = node;
current = current.next;
}
return rev;
}
The running time is O(n).
pf2

Partial preview of the text

Download Recursive Calls - Data Structures - Solved Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

22c:021 Computer Science II: Data Structures Name:

Midterm Exam, Fall 2010

Open books and notes; no computers

1. [25 points] FindTriples returns true if there are three equal integers in the array A.

public static boolean FindTriples( int[] A ) {

for (int i = 0; i < A.length; i++)

for (int j = i+1; j < A.length; j++)

for (int k = j+1; k < A.length; k++)

if (A[i] == A[j] && A[i] == A[k] && A[j] == A[k]) return true;

return false;

(a) What is the running time of this method?

O(n^3 )

(b) Describe a more efficient algorithm than the one above and give its running time. No code is

needed in your answer.

We may first sort the array using mergesort and then check if A[i-2] == A[i] for i from 2 to

A.length. Sorting takes O(n lg n) and the checking takes O(n). The total time is O(n lg n).

2. [25 points] Suppose the class Link is used to define a linked list:

class Link { public long data;

Link next;

public Link (long d) { data = d; }}

class LinkList {

private Link first; …

public LinkList reverse() { … }

Please complete the method reverse that reverses a linked list without destroying the original list.

Syntax errors are allowed. What is the running time of your implementation?

public LinkList reverse() { Link current = this .first; LinkList rev = new LinkList(); while (current != null ) { Link node = new Link(current.data); node.next = rev.first; rev.first = node; current = current.next; } return rev; }

The running time is O(n).

3. [25 points] The following method computes the size of a binary tree rooted at node

public static int size( Node x

if (x == null) return 0;

return 1 + size(x.leftChild) + size(

Please provide another implementation of this method

running time of your method?

public static int size(Node x) { if (x == null ) return Stack stack = new int count = 0; while ( true ) { count++; if (x.leftChild != if (x.rightChild x = x.leftChild } else if (x.rightChild else if (!stack.isEmpty()) x = stack.pop(); else break ; } return count; } The running time is O(n).

4. [25 points] Suppose a Red-Black tree is initially empty. Please draw the resulting Red

trees after each insertion of the follo

The following method computes the size of a binary tree rooted at node

ublic static int size( Node x ) {

== null) return 0;

.leftChild) + size(x.rightChild);

provide another implementation of this method without recursive calls.

running time of your method?

Node x) { 0; new Stack();

!= null ) { rightChild != null ) { stack.push(x.rightChild leftChild;

rightChild != null ) x = x.rightChild; (!stack.isEmpty()) x = stack.pop();

The running time is O(n).

Black tree is initially empty. Please draw the resulting Red

trees after each insertion of the following numbers (in that order): 1, 2, 3, 4, 5.

The following method computes the size of a binary tree rooted at node x.

without recursive calls. What is the

rightChild); }

Black tree is initially empty. Please draw the resulting Red-Black

(in that order): 1, 2, 3, 4, 5.