cs 2420 midterm 1
Why do B+ trees have a minimum node size?
Requiring nodes to be half full guarantees that the tree does not degenerate into a simple binary search tree.
Using the formula, what is the complexity of public void doit3(int n) { System.out.println("C"); if (n <=0) return; doit3(n/2); } List the values of a, b, k
a=1, b=2, k=0
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++) x++; doit(n/2); } List the values of a, b, k
a=1, b=2, k=1
A friend wrote a recursive routine to determine if an array contains a specific element. The attempt and its output are shown below. Explain what the problems are and fix the code. Compare your answer with the ones below. Select all correct answers. public static boolean isFound(int[] a, int low, int high, int lookFor) { if (low == high) return false; int mid = (low + high) / 2; return isFound(a, low, mid, lookFor) && isFound(a, mid + 1, high, lookFor);} public static void main(String[] args) { int []a = {1, 62, 15, 16, 17, 18, 192, 21, 55, 62, 63, 64, 65, 67, 68, 74}; int k = 18; boolean success = isFound(a, 0, 15, k); if (success) System.out.println(k + " Is Found"); else System.out.println("Not Found"); k = 74; success = isFound(a, 0, 15, k); if (success) System.out.println(k + " Is Found"); else System.out.println("Not Found");} Output is: Not Found Not Found
&& needs to be || You need to change the first statement in the method to if (low==high) return a[low]==lookFor;
The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum level when the root is at level 0)?
3
Consider the following Binary Search Tree 10 / \ 5 20 / / \ 4 15 30 / 11 If we randomly search one of the keys present in above BST, what would be the expected number of comparisons? Note that you are ONLY searching for things that are in the tree. When you find something you are looking for, you stop.
2.57
An algorithm that requires __________ operations to complete its task on n data elements is said to have a linear runtime. (We use ^ to represent power.)
2n+1
How many asterisks are printed by the following code when n=8? void doit (int n){ if (n <=1) return; System.out.print("*"); doit(n/2); }
3
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree?
0 1 2 3 4 5 6 7 8 9
We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree? If you have an unlabeled tree, it means your have the SHAPE of the tree given to you.
1
How many distinct binary search trees can be created out of 4 distinct keys?
14
What is the maximum height of a binary tree with 16 elements? (Height of a tree with one node is zero.)
15
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?
15, 10, 23, 25, 20, 35, 42, 39, 30
Consider the following AVL tree. Create the tree after insertion of 17 followed by deletion of 26 (assuming deletion replaces the node with the inorder successor, if possible). What is the new root of the tree 26 / \ 14 42 /\ / 5 18 40 / 16
17
What happens if you insert a 2 into the tree below? 05 / \ 3 15 / \ / \ 0 4 8 27 / \ 6 55
2 becomes a child of 00, but no rotation occurs
Create a tree by adding 1 through 8 in order in an initially empty AVL tree. What is the root of the tree?
4
What is the minimum height of a binary tree with 16 elements? The root is at level zero.
4
The following tree is a B+ in which internal nodes can have no more than 3 kids and leaf nodes can have one or two keys. What is the resulting tree after adding 7? 3|5 / | \ 1|2 3|4 5|6
5 / \ 3 7 / \ / \ 1|2 3|4 5|6 7
An AVL tree of height 3 must have at least _____ nodes. (Assume a node with no children has height 0). [Hint: Consider the tree with the fewest nodes which is AVL and height 3.]
7
How many asterisks are printed by the following code when n=8? void doit (int n){ if (n <=1) return; System.out.print("*"); doit(n/2); doit(n/2); }
7
Suppose that we have numbers between 1 and 1000 stored in a binarysearch tree and we want to search for the number 259. Which of the fol-lowing sequences could not be the sequence of keys examined?
999, 112, 602, 253, 411, 110, 259
Suppose we insert a 18 into the AVL tree below, what rotation would be used to fix the valance according to the algorithm we discussed in class? 50 / \ 30 70 / \ / \ 10 40 60 80 \ 20
A double rotation rooted at the node containing 10.
For the AVL tree below, which of the following is correct? 28 / \ 14 40 / \ 12 16
A single rotation is needed to restore the AVL property if any node with value less than 14 is added to the present tree.
Select the incorrect statement. Binary search trees (regardless of the order in which the values are inserted into the tree):
Always have the same shape for a particular set of data
In big O notation, we ignore constant factors. Why? Select all correct answers.
As n gets large, the constant becomes unimportant. If we move to another machine, all the constants associated with the complexity would change. It is difficult to account for every constant as we would have to know a lot about operator costs.
Algorithms which have complexity 2n are considered intractable. Which of the following problems likely have only intractable solutions? (Select all possible answers)
Bin Packing: Suppose we have unlimited number of bins each of unit capacity and n objects with sizes s1, s2,..., sn where the sizes si(i = 1,..., n) are rational numbers in the range 0 < si <=1. Determine the smallest number of bins into which the objects can be packed and find an optimal packing. Towers of Hanoi (where n is the number of disks) Traveling Salesperson problem: Given a collection of cities on a map, a salesman must make a tour of the cities, visiting each once, and returning to the city from which he started in the shortest amount of mileage. The input is a positive integer C and n objects whose sizes are positive integers s1, s2,..., sn. Among all subsets of objects with sum at most C, what is the largest subset sum?
Compare the two problems: Problem 1: I want each of you to shake hands with every other person in the room. Problem 2: I want each of you to shake hands with every other person in the room five times. How do the solutions compare in complexity?
Both have the same complexity.
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++) x++; doit(n-1); doit(n-1); } List the values of a, b, k
Cannot uses the formula for this problem
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++) x++; doit(n-1); } List the values of a, b, k
Cannot uses the formula for this problem
Which of the following problems have solutions (algorithms) that run in ΘΘ(n) time in the worst-case? (Recall, theta is a tight bound: both a lower and an upper bound.) Select all correct answers.
Finding the sum of n integers Finding the largest of n integers
The following routine for check if t is a binary search tree is incorrect. Boolean is Ok(TreeNode<integer> t, int low, int high){ if(t.value > high || t.value< low) return false; if(t==null) return true; return (isOk(t.left, low, t.value) && isOK(t.right, t.value,high); }
Follows a null pointer
Which of the following traversal outputs the data in sorted order in a BST?
In order
The definition of Big O is as follows: Time(n) <= c*f(n) whenever n >= n0. What is the role of c?
It allows us to measure runtime without being concerned with specific constant.
A key 10 is to be deleted from the following B+ tree. This b+ tree is allowed to have two to three children per internal node and two or three records per leaf. 45 / \ 15,30 55 / | \ / \ 5,10 15,20 30,35,40 45,50 55,60 To revalance the tree, what happens to node 15,30?
It is changed to be anode with a single value 30
Given a binary search tree containing integer values, write an algorithm to count the number of nodes storing values greater than "low" and less than "high". Write your own solution and then compare it with the one below. What is true of the code solution below? Select all correct answers. int count( TreeNode t, int low, int high ){ if(t.x < low) return count(t.right, low, high); if(t.x > high) return count(t.left, low, high); return count(t.left, low, high) && count(t.right, low, high); }
Needs to count the root if it is between low and high count(t.left, low, high) && count(t.right, low, high); needs to be count(t.left, low, high) + count(t.right, low, high); Need to add if (t==NULL) return 0;
Print a pattern without using any loop Given a number n, print following a pattern without using any loop. Input: n = 16 Output: 16, 11, 6, 1, -4, 1, 6, 11, 16 Input: n = 10 Output: 10, 5, 0, 5, 10 public static void printPattern(int m) { System.out.print(" " + m); if (m > 0) { System.out.print(" " + m); printPattern(m - 5); } }
No. The first line needs to be moved to the bottom of the method.
If Node A is a proper ancestor of Node B in a Binary Search Tree, which of the following is always true?
Node A is inserted before Node B.
Given the following data, what is the complexity? n Running Time 2 4 4 16 8 256 16 65536 32 4294967296
O(2^n)
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++){ x++; } doit(n-1); doit(n-1); }
O(2^n)
Given the following data, what is the complexity? n Running Time 2 5 4 10 8 15 16 20 32 25
O(log n)
What is the complexity of public void doitA(int n) { System.out.println('C'); if (n <=0) return; doitA(n/2); }
O(log n)
Given the following data, what is the complexity? n Running Time 2 12 4 48 8 144 16 384 32 960
O(n log n)
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++) x++; doit(n/2); doit(n/2); }
O(n log n)
Given the following data, what is the complexity? n Running Time 2 144 4 288 8 576 16 1152 32 2304
O(n)
Given the following data, what is the complexity? n Running Time 2 286 4 572 8 1144 16 2288 32 4576
O(n)
What is the complexity of public void doitB(int n) { System.out.println("C"); if (n <=0) return; doitB(n/2); doitB(n/2); }
O(n)
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++) x++; doit(n/2); }
O(n)
What is the complexity of the code: for (i=0; i < n; i++) x++
O(n)
What is the complexity of the following code: boolean isIsomorphic(treeNode<E> p1, treeNode<E> p2){ if (p1==null){ if(p2 == null) return true; else return false; } if (p2 == null) return false; return isIsomorphic1(p1.left,p2.left) && isIsomorphic(p1.right, p2.right)); }
O(n)
What is the complexity of the following code: int product (int n){ if (n==1) return 1; return n*product(n-1); }
O(n)
Given the following data, what is the complexity? n Running Time 2 12 4 48 8 192 16 768 32 3072
O(n^2)
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++) x++; doit(n-1); }
O(n^2)
What is the complexity of the code: for (i=0; i < n; i++) for (j=0; j < n/2; j++) x++
O(n^2)
What is the complexity of the code: for (i=0; i < n; i++) for (j=i; j < n; j++) // This begins at i x++
O(n^2)
Whatis the complexity of the code: for (i=0; i < n; i++) for (j=0; j< n; j++) x++;
O(n^2)
Given the following data, what is the complexity? n Running Time 2 8 4 64 8 512 16 4096 32 32768
O(n^3)
What is the complexity of the code: for (i=0; i < n; i++) for (j=0; j < n; j++) for (k=0; k < n; k++) x++
O(n^3)
When we say that a data structure has O(log n) amortized analysis for the T operation, what do we mean by that statement?
On average over the entire set of operations on the data structures, operatoin T will take no longer than order log n time.
Given a splay tree below, after a find on 12, what is true? 17 / \ 8 30 / \ / \ 3 9 19 50 \ \ 5 12 / \ 11 15
The children of 12 are 9 and 17
The definition of Big O is as follows: Time(n) <= c*f(n) whenever n >= n0. What is the role of n0?
The condition doesn't need to hold when n is smaller than n0
5 / \ 3 8 / \ / \ 2 4 7 9 / / 1 6 Which of the following about the above tree is NOT true?
The height will be increased if we insert 10.
Here is another proposed solution to the problem of counting how many nodes are between low and high in a BST. Select all correct answers. int count(TreeNode t, int low, int high){ if(t == NULL) return 0; int tempInt = 0; tempInt = tempInt + count(t.left, low, high); if(t.value > low && t.value < high){ tempInt = tempInt + 1; } tempInt = tempInt + count(t.right, low, high); return tempInt; }
This is inefficient as it always traverses the whole tree, even when it doesn't need to. This produces the correct ansers
Here is another solution to the problem of counting how many nodes are between low and high. Select all correct answers. int count( TreeNode t, int low, int high ){ if (t==NULL) return 0; int leftCount = count(t.left, low, high); int rightCount = count(t.right, low, high); if(t.x <= low)return rightCount; if(t.x >= high)return leftCount; return leftCount + rightCount + 1; }
This produces correct results This is inefficient as you traverse the whole tree even when you don't need to.
In performing fibonacci iteratively, why did runtime improve?
We didn't have to recompute the same values over and over again.
Since we have very fast machines, why do we bother trying to make an algorithm more efficient?
When n becomes large, the time required for an inefficient algorithm could become prohibitive.
What is the motivation for using a B+ tree, as opposed to some other balanced tree (such as an AVL tree)? Select all that apply.
When the entire AVL tree won't fit in main memory, the increasing number of disk accesses is problematic. Complete record data is not stored in the interior nodes making them desirable for database systems.
A friend wrote a recursive routine to determine if the contents of an array are ordered. The attempt and its output is shown below. Identify the errors. Select the best answer. [Note, on an exam, you wouldn't be given the list of possible errors. You would just fix the code.] public class test { public static boolean inOrder(int[] a, int low, int high) { if (low > high) return true; if (low == high) {return true;} int mid = (high + low) / 2; return inOrder(a, low, mid) && inOrder(a, mid + 1, high); } public static void main(String[] args) { int [] b = {1, 62, 15, 16, 17, 18, 192, 21, 55, 62, 63, 64, 65, 67, 68, 74}; boolean okay = inOrder(b, 0, 15); if (okay) System.out.println("Is Okay"); else System.out.println("Not Okay"); } } Output is: Is Okay
You must add if (a[mid] > a[mid+1]) return false;
What is a, b, k for the following code? boolean isIsomorphic(treeNode<E> p1, treeNode<E> p2){ if (p1==null){ if(p2 == null) return true; else return false; } if (p2 == null) return false; return isIsomorphic1(p1.left,p2.left) && isIsomorphic(p1.right, p2.right)); }
a=2, b=2, k=0
What is the complexity of public void doit3(int n) { System.out.println("C"); if (n <=0) return; doit3(n/2); doit3(n/2); } List the values of a, b, k
a=2, b=2, k=0
What is the complexity of void doit(int n) { if (n <1) return; for (int i=0; i < n ; i++) x++; doit(n/2); doit(n/2); } List the values of a, b, k
a=2, b=2, k=1
What picture represents the complexity of the following piece of code? void doit(int n){ if (n<=1) return;doit(n/2); doit(n/2);System.out.println(n);}
centered pyramid picture
What picture represents the complexity of the following piece of code? Note we only care about work that depends on n. void doit(int n){ if (n<=1) return; for (int i=0; i < 4; i++) System.out.println("*");doit(n/2);doit(n/2);System.out.println("*");}
centered pyramid picture
An algorithm has complexity O(n). What do you expect to happen to the execution time if the problem size (n) doubles?
doulbes
In a Binary Search Tree with root.element = 10, if we want to insert the element 5, we need to
follow the left child
In a Binary Search Tree, the largest element must
have at most one child.
An algorithm A is made up of two independent algorithms F and G written one after the other. The worst-caseasymptotic time complexities of F and G on a set of n inputs are f(n) and g(n), respectively. Then the com-plexity of the algorithm A is
max {f(n), g(n)}
If a node in a binary search tree has two children, then its in order successor has
no left child
What picture illustrates the complexity of the following code? Remember that throwing out constants means we only care how the total work relates to n (not whether there is 2n or 3n work at each call). We care about the exponent on n. void doit(int n) { if (n <=1) return; for (int i=0;i<n;i++) System.out.println("*");;doit (n/2); for (int i=0;i<n;i++) System.out.println("*"); ;}
rectangle that decreases by half as it goes down. so like a left weighted triangle
What does teh following code do when the input parameters is an AVL tree node rooted at t? private AvlNode<AnyType> myster(AvlNode,AbyType>t){ AvlNode<AnyType> kid = t.left; t.left = kid.right; kid.right = t; t.height = Math.max( height(t.left), height(t.right)) +1; kid.height = Math.max(height(kid.left), t.height) +1; return kid; }
right rotation
In order to get the information stored in a binary search tree in the descending order, one should recursivelytraverse it in the following order.
right subtree, root, left subtree
What picture represents the complexity of the following piece of code? void doit(int n){ if (n<=1) return; doit(n/2); System.out.print( n); }
stacked rectangle picture
Node A and Node B are leaf nodes in a 1000-node tree where all nodes have an additional reference that points upward, to the parent. A and B are not siblings, and the overall tree is reasonably well-balanced. If you are going to move from Node A to Node B, which of the nodes below are you NOT guaranteed to pass through as you move along the shortest path from A to B? (Select all correct answers.)
the root grandparent of A
Is the following tree an AVL tree? If not, indicate which node violates the AVL property. 20 / \ 15 35 / / \ 10 25 55 /\ 22 30
yes it is
is the following tree an AVL tree? If not, indicate which node violates the AVL property. 05 / \ 4 15 / / \ 0 8 27 \ 55
yes it is
In the tree below, what happens when I insert 70? 20 / \ 15 35 / \ / \ 10 16 25 55 /\ \ 22 30 60
you perform a left rotation at 55