






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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Main points of this exam paper are: Lowest Leaf, Question Carefully, Implemented, Lowest Leaf, Each Element, Dimensional Array, Complexity
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!
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.
(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
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]);
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).
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.
public Queue merge(Queue Q1, Queue Q2){
(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; /*
Listing 2: The Binary Search Tree Class. import java.util.; public class BST{ private Node root; public BST(){ root = null; } /*
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(); } } }