140 likes | 245 Views
Post Exam 2 Review. Question 1. Write a recursive method even that takes as a parameter a LinkedNode <E > and returns a boolean . If the parameter is null or refers to a linked list of even length, the method should return true . Otherwise the method should return false . .
E N D
Question 1 • Write a recursive method even that takes as a parameter a LinkedNode<E>and returns a boolean. If the parameter is null or refers to a linked list of even length, the method should return true. Otherwise the method should return false. public boolean even(LinkedNode<E> list) { } if (list == null) { } return true; return !even(list.next);
Question 2 • Implement a recursive method to add a value to a binary search tree (correctly handling the case where the value is already in the tree). The method is originally call by this public add method (the declaration of root is also shown, and T extends java.lang.Comparable<T>.) protected BinaryNode<T> root; public void add(T value) { root = add(value, root); } This question has a typo and will not work. Everybody gets 20 points.
Question 3 • Implement the following method. public class ArrayStack<E> implmentsStackInterface<E> { private int top; private E[] array; public E pop() throws EmptyStackException { } if (top == 0) { // or if (empty()) { throw new EmptyStackException(); } return array[top--];
Question 4 • Implement this method to perform a binary search. If the value is found, the method returns the index of the value, otherwise the method returns -1. The array data is sorted in ascending order. static intbinarySearch(java.lang.Comparable value, Object[] data) { }
Question 4 Looping static intbinarySearch(java.lang.Comparable value, Object[] data) { } int left = 0; int right = data.length – 1; int mid = (left + right) / 2; while (left < right && value.compareTo(data[mid]) != 0) { } int compare = value.compareTo(data[mid]); if (compare < 0) { } else { } mid = (left + right) / 2; right = mid - 1; left = mid + 1; if (value.compareTo(data[mid]) == 0) { return mid; } return -1;
Question 4 recursive static intbinarySearch(java.lang.Comparable value, Object[] data) { return binSearch(value, data, 0, data.length - 1); } static intbinSearch(java.lang.Comparable value, Object[] data, int left, int right) { } if (left > right) { return -1; // Base case for unsuccessful search } else { int mid = (left + right) / 2; int compare = value.compareTo(data[mid]); } if (compare == 0) { return mid; } else if (compare < 0) { return binSearch(value, data, left, mid – 1); } else { return binSearch(value, data, mid + 1, right); }
Question 5 • Create a max heap (stored in an array) by inserting in sequence, the values 5, 2, 4, 7, 6, 8, 1, 3. Show the resulting array. 5 7 8 2 7 5 6 7 8 4 7 2 3 5 6 8 4 1 2 3 8, 6, 7, 3, 5, 4, 1, 2
Question 6 • Which of the following is a preorder traversal of a binary tree?A) Visit root node, traverse TR, traverse TLB) Traverse TL, traverse TR, visit root nodeC) Visit root node, traverse TL, traverse TRD) Traverse TL, visit root node, traverse TR
Question 7 • What is the worst-case runtime of binary search on a sorted array of n items. O(log n)
Question 8 • Fill in the blank for both of these sentences.The node at the top of a tree is called its ___________.Nodes that have the same parent are ___________. root siblings
Question 9 • A double-linked list requires the same amount of storage as that of a single-linked list.True or False (circle one) private static class SingleNode<E> { E data; LinkedNode<E> next; } private static class DoubleNode<E> { E data; LinkedNode<E> next; LinkedNode<E> prev; }
Question 10 • What is the worst-case runtime to add a value to a heap that has n values? O(log n)
Question 11 • What is the worst-case runtime to add a value to a binary search tree with nvalues? 5 O(n) 4 3 2