Algorithms and Data Structures Written Exam Proposed SOLUTION



Similar documents
A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Ordered Lists and Binary Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Binary Search Trees (BST)

Binary Heap Algorithms

Chapter 14 The Binary Search Tree

Data Structures and Algorithms

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Binary Heaps. CSE 373 Data Structures

The ADT Binary Search Tree

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Why Use Binary Trees?

Analysis of Algorithms I: Binary Search Trees

DATA STRUCTURES USING C

10CS35: Data Structures Using C

LINKED DATA STRUCTURES

Java Software Structures

PES Institute of Technology-BSC QUESTION BANK

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures and Algorithms Written Examination

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Analysis of a Search Algorithm

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Alex. Adam Agnes Allen Arthur

Data Structures. Level 6 C Module Descriptor

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Trees and Huffman Encoding Binary Search Trees

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Cpt S 223. School of EECS, WSU

Data Structure with C

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

COMPUTER SCIENCE. Paper 1 (THEORY)

Full and Complete Binary Trees

Binary Search Trees. Ric Glassey

Graduate Assessment Test (Sample)

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

From Last Time: Remove (Delete) Operation

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Binary Search Trees CMPSC 122

CompSci-61B, Data Structures Final Exam

Converting a Number from Decimal to Binary

Analysis of Binary Search algorithm and Selection Sort algorithm

6 March Array Implementation of Binary Trees

CmpSci 187: Programming with Data Structures Spring 2015

Lecture Notes on Binary Search Trees

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Binary Search Trees 3/20/14

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Chapter 3: Restricted Structures Page 1

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Universidad Carlos III de Madrid

AP Computer Science AB Syllabus 1

OPTIMAL BINARY SEARCH TREES

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

TREE BASIC TERMINOLOGIES

Chapter 7: Sequential Data Structures

Sample Questions Csci 1112 A. Bellaachia

Programming with Data Structures

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Unordered Linked Lists

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Recursive Implementation of Recursive Data Structures

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Algorithms and Data Structures

Exam study sheet for CS2711. List of topics

Algorithms Chapter 12 Binary Search Trees

Persistent Binary Search Trees

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Zabin Visram Room CS115 CS126 Searching. Binary Search

7.1 Our Current Model

6 Creating the Animation

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Searching Algorithms

Analysis of Algorithms I: Optimal Binary Search Trees

Lecture Notes on Binary Search Trees

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Transcription:

Algorithms and Data Structures Written Exam Proposed SOLUTION 2005-01-07 from 09:00 to 13:00 Allowed tools: A standard calculator. Grading criteria: You can get at most 30 points. For an E, 15 points are required. For an D, 18 points are required. For an C, 21 points are required. For an B, 24 points are required. For an A, 27 points are required. Responsible: Verónica Gaspes, telephone 16 7380. Read carefully! Write clearly! Good Luck! 1

1. (5 pts.) The following program fragment is taken from the code distributed with the laboration on sequence alignment. Given the two argument strings, it fills a matrix with the costs for the optimal alignments of all prefixes of both strings. Assuming that the operations length, min, d and charat are constant time, give a Big-Oh analysis of the execution time of the fragment. public SequenceAlignment1(String a, String b) n = a.length(); m = b.length(); M = new int[n+1][m+1]; for(int i = 1; i<=n; i++) M[i][0] = i; for(int j = 1; j<=m; j++) M[0][j] = j; for(int i = 1; i<=n; i++) for(int j = 1; j<=m; j++) M[i][j] = min(m[i-1][j-1]+d(a.charat(i-1),b.charat(j-1)), M[i-1][j]+1, M[i][j-1]+1); The first for-loop does N iterations, where N is the length of string a. The second one does M iterations, where M is the length of string b. The last one does M N iterations, and this is the dominant term. We say then that the algorithm is O(M N), or if we consider strings of the same length, say L, the algorithm is O(L 2 ) 2. (a) (5 pts.) Design an algorithm for searching for a given element X in an ordered array A. You should use the technique known as Divide and Conquer. Describe your algorithm briefly. Say the array A has length N > 1. We can consider the element in the middle of the array and the two strictly smaller slices of the array before and after the middle element (the divide phase). The sought element X will either be equal to the element in the middle (in which case it is found!) or it will be smaller (and then it can be sought in 2

the lower part of the array) or it will be bigger (and then it can be sought in the higher part of the array). In this case there is nothing left to do when the solution is found (the conquer phase is trivial) In case the array has length 1 the element X is compared with the only element of the array In case the array has length 0 the element X is not present in the array. (b) (5 pts.) Implement your algorithm in Java as a method static int find(comparable x, Comparable[] a) that returns the position of x in a (or -1 in case x is not in a). Here is a possible program: static int find( Comparable [ ] a, Comparable x ) return binarysearch( a, x, 0, a.length -1 ); static int binarysearch( Comparable [ ] a, Comparable x, int low, int high ) if(low > high) //slice length == 0 return -1; //not found if (low == high) //slice length == 1 return (a[low].compareto(x)==0)?low:-1; //check the only element int mid = (low + high) / 2; if(a[ mid ].compareto(x) < 0) return binarysearch( a, x, mid + 1, high ); //search in the higher else if(a[ mid ].compareto(x) > 0) return binarysearch( a, x, low, mid - 1 ); //search in the lowe else return mid; //found! 3. (5 pts.) Queues can be implemented in many different ways. In the lectures we presented an implementation where the elements of the queue are stored in an array. In this exercise you will complete an implementation where the elements are stored in nodes that are linked together. New nodes are created as the queue grows and removed as the queue shrinks. In this way, at any time only the memory needed to store the elements in the queue is used. For this exercise you are provided with the class that implements nodes and part of the implementation of queues using these nodes. 3

Your task is to program all methods where the text // YOUR TASK! has been included as method body in the class Queue. class ListNode public Object element; public ListNode next; // Constructors public ListNode( Object theelement ) this( theelement, null ); public ListNode( Object theelement, ListNode n ) element = theelement; next = n; // ListQueue class // // CONSTRUCTION: with no initializer // // ******************PUBLIC OPERATIONS******************* // void enqueue(x) --> Insert x // Object getfront()-->return least recently inserted item // Object dequeue() -->Return and remove least recent item // boolean isempty()--> Return true if empty; else false // void makeempty() --> Remove all items * List-based implementation of the queue. * @author Mark Allen Weiss public class ListQueue private ListNode front; private ListNode back; 4

* Construct the queue. public ListQueue( ) front = back = null; * Test if the queue is logically empty. * @return true if empty, false otherwise. public boolean isempty( ) return front == null; * Insert a new item into the queue. * @param x the item to insert. public void enqueue( Object x ) // YOUR TASK! * Return and remove the least recently inserted item * from the queue. * @return the least recently inserted item in the * queue. public Object dequeue( ) // YOUR TASK! 5

* Get the least recently inserted item in the queue. * Does not alter the queue. * @return the least recently inserted item in the * queue. public Object getfront( ) // YOUR TASK! * Make the queue logically empty. public void makeempty( ) // YOUR TASK! Here is the code provided with the course book: public void enqueue( Object x ) if( isempty( ) ) // Make queue of one element back = front = new ListNode( x ); else // Regular case back = back.next = new ListNode( x ); // has to be used on non-empty queues! public Object dequeue( ) Object returnvalue = front.element; front = front.next; return returnvalue; // has to be used on non-empty queues! public Object getfront( ) 6

return front.element; public void makeempty( ) front = null; back = null; 4. In the course we discussed a naive implementation of Binary Search Trees. This implementation uses a binary node to represent the root of the binary search tree and implements the methods for insertion, deletion and search recursively. You find part of the implementation after the text for this exercise. (a) (2 pts.) Study the constructor and the method insert in the class BinarySearchTree. Draw the tree resulting from the following sequence of statements: BinarySearchTree bst = new BinarySearchTree(); bst.insert(new Integer(1)); bst.insert(new Integer(2)); bst.insert(new Integer(3)); bst.insert(new Integer(4)); bst.insert(new Integer(5)); bst.insert(new Integer(6)); 1 2 Here is the tree: 3 4 5 6 7

(b) (2 pts.) What is the execution time of the operation find(comparable a, BinaryNode t) for a tree of the form you obtained above? (see the implementation of find in the class BinarySearchTree!) The execution time is in the order of the number of elements. (c) (1 pts.) Given a binary search tree consider outputing its elements by traversing the tree in inorder. What can you say about the order of the elements in the output? The elements will be output so that they are ordered from lower values to higher values. (d) (2 pts.) Imagine you have to write a program that has to deal with a large data set where you have to do frequent searches. You choose to store the elements in a binary search tree. When the program starts it loads the elements form a file into the binary search tree by doing repeated insertions. When the program terminates it stores the elements back into the file. What strategy will you choose to traverse the tree to output its elements to the file? Why? By traversing the tree in preorder, when building the tree again by repeated insertions the shape of the tree will be preserved. If it was balanced it will remain balanced! (e) (3 pts.) Implement the operation Comparable findmin() in the class BinarySearchTree that returns the least element in a binary search tree. Here it is as in the course book: * Find the smallest item in the tree. * @return smallest item or null if empty. public Comparable findmin( ) return elementat( findmin( root ) ); * Internal method to find the smallest item in a subtree. * @param t the node that roots the tree. * @return node containing the smallest item. protected BinaryNode findmin( BinaryNode t ) if( t!= null ) while( t.left!= null ) 8

t = t.left; return t; 9

class BinaryNode // Friendly data; // accessible by other package routines Comparable element; // The data in the node BinaryNode left; // Left child BinaryNode right; // Right child // Constructors BinaryNode( Comparable theelement ) element = theelement; left = right = null; // BinarySearchTree class // CONSTRUCTION: with no initializer // ******************PUBLIC OPERATIONS****************** // void insert( x ) --> Insert x // void remove( x ) --> Remove x // void removemin( ) --> Remove minimum item // Comparable find( x ) --> Return item that matches x // Comparable findmin( ) --> Return smallest item // Comparable findmax( ) --> Return largest item // boolean isempty( ) --> Return true if empty; // else false // void makeempty( ) --> Remove all items public class BinarySearchTree The tree root. protected BinaryNode root; * Construct the tree. public BinarySearchTree( ) 10

root = null; * Insert into the tree. * @param x the item to insert. public void insert( Comparable x ) root = insert( x, root ); * Internal method to insert into a subtree. * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. protected BinaryNode insert( Comparable x, BinaryNode t) if( t == null ) t = new BinaryNode( x ); else if( x.compareto( t.element ) < 0 ) t.left = insert( x, t.left ); else if( x.compareto( t.element ) > 0 ) t.right = insert( x, t.right ); return t; * Find an item in the tree. * @param x the item to search for. * @return the matching item or null if not found. public Comparable find( Comparable x ) return elementat( find( x, root ) ); private Comparable elementat( BinaryNode t ) return t == null? null : t.element; 11

* Internal method to find an item in a subtree. * @param x is item to search for. * @param t the node that roots the tree. * @return node containing the matched item. private BinaryNode find( Comparable x, BinaryNode t) while( t!= null ) if( x.compareto( t.element ) < 0 ) t = t.left; else if( x.compareto( t.element ) > 0 ) t = t.right; else return t; // Match return null; // Not found // OTHER METHODS NOT SHOWN HERE! 12