Computer Science 136 Exam 2

Similar documents
9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

COMP 250 Midterm #2 March 11 th 2013

Computer Science 136. Midterm Examination

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

COS 226 Algorithms and Data Structures Fall Midterm

CSE 214 Computer Science II Heaps and Priority Queues

COMP 103. Data Structures and Algorithms

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Programming II (CS300)

CS171 Midterm Exam. October 29, Name:

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

CS171 Final Practice Exam

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Programming II (CS300)

CS 151. Binary Trees. Friday, October 5, 12

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Te Whare Wananga o te Upoko o te Ika a Maui EXAMINATIONS 2003 END-YEAR COMP 103. Data Structures and Algorithms

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Introduction to Computing II (ITI 1121) Final Examination

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Exam Data structures DAT036/DAT037/DIT960

Name CPTR246 Spring '17 (100 total points) Exam 3

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Summer Final Exam Review Session August 5, 2009

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS 216 Exam 1 Fall SOLUTION

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

Lecture 12: Doubly Linked Lists

CSE373 Fall 2013, Midterm Examination October 18, 2013

CS171 Final Practice Exam

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Tree Travsersals and BST Iterators

CE 221 Data Structures and Algorithms

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Largest Online Community of VU Students

Lecture 13: AVL Trees and Binary Heaps

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CS350: Data Structures Heaps and Priority Queues

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

CSE 2123: Collections: Priority Queues. Jeremy Morris

Computer Science E-22 Practice Final Exam

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Problem Set 6 Due: 6pm Friday, November 12

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Total Score /1 /20 /41 /15 /23 Grader

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

182 review 1. Course Goals

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Computer Science 62. Midterm Examination

Algorithms and Data Structures

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

UNIVERSITY REGULATIONS

Recall: Properties of B-Trees

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

CS301 All Current Final Term Paper Subjective 2013 Solved with refernces

Data Structures Question Bank Multiple Choice

Basic Data Structures (Version 7) Name:

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Data Structures and Algorithms, Winter term 2017 Practice Assignment 10

CS301 - Data Structures Glossary By

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Sorting and Searching

Priority Queues, Binary Heaps, and Heapsort

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2, CS2110. SOLUTION

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Binary Trees: Practice Problems

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

EE 368. Weeks 5 (Notes)

Exam Data structures DIT960/DAT036

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CmpSci 187: Programming with Data Structures Spring 2015

ITI Introduction to Computing II

INF2220: algorithms and data structures Series 1

Heaps. Heaps. A heap is a complete binary tree.

Sorting and Searching

Transcription:

Computer Science 136 Exam 2 Sample exam Show all work. No credit will be given if necessary steps are not shown or for illegible answers. Partial credit for partial answers. Be clear and concise. Write your name on each page of the exam. There are 102 points available, but the maximum score is 100. You may use one page of handwritten notes (turn this in also). The use of other reference materials or electronic devices is a violation of the honor code. Good luck!! Question Points Earned Points Available 1 23 2 15 3 15 4 15 5 16 6 18 Total 102 Your name (please print): I have neither given nor received aid on this examination: (sign here) 1

1. (23 points) Short answers. Show your work and justify answers where appropriate. a. Free bonus points given in class. What is the answer given in class for the first question on exam 2? (2 points) Skip this question. b. When is it advantageous to use a splay tree instead of a regular binary search tree? (3 points) Skip this question. c. A tree with n elements is both a min-heap and a binary search tree. What does it look like? (3 points) Skip this question. d. Which tree traversal would you use to print an expression tree in human-readable form? (2 points) e. Which tree traversal would you use to evaluate an expression tree? (2 points) 2

f. Given a SinglyLinkedList containing n elements, what is the complexity (Big O) of removefirst()? of removelast()? (4 points) g. Given a DoublyLinkedList containing n elements, what is the complexity (Big O) of removefirst()? of removelast()? (4 points) i. We applied sorting methods primarily to arrays and Vectors. Of the following sort algorithms, which are most appropriate to sort a SinglyLinkedList: insertion sort, selection sort, quicksort, merge sort? (3 points) 3

2. (15 points) A circular doubly linked list with four elements is represented as in the picture below: head Suppose we have an implementation of such a list, class CircularDoublyLinkedList, which includes instance variables: protected DoublyLinkedListElement head; protected int count; Relevant parts of the class DoublyLinkedListElement from structure5 are on page 6. Consider the addlast() method of the CircularDoublyLinkedList class: // pre: value is not null // post: adds value to the tail of the list public void addlast(object value); a. What special cases must be considered when writing this method? (5 points) 4

b. Write Java code for this method. You need not replicate the pre- and post-conditions specified on the previous page. You may not use addfirst() in your code. (10 points) public void addlast(object value) { 5

public class DoublyLinkedListElement { protected Object data; protected DoublyLinkedListElement nextelement; protected DoublyLinkedListElement previouselement; public DoublyLinkedListElement(Object v, DoublyLinkedListElement next, DoublyLinkedListElement previous) { // post: constructs new element with list prefix referenced by // previous and suffix referenced by next data = v; nextelement = next; if (nextelement!= null) nextelement.previouselement = this; previouselement = previous; if (previouselement!= null) previouselement.nextelement = this; public DoublyLinkedListElement(Object v) { // post: constructs a single element this(v,null,null); public DoublyLinkedListElement next() { // post: returns the element that follows this return nextelement; public DoublyLinkedListElement previous() { // post: returns element that precedes this return previouselement; public Object value() { // post: returns value stored here return data; public void setnext(doublylinkedlistelement next) { // post: sets value associated with this element nextelement = next; public void setprevious(doublylinkedlistelement previous) { // post: establishes a new reference to a previous value previouselement = previous; 6

3. (15 points) Recall that the Queue interface may be implemented using an array to store the queue elements. Suppose that two int values are used to keep track of the ends of the queue. We treat the array as circular: adding or deleting an element may cause the head or tail to wrap around to the beginning of the array. You are to provide a Java implementation of class CircularQueueArray by filling in the bodies of the methods below. Note that there is no instance variable which stored the number of elements current in the queue; you must compute this from the values of head and tail. You may not add any additional instance variables. public class CircularQueueArray { // instance variables protected int head, tail; protected Object[] data; // constructor: build an empty queue of capacity n public CircularQueueArray(int n) { // pre: queue is not fill // post: adds value to the queue public void enqueue(object value) { 7

3. (continued) // pre: queue is not empty // post: removes value from the head of the queue public Object dequeue() { // post: return the number of elements in the queue public int size() { // post: returns true iff queue is empty public boolean isempty() { // post: returns true iff queue is full public boolean isfull() { 8

4. (15 points) The StackSort. Suppose you need to sort a stream of Comparable elements, and the only data structure available to you is an implementation of the Stack interface in the structure5 package (say, a StackList). The elements are available only through an Iterator, so you must process each item as it is returned by the next() method of the Iterator. The sort method should return a Stack containing the sorted elements, with the smallest at the top of the stack. Please fill in the body of the method. public static Stack StackSort(Iterator iter) { // pre: iter is an Iterator over a structure containing Comparables // post: a Stack is returned with the elements sorted, smallest on top 9

5. (16 points) Recall the definition of a min-heap, a binary tree in which each node is smaller than any of its descendants. For the rest of this question, we presume the Vector implementation of heaps (class VectorHeap). Consider the following tree, which is a minheap. 1 2 13 5 3 89 34 21 233 55 8 144 a. Show the order in which the elements would be stored in the Vector underlying our VectorHeap. (4 points) 0 1 2 3 4 5 6 7 8 9 10 11 b. Show the steps involved in adding the value 4 to the heap. Use drawings of the tree, not the vector. (4 points) 10

c. Using the original tree (not the one with the 4 added), show the steps involved in removing the minimum value of the heap. (4 points) d. Why is the VectorHeap implementation of a priority queue better than one that uses a linked list implementation of regular queues, modified to keep all items in order by priority? Hint: Your answer should compare the complexities of the add and remove operations. (4 points) 11

6. (18 points) Suppose we have a BinaryTree that contains only Comparable values. a. It is often useful to find the minimum and maximum values in the tree. Implement the method maximum as a member of class BinaryTree. Relevant sections of BinaryTree.java from the structure5 package are included on pages 14 16 to guide you. Your method should return the Comparable that is the maximum value in the tree. It should returnnull if called on an empty tree. (6 points) public Comparable maximum() { // pre: the values in this tree are all Comparable // post: the maximum value in the tree is returned b. What is the worst-case complexity of maximum on a tree containing n values? (2 points) c. What is the complexity of maximum on a full tree containing n values? (2 points) 12

d. Consider the following method, which I propose as a member of class BinaryTree: public boolean isbst() { // post: returns true iff the tree rooted here is a binary search tree if (this == EMPTY) return true; return left().isbst() && right().isbst(); This method will not always return the correct value. Explain why, then provide a correct method. You may useminimum() andmaximum() from part (a), as well as any other methods of BinaryTree. (6 points) public boolean isbst() { e. In class BinaryTree, why is the setleft() method public, but the setparent() method is protected? (2 points) 13

public class BinaryTree { protected Object val; // value associated with node protected BinaryTree parent; // parent of node protected BinaryTree left; // left child of node protected BinaryTree right; // right child of node // The unique empty node public static final BinaryTree EMPTY = new BinaryTree(); // A one-time constructor, for constructing empty trees. private BinaryTree() { val = null; parent = null; left = right = this; // Constructs a tree node with no children. Value of the node // is provided by the user public BinaryTree(Object value) { val = value; parent = null; left = right = EMPTY; // Constructs a tree node with no children. Value of the node // and subtrees are provided by the user public BinaryTree(Object value, BinaryTree left, BinaryTree right) { this(value); setleft(left); setright(right); // Get left subtree of current node public BinaryTree left() { return left; // Get right subtree of current node public BinaryTree right() { return right; // Get reference to parent of this node public BinaryTree parent() { return parent; // Update the left subtree of this node. Parent of the left subtree // is updated consistently. Existing subtree is detached public void setleft(binarytree newleft) { if (isempty()) return; if (left.parent() == this) left.setparent(null); 14

left = newleft; left.setparent(this); // Update the right subtree of this node. Parent of the right subtree // is updated consistently. Existing subtree is detached public void setright(binarytree newright) { if (isempty()) return; if (right.parent() == this) right.setparent(null); right = newright; right.setparent(this); // Update the parent of this node protected void setparent(binarytree newparent) { parent = newparent; // Returns the number of descendants of node public int size() { if (this == EMPTY) return 0; return left().size() + right.size() + 1; // Returns reference to root of tree containing n public BinaryTree root() { if (parent() == null) return this; else return parent().root(); // Returns height of node in tree. Height is maximum path // length to descendant public int height() { if (this == EMPTY) return -1; return 1 + Math.max(left.height(),right.height()); // Compute the depth of a node. The depth is the path length // from node to root public int depth() { if (parent() == null) return 0; return 1 + parent.depth(); // Returns true if tree is full. A tree is full if adding a node // to tree would necessarily increase its height public boolean isfull() { 15

if (this == EMPTY) return true; if (left().height()!= right().height()) return false; return left().isfull() && right().isfull(); // Returns true if tree is empty. public boolean isempty() { return this == EMPTY; // Return whether tree is complete. A complete tree has minimal height // and any holes in tree would appear in last level to right. public boolean iscomplete() { int leftheight, rightheight; boolean leftisfull, rightisfull, leftiscomplete, rightiscomplete; if (this == EMPTY) return true; leftheight = left().height(); rightheight = right().height(); leftisfull = left().isfull(); rightisfull = right().isfull(); leftiscomplete = left().iscomplete(); rightiscomplete = right().iscomplete(); // case 1: left is full, right is complete, heights same if (leftisfull && rightiscomplete && (leftheight == rightheight)) return true; // case 2: left is complete, right is full, heights differ if (leftiscomplete && rightisfull && (leftheight == (rightheight + 1))) return true; return false; // Return true iff the tree is height balanced. A tree is height // balanced iff at every node the difference in heights of subtrees is // no greater than one public boolean isbalanced() { if (this == EMPTY) return true; return (Math.abs(left().height()-right().height()) <= 1) && left().isbalanced() && right().isbalanced(); // Returns value associated with this node public Object value() { return val; 16