1 / 14

Post Exam 2 Review

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 . .

joshwa
Download Presentation

Post Exam 2 Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Post Exam 2 Review

  2. 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);

  3. 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.

  4. 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--];

  5. 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) { }

  6. 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;

  7. 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); }

  8. 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

  9. 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

  10. Question 7 • What is the worst-case runtime of binary search on a sorted array of n items. O(log n)

  11. 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

  12. 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; }

  13. Question 10 • What is the worst-case runtime to add a value to a heap that has n values? O(log n)

  14. 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

More Related