CM0551 Exam Prep
What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer memory growth rate, requirement of the algorithm. Determine the time complexity of the following algorithm. Your analysis should count comparisons. To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. Answer Step 1.2 involves an iteration (loop) down or up the array and this is in a loop (step 1) so the time complexity is O(n 2 )
Complete the algorithm below for a method to return the position of the smallest item in array data with bounds 0..limit. positionOfSmallest(data[0..limit]) 1. set smallest = data[0]. 2. set indexOfSmallest = set index = 1. (6 marks) 4 while index is less than or equal limit, repeat: 4.1 if data[index] is less than smallest: set smallest = data[index] set indexOfSmallest = index set index = index Terminate with answer indexOfSmallest.
2 a)The following list gives the requirements of a Stack ADT: It must be possible to make a stack empty. It must be possible to add (‘push’) an element to the top of a stack. It must be possible to remove (‘pop’) the topmost element from a stack. It must be possible to test whether a stack is empty. It should be possible to access the topmost element in a stack without removing it. Write these requirements as a contract, expressed as a Java interface (include suitable header comments). (6 marks)
Answer public interface Stack { // Each Stack object is a stack whose elements are objects. /////////////// Accessors /////////////// public boolean isEmpty (); // Returns true if and only if this stack is empty. public Object getLast (); // Returns the element at the top of this stack. /////////////// Transformers /////////////// public void clear (); // Make this stack empty. public void push (Object elem); // Add elem as the top element of this stack. public Object pop (); // Removes and return the element at the top of this stack. }
The following Java code gives the data structure and constructor of an ArrayStack. public class ArrayStack { private Object[] elems; private int depth; /////////////// Constructor /////////////// public ArrayStack (int maxDepth) { elems = new Object[maxDepth]; depth = 0; } Write additional Java code to implement the methods to clear and add an element to the stack. What is the time complexity of these operations? (6 marks)
Answer public void clear () { for (int i = 0; i < depth; i++) elems[i] = null; depth = 0; } 2 public void push (Object elem) { elems[depth++] = elem; } 2 Operation push has time complexity O(1). Operation clear has time complexity O(n). 2 Could draw dias to help
3 a)Given the following queue contract and the SLL declaration giving the data structure and constructor. public interface Queue { /////////////// Accessors /////////////// public boolean isEmpty (); // Return true if and only if this queue is empty. public int size (); // Return this queue’s length. public Object getFirst (); // Return the element at the front of this queue. /////////////// Transformers /////////////// public void clear (); // Make this queue empty. public void addLast (Object elem); // Add elem as the rear element of this queue.
public Object removeFirst (); // Remove and return the front element of this queue. public String toString (); // Returns a String of all elements in the queue. } public class LinkedQueue implements Queue { private SLLNode front, rear; /////////////// Constructor /////////////// public LinkedQueue () { front = rear = null; } Write Java code to implement the isEmpty and removeFirst methods (include error checking). (5 marks)
public boolean isEmpty () { return (front == null); } 2 public Object removeFirst () { if (front == null) throw …; Object frontElem = front.element; front = front.succ; if (front == null) rear = null; return frontElem; } 3 Could draw dia. to help
Write a Java main method to create a nameQueue and add three names, then remove a name, then add another name. Output the contents of the queue after each of these three steps. (7 marks)
Answer public static void main (String[] args) { //set up the queue LinkedList nameQueue = new LinkedList(); // add three elements to the queue queue.addLast("Homer"); queue.addLast("Marge"); queue.addLast("Bart"); System.out.println(queue.toString()); // remove an element System.out.println(queue.removeFirst()+ " has been removed"); System.out.println(queue.toString()); // add one more element queue.addLast("Lisa"); System.out.println(queue.toString()); }
4 a)Define the terms ‘binary tree’ and ‘binary search tree’. (6 marks) Answer A binary tree consists of a header, plus a number of nodes connected by links in a hierarchical data structure: Each node contains an element (value or object), plus links to at most two other nodes (its left child and right child). The header contains a link to a node designated as the root node. A binary search tree (or BST) is a binary tree with the following property. For any node in the binary tree, if that node contains element elem: Its left subtree (if nonempty) contains only elements less than elem. Its right subtree (if nonempty) contains only elements greater than elem.
b)Implement the following algorithm in Java. To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null: Terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element: Terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element: Set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element: Set curr to curr’s right child.
Given this definition of a BST. public class BSTNode { protected Comparable element; protected BSTNode left, right; protected BSTNode (Comparable elem) { element = elem; left = null; right = null; } … public int compareTo(Comarable elem) … } (8 marks)
Answer public BSTNode search (Comparable target) { int direction = 0; BSTNode curr = root; while(true) { if (curr == null) return null; direction = target.compareTo(curr.element); if (direction == 0) return curr; else if (direction < 0) curr = curr.left; else curr = curr.right; } } Could draw dia to help
c)Draw the resulting structure after inserting the following elements into a BST: mouse, screen, keyboard, modem, network, battery, floppy, and speaker. Write down the order in which elements are visited by an in-order and pre-order transversals. (6 marks)
In-order: Battery, floppy, modem, mouse, network, screen and speaker Pre-order: mouse, modem, battery, floppy, screen, network and speaker floppy battery modem mouse network screen speaker