Test 2: CPS 103 Owen Astrachan November 19, 1993 Name: Honor code acknowledgement (signature) Problem 1 value 9 pts. grade Problem 2 12 pts. Problem 3

Similar documents
PROBLEM 1 : (Short Answer: 13 points) Three points each, except for last two which are two points 1. Both bubble-sort and selection sort are O(n 2 ) s

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

"apple" "grape" "grape" "grape" "apple"

"sort A" "sort B" "sort C" time (seconds) # of elements

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

PROBLEM 1 : (ghiiknnt abotu orsst (9 points)) 1. In the Anagram program at the beginning of the semester it was suggested that you use selection sort

cameron grace derek cameron

PROBLEM 1 : (And the winner is...(12 points)) Assume you are considering the implementation of a priority queue that will always give you the smallest

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

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?

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

Test 2: CPS Owen Astrachan. November 17, Name: Honor code acknowledgement (signature)

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

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

Test 1: CPS 100. Owen Astrachan. October 11, 2000

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

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Trees. (Trees) Data Structures and Programming Spring / 28

Test 1: CPS 100. Owen Astrachan. February 23, 2000

Section 1: True / False (1 point each, 15 pts total)

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Q1 Q2 Q3 Q4 Q5 Q6 Total

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Trees, Binary Search Trees

Trees. A tree is a directed graph with the property

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Computer Science E-22 Practice Final Exam

Section I B COMPUTER SCIENCE SOLUTION

Test 2: CPS 100. Owen Astrachan. April 3, Name: Login: (1 pt)

Binary Trees

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

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

CS301 - Data Structures Glossary By

Data Structures and Algorithms

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Midterm solutions. n f 3 (n) = 3

Algorithms and Data Structures

Final Exam. Name: Student ID: Section: Signature:

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

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

Priority Queues. T. M. Murali. January 29, 2009

CP2 Revision. theme: dynamic datatypes & data structures

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Advanced Set Representation Methods

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

CMSC 341 Lecture 15 Leftist Heaps

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

Priority queues. Priority queues. Priority queue operations

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

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

CSCI2100B Data Structures Trees

Chapter 20: Binary Trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

CMSC 341 Lecture 15 Leftist Heaps

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Before calling Prepend(list,13) After calling Prepend(list,13) After calling RemoveLast(list)

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

class for simulating a die (object "rolled" to generate a random number) Dice(int sides) -- constructor, sides specifies number of "sides" for the die

Data Structures Question Bank Multiple Choice

CSI33 Data Structures

CMSC 341 Leftist Heaps

Part I: Short Answer (12 questions, 65 points total)

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Test 2: CPS 100. Owen Astrachan. November 15, 2000

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

UNIT III BALANCED SEARCH TREES AND INDEXING

TREES. Trees - Introduction

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Overview of Sorting Algorithms

Course Review. Cpt S 223 Fall 2009

COMP : Trees. COMP20012 Trees 219

Computer Science Foundation Exam

CSCI2100B Data Structures Heaps

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Data Structures and Algorithms for Engineers

Programming II (CS300)

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

DATA STRUCTURES AND ALGORITHMS

Suppose that the following is from a correct C++ program:

CSE373 Fall 2013, Midterm Examination October 18, 2013

Data Structures and Algorithms for Engineers

Programming II (CS300)

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Second Examination Solution

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

PROBLEM 1 : (I'll huff and I'll puff and I'll... (10 pts)) The Huffman tree below is used for this problem. e s r o t Part A(4 points) Using this tree

Summer Final Exam Review Session August 5, 2009

Test 2: Compsci 100. Owen Astrachan. November 12, 2008

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Priority Queues Heaps Heapsort

Analysis of Algorithms

CS171 Final Practice Exam

Transcription:

Test 2: CPS 103 Owen Astrachan November 19, 1993 Name: Honor code acknowledgement (signature) Problem 1 value 9 pts. grade Problem 2 12 pts. Problem 3 6 pts. Problem 4 12 pts. Problem 5 12 pts. Problem 6 9 pts. Problem 7 5 pts. Problem 8 extra TOTAL: 6 pts. 65 pts. 1

PROBLEM 1 : (Vocabulary: 9 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context of computer science, programming, and C/C++. 1. templates (a) The method employed in C/C++ that prevents recursion from using all available memory (b) The method employed in C++ to parameterize classes (c) A software-engineering practice in which programs are designed using a bottom-up, scaold-building approach 2. hashing (a) A method of maintaining a table that supports O(1) search operations in the average case. (b) A method of generating random numbers based on maintaining a seed and performing a series of exclusive-or operations on the seed. (c) A method of turning left-over food into inedible glop. 3. bubble sort (a) An O(n 2 ) worst-case sort that, on large arrays of C++ Strings, performs better than selection sort (b) An O(n 2 )worst-case sort that can be coded so that already sorted arrays can be recognized in O(log n) time (c) A sort whose usefulness is comparable to that of a ea-infested dog in a pet-grooming business PROBLEM 2 : (Complexity (12 points)) Part I: An AVL tree is used to store n strings so that an inorder traversal of the tree would print the strings in alphabetical order. For each of the operations listed below, give a big-o expression for the complexity of the operation and a brief explanation justifying the expression. A. nd the alphabetically rst string in the tree B. nd the average string length of strings in the tree 2

C. delete a string from the tree D. nd the smallest (# of characters) string in the tree Part II: Which of the operations AD above has a dierent worst-case complexity if a \normal" search tree is used rather than an AVL tree. Briey justify your answers. PROBLEM 3 : (Trees and functions (6 points)) Consider the following functions on binary trees of integers: int Sum(Tree * t) if (t == NULL) return 0; else return t->info + Sum(t->left) + Sum(t->right); int Count(Tree * t) if (t == NULL) return 0; else return 1 + Count(t->left) + Count(t->right); For a tree t, what is the value of the expression double(sum(t))/count(t) and what is the complexity of computing this value (using big-o notation) for a tree of n nodes? Briey justify your answer. 3

PROBLEM 4 : (Meta Functions) Consider the following functions: int TreeStat(int (*Func) (Tree *), Tree * t) if (t == NULL) return 0; else return (*Func)(t) + TreeStat(t->left) + TreeStat(t->right); int One(Tree * t) return 1; int Value(Tree * t) return t->info; The expression double(treestat(value,t))/treestat(one,t) has the same value as the expression double(sum(t))/count(t) of the previous problem becaue TreeStat(Value,t) == Sum(t) and TreeStat(One,t) == Count(t). Write the function LeafIt whose header is given below so that the expression TreeStat(LeafIt,t) evaluates to the number of leaves in the binary tree t. int LeafIt(Tree * t) 4

PROBLEM 5 : (Priority Queues (12 points)) If a sorted linked list is used to implement a priority queue, what is the complexity ofthedeletemin operation and the Insert operation on a queue of n elements. Briey justify your answer. If a hash table is used to implement a priority queue, what is the complexity of the DeleteMin operation and the Insert operation on a queue of n elements. Briey justify your answer. If a heap is used to implement a priority queue, what is the complexity of the DeleteMin operation and the Insert operation on a queue of n elemenst. Briey justify your answer. PROBLEM 6 : (General Trees (9 points)) A tree in which each node can have an arbitray number of children, and in which each node stores an integer, can be implemented using the following declarations: struct Tree int info; Tree * firstchild; Tree * sibling; // first child of node // sibling of node Write the routine TreeProduct whose header is given below. TreeProduct returns the product of all the values stored in the nodes of t. For example, it would return 12! = 479; 001; 600 for the tree diagrammed below. In the tree below, the solid arrows! represent the connections using the declaration for Tree given above, the dotted arrows represent the \conceptual" children for each node of the tree. 5

By denition, the value returned for an empty tree is zero. 2 3 4 5 6 7 8 9 10 11 12 int TreeProduct(Tree * t) // postcondition: returns product of all nodes in t // returns 0 if t is empty 6

PROBLEM 7 : (Profound Leaves (5 points)) Part I The code for function DeepLeaf given below returns a pointer to a leaf in the tree that is farthest from the root, i.e., the deepest leaf in the tree. Tree * DeepLeaf(Tree * t) if (t == NULL) return NULL; else if (IsLeaf(t)) return t; else if (height(t->left) >= height(t->right)) return DeepLeaf(t->left); else return DeepLeaf(t->right); For a tree of n nodes, the average-case complexity of the function DeepLeaf shown above is NOT O(n), what is it? (Briey justify your answer, you may assume that, on the average, trees are \balanced".) Part II It is possible to write DeepLeaf so that it works in time O(n) for a tree of n nodes. This is shown below. Fill in the recursive calls of DeepWork so that DeepLeaf works correctly in O(n) time. Tree * DeepLeaf(Tree * t) Tree * deep = NULL; int max = 0; DoDeep(t,0,max,deep); return deep; void DoDeep(Tree * t,int depth, int & maxdepth, Tree * & deeptree) // precondition: // postcondition: if (t == NULL) if (IsLeaf(t)) if (depth > maxdepth) maxdepth = depth; deeptree = t; else DeepWork(,maxDepth,deepTree); DeepWork(,maxDepth,deepTree); 7

8

PROBLEM 8 : (Lazy Dog Sorts (6 points) EXTRA CREDIT) Consider 1 sorting a linked list using quicksort. Assume the following declarations have been made: struct Dlist double info; Dlist * next; ; void Pivot(Dlist * & first, Dlist * & second) // precondition: first is non-empty list of n nodes // postcondition: second is non-empty list // all nodes in first have info values strictly less than // second->info. Also, second->info is the smallest value // of all nodes in second // // all nodes originally in first are in either first // or second The idea is to use the following code to implement quicksort for linked lists, it is complete except for after the comment: \connect things back so that list is sorted". You are to complete the code. void Quicksort(Dlist * & list) if (list!= NULL) //partition list into two parts Dlist * hold; Pivot(list,hold); // note: all nodes in list are less than hold->info // and hold->info is smallest value in hold // (see spec. for Pivot) Dlist * anchor = hold; hold = hold->next; anchor->next = NULL; Quicksort(list); Quicksort(hold); // connect things back so that list is sorted> 1 Why is this question called \lazy dog sorts" it has nothing to do with bubble sort and dogs. 9