Exam 2 Answers
Section 02

  1. D
  2. E
  3. B
  4. B
  5. False
  6.    2
       3
       10
       26
    
  7.    public void setNth (int loc, T value) {
          if (loc > numElements -1)
             throw new QueueUnderflowException();
          int i = 0;
          T hold = null;
          LLNode<T> curr = front;
          for (i = 0 ; i < loc ; i++)
             curr = curr.getLink();
          curr.setInfo(value);
       }
    
  8. The setNth method will have to start at the beginning of the linked list and access loc + 1 elements to find and return the element at position loc. In the worst case loc is N-1, which gives a complexity of O(N).
    1.  

    2. This compareTo method is in the Printer class:
         public int compareTo(Printer printer) {
            int eq = manufact.compareTo(printer.manufact);
            if (eq != 0)
               return eq;
            return model.compareTo(printer.model);
         }
      
    3.    SortedABList<Printer> printersSorted = new SortedABList<>();
      
    4. Like the compareTo method, this method is in the Printer class.
    5.    SortedABList<Album> speedSorted = new SortedABList<>(Printer.speedComparator());
      
    6.    public int countAfter(ABList<Printer> prList, Printer printer) {
           int count = 0;
           Printer prFromList;
           Iterator<Printer> prIter = prList.iterator();
           while (prIter.hasNext()) {
              prFromList = prIter.next();
              if (prFromList.compareTo(printer) > 0)
                 count++;
           }
           return count;
         }
      
    1.    public void enqueue(T newItem) {
            elements.add(elements.size(), newItem);
         }
      
    2.    public T dequeue() {
            if (elements.size() == 0)
               throw new QueueUndeflowException();
            T item = elements.get(0);
            elements.remove(0);
            return item;
         }
      
    1. No, it is not balanced. It's out of balance at 470.
    2. No, it's not a BST because 365 is in the right subtree of 375 and 485 is in the left subtree of 470.
    3. The height is equal to the maximum level of a node. The maximum level is 4 (nodes 199, 432, and 485) so the height is 4.
    4. pre: 375 255 170 109 233 199 282 303 470 417 365 461 432 485 566
      in: 109 170 199 233 255 282 303 375 365 417 432 461 485 470 566
      post: 109 199 233 170 303 282 255 365 432 485 461 417 566 470 375


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2023