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.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.
7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues.
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
CHAPTER 7 Queues.
7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
4 Linked-List Data Structures  Linked-lists: singly-linked-lists, doubly-linked-lists  Insertion  Deletion  Searching © 2008 David A Watt, University.
COMP 103 Linked Stack and Linked Queue.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Stacks, Queues & Deques CSC212.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Fundamentals of Python: From First Programs Through Data Structures
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 6.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
Information and Computer Sciences University of Hawaii, Manoa
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
7.2 Priority Queue ADTs Priority queue concepts
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 8 Binary Search Trees. Chapter 8: Binary Search Trees 8.1 – Trees 8.2 – The Logical Level 8.3 – The Application Level 8.4 – The Implementation.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
(c) University of Washington20-1 CSC 143 Java Trees.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Linked Data Structures
Linked Lists, Queues, Stacks
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
The Tree Data Structure
Stacks.
Week 6 - Wednesday CS221.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Linked List (Part I) Data structure.
Stacks with Dynamic Memory
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Tree.
8 List ADTs List concepts. List applications.
CS2005 Week 8 Lectures Maps & Binary Trees.
Presentation transcript:

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