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

Lowest Leaf - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Lowest Leaf, Question Carefully, Implemented, Lowest Leaf, Each Element, Dimensional Array, Complexity

Typology: Exams

2012/2013

Uploaded on 04/07/2013

sethuraman_h34rt
sethuraman_h34rt 🇮🇳

4.3

(8)

159 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
15-111 : Advanced Programming
Final Exam Summer 2008
June 27, 2008
Name: ______________________________________
Andrew ID: ___________________________________
Answer the questions in the space provided following each question. We must be able to clearly
understand your answer. If it is vague or confusing, it will be marked wrong. Be sure to read the
directions to each question carefully. Answers to most questions are NOT long. Just carefully
think about it before you write the answer. This exam is worth 20% of your final grade. You
have 120 minutes.
1. Runtime Complexity.
(a) Assume BST is an implemented class and you have a method height(), which has an
optimal implementation, that finds the height of a balanced binary tree. What is the
worst-case runtime complexity of the following code and why?
public int totalHeight(ArrayList<BST> list){
int sum = 0;
for each (tree in list)
sum += height(tree);
return sum;
}
Assume that list contains M elements, and each binary tree is balanced and contains N elements.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lowest Leaf - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

15-111 : Advanced Programming Final Exam Summer 2008 June 27, 2008

Name: ______________________________________

Andrew ID: ___________________________________

Answer the questions in the space provided following each question. We must be able to clearly understand your answer. If it is vague or confusing, it will be marked wrong. Be sure to read the directions to each question carefully. Answers to most questions are NOT long. Just carefully think about it before you write the answer. This exam is worth 20% of your final grade. You have 120 minutes.

  1. Runtime Complexity.

(a) Assume BST is an implemented class and you have a method height(), which has an optimal implementation, that finds the height of a balanced binary tree. What is the worst-case runtime complexity of the following code and why?

public int totalHeight(ArrayList list){ int sum = 0; for each (tree in list) sum += height(tree); return sum; }

Assume that list contains M elements, and each binary tree is balanced and contains N elements.

(b) You have a method minLeafLevel() that finds the level of the lowest leaf. The method is as implemented below. What is its worst-case runtime complexity and why? Justify your answer. Assume that a binary tree has N elements.

public int minLeafLevel() { return minLeafLevel(root); }

private int minLeafLevel(Node p) { if (p == null) return -1; if (p.left == null) return 1 + minLeafLevel(p.right); if (p.right == null) return 1 + minLeafLevel(p.left); else return 1 + Math.min(minLeafLevel(p.left), minLeafLevel(p.right)); }

(c) You have a method foobar(X) that iterates over each element in a one-dimensional array X. What is the time complexity of the following code? Describe in terms of O(1), O(n), O(n^2 ) or O(n^3 ). You need to justify the answer.

String [ ][ ] str = new String[n][n]; for (int k = 0; k < n; k++) foobar(str[k]);

  1. Recursion. (a) Consider the following methods:

public void print1(int n, int m) { if (m < n) { System.out.print("*"); print1(n, m+1); } else if (m > n) { System.out.print("-"); print1(n, m-1); } }

public void print2(int n, int m) { if (n > 0) { print1(n, 0); print1(n, m); System.out.println(); print2(n-1, m+1); } } Show the output that would be produced by the call print2(5,5).

  1. Sorting.

Given a million objects in memory (assume we have no memory constraints) all of type HighSchoolStudent:

public class HighSchoolStudent { private String name; private int numberOfYearsOld; }

What sorting algorithm would you use, and why would you use it, to sort these objects by the second field numberOfYearsOld? Consider all sorting algorithms we discussed and justify why you would use a particular algorithm. Pay careful attention to type of the sort key.

  1. Stacks and Queues. (a) Given two FIFO queues with data in sorted order, move all data to a third queue so that the third queue ends up with data again in sorted order. Implement your algorithm and give its worst-case runtime complexity. You may use all the standard queue operations. No data structure other than Queue can be used.

public Queue merge(Queue Q1, Queue Q2){

  1. Binary Search Tree. (a) Given an array of numbers: 19, 6, 8, 11, 4, 13, 5, 27, 43, 49, 31, 25 Draw a binary search tree by inserting the above numbers from left to right and then show the resulting tree after 19 is removed. Assume the BST class given in Listing 2.

(b) Given an array of numbers: 19, 6, 8, 11, 4, 13, 5, 27, 43, 49, 31, 25 Create an AVL tree by inserting and balancing the tree using single or double rotations. Show all

steps.

(c) Draw a binary tree T such that each node stores a single number and a preorder traversal of T yields 15,7,4,9,12,10,17,22,20,18,25 and an inorder traversal of T yields 4,7,9,10,12,15,17,18,20,22,25.

Listing 1: The Linked List Class. import java.util.; public class LinkedList { private Node head; /*

  • Constructs an empty list / public LinkedList() { head = null; } /*
  • Returns true if the list is empty / public boolean isEmpty() { return head == null; } /*
  • Inserts a new node at the beginning of this list. / public void addFirst(Object item) { head = new Node(item, head); } /*
  • Returns a string representation */ public String toString() { StringBuffer result = new StringBuffer(); for(Node tmp = head; tmp != null; tmp = tmp.next) result.append(tmp.data + " "); return result.toString(); } private static class Node { private Object data; private Node next; public Node(Object data, Node next) { this.data = data; this.next = next; } } }

Listing 2: The Binary Search Tree Class. import java.util.; public class BST{ private Node root; public BST(){ root = null; } /*

  • Inserts a new item / public void insert(Comparable data){ root = insert(root, data); } private Node insert(Node p, Comparable toInsert){ if (p == null) return new Node(toInsert); if (toInsert.compareTo(p.data) == 0) return p; if (toInsert.compareTo(p.data) < 0) p.left = insert(p.left, toInsert); else p.right = insert(p.right, toInsert); return p; } /*
  • Searches for an item */ public boolean search(Comparable toSearch){

return search(root, toSearch); } private boolean search(Node p, Comparable toSearch){ if (p == null) return false; else if (toSearch.compareTo(p.data) == 0) return true; else if (toSearch.compareTo(p.data) < 0) return search(p.left, toSearch); else return search(p.right, toSearch); }

private static class Node { private Comparable data; private Node left, right; public Node(Comparable data) { left = right = null; this.data = data; } public Node(Comparable data, Node l, Node r){ left = l; right = r; this.data = data; } public String toString() { return data.toString(); } } }