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

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

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

cameron grace derek cameron

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

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

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

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

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

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

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

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

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 100. Owen Astrachan. April 3, Name: Login: (1 pt)

2016 Spring COP 3502 Final Exam. Date: 4/28/2016. Name:

Test 2: CPS 100. Owen Astrachan. November 10, 2004

Q1 Q2 Q3 Q4 Q5 Q6 Total

(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

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

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

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

Test 2: Compsci 100. Owen Astrachan. November 14, 2006

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

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

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

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

Compsci 100. Owen Astrachan April 14, 2010

Computer Science Foundation Exam

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

Test 1: Compsci 100. Owen Astrachan. February 15, 2006

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

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

CS171 Final Practice Exam

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

Fall, 2015 Prof. Jungkeun Park

Computer Science Foundation Exam

CS171 Final Practice Exam

Final Exam. 1. (8 pts) Consider the following directed graph. If you iterate through all of the vertices as

Test 2: Compsci 100. Owen Astrachan. November 11, 2009

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

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

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

Test #1. Login: Initials: 2 PROBLEM 1 : (The Sorted Link (12 points)) Using the following definition of a linked list node, write a function that inse

Second Examination Solution

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

Data Structures and Algorithms Week 4

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

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Problem Set 6 Due: 11:59 Sunday, April 29

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Data Structures and Algorithms Chapter 4

CS 216 Exam 1 Fall SOLUTION

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

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

Mergesort again. 1. Split the list into two equal parts

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

Computer Science E-22 Practice Final Exam

CSCE 2014 Final Exam Spring Version A

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

8. Binary Search Tree

Quick Sort. CSE Data Structures May 15, 2002

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Exercise 1 : B-Trees [ =17pts]

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

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

Lecture Notes on Binary Search Trees

Balanced Search Trees

TREES. Trees - Introduction

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

Friday Four Square! 4:15PM, Outside Gates

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

No aids allowed. No books. No notes. No calculators. No computers. No communicating devices.

Data Structures and Algorithms. Roberto Sebastiani

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

Computer Science Foundation Exam

Expression Trees and the Heap

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

COP 3502 (Computer Science I) Final Exam 12/10/2015. Last Name:, First Name:

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?

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

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

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

ECE242 Data Structures and Algorithms Fall 2008

Test 2 Practice : Compsci 201

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

Data Structures and Algorithms for Engineers

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

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

INSTITUTE OF AERONAUTICAL ENGINEERING

Computer Science Foundation Exam

CS 223: Data Structures and Programming Techniques. Exam 2

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CS 3114 Data Structures and Algorithms READ THIS NOW!

Instructions PLEASE READ (notice bold and underlined phrases)

Lecture 15 Notes Binary Search Trees

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Transcription:

Test 2: CPS 100 Owen Astrachan November 19, 1997 Name: Honor code acknowledgment (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 TOTAL: value 9 pts. 12 pts. 10 pts. 15 pts. 10 pts. 9 pts. 65 pts. grade This test has 9 pages, be sure your test has them all. Do NOT spend too much time on one question remember that this class lasts 50 minutes. In writing code you do not need to worry about specifying the proper #include header les. Assume that all the header les we've discussed are included in any code you write. Declarations for Tree and List nodes, the function height for trees, and some recurrence relations, are found on the last page. 1

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 to sort the letters of a word in forming the canonical form of a word, e.g., in converting the word "smile" to "eilms". Briey, why was this a better choice than using Mergesort which is an O(n log n) sort whereas selection sort is O(n 2 ). 2. Briey, why does using smart string pointers (rather than strings) reduce the time to sort more for bubble sort and insertion sort than it does to reduce the time for selection sort? 3. Briey, why does a Quicksort of 10,000 ints, all in the range 0..99 take much longer than quicksort of 10,000 ints that range over all int values [as implemented in sortall.cc]. PROBLEM 2 : (Bonsai Problems ( 12 points)) Part A: 4 points Write the function twochildcount that returns the number of nodes in tree t that have exactly two children. int twochildcount(tree<string> * t) // post: returns number of nodes in t with exactly two children 2

Part B: 8 points Write the function makehtree whose header is given below. makehtree creates a new tree with the same shape as the tree t, but in which each node stores the height of the node. Recall that the height is the number of edges on the longest root-to-leaf path, that the height ofany node is one more than the maximum of the left/right subtree heights, and the height of a leaf node is zero. (the function height we discussed in class is on the last page of the test). For full credit your function should work in O(n) time for an n-node tree. For example, in the diagram below, the tree t on the left would generate the tree shown on the right asa result of makehtree(t): t makehtree(t) 11 4 4 21 3 1 2 9 37 0 2 0 5 1 7 0 Tree<int> * makehtree(tree<int> * t) // postcondition: returns a new tree with the same shape as t // in which each node contains a value that is the height // of the node 3

PROBLEM 3 : (Hash, hash, and more hash ( 10 points)) Part A: 8 points string is as shown. You are inserting the strings below into a hash table. Assume the hash value for each string hash value "pea" 12 "carrot" 25 "bean" 17 "broccoli" 37 "corn" 27 "kale" 8 "okra" 19 If these values are inserted in this order (i.e., "pea" rst and "okra" last) into a hash table with 11 entries draw the table assuming that: hashing is resolved by chaining 0 1 2 3 4 5 6 7 8 9 10 hashing is resolved by linear probing 0 1 2 3 4 5 6 7 8 9 10 Part B: 2 points Once the entries are in the hash table, describe a method for printing the entries in alphabetical order in O(n log n) time, where n is the number of elements in the hash table. Be brief, a few words suce. 4

PROBLEM 4 : (Sometimes a branch is just a branch ( 15 points)) For each of the questions below, give a big-oh expression that best describes the quantity described, and draw a picture of a 7 node tree that \justies" your expression. For example: in a binary tree of n nodes, what are the smallest and largest values for the height? Answer: smallest height is O(log n), largest height is O(n). Pictures showing this are on the left (I) and the right (II). If you want to use one of these pictures, refer to them as I and II. I II 1. In a binary tree of n nodes what are the minimal and maximal number of leaves (give big-oh expressions)? 2. In a binary search tree of n nodes what are the minimal, maximal, and average case numberofnodes with values less than the root value (give big-oh expressions)? 3. What are the minimal and maximal height of a binary search tree of n nodes in which the number of values less than the root is equal to the number of values greater than the root (give big-oh expressions)? 5

PROBLEM 5 : (Trees to starboard (10 points)) Consider the problem of inserting the values in a sorted linked-list into a binary search tree. Two methods are suggested: list2tree which uses the standard insert function for search trees, and a recursive function reclist2tree. You don't need to look at these functions in detail, they both succeed in constructing a binary search tree, they both have a complexity that's bigger than O(n) for an n-node list. Part A: 5 points What is the big-oh complexity of list2tree? Briey justify your answer. Draw the tree that results from using list2tree when the list below is converted into a search tree. 6 11 15 22 31 37 48 void insert(tree<int> * & t, int key) // pre: t is a search tree // post: key is inserted into t, t is a search tree if (t == 0) t = new Tree<int>(key); else if (key < t->info) insert(t->left,key); else insert(t->right,key); Tree<int> * list2tree(node<int> * list) // pre: list is sorted // post: returns a search tree whose elements are those in list Tree<int> * root = 0; while (list!= 0) insert(root,list->info); list = list->next; return root; 6

Part B: 5 points What is the big-oh complexity of reclist2tree? Briey justify your answer. Draw the tree that results from using reclist2tree when the list above is converted into a search tree. Tree<int> * reclist2tree(node<int> * list) // pre: list is sorted // post: returns a search tree whose elements are those in list Node<int> * middle = 0; Node<int> * temp = list; // treat 0 and 1 node lists separately if (list == 0) return 0; if (list->next == 0) return new Tree<int>(list->info); while (temp!= 0) temp = temp->next; if (temp!= 0) if (middle == 0) middle = list; else middle = middle->next; temp = temp->next; temp = middle->next; // unlink middle node from front sublist middle->next = 0; Tree<int> * t = new Tree<int>(temp->info, reclist2tree(list), reclist2tree(temp->next)); middle->next = temp; return t; // link middle node back to front sublist 7

PROBLEM 6 : (Home on the Range ( 9 points)) Write the function range whose header is given below. The tree t is a search tree, and range stores all values from t between (and including) rst and last in the vector words. The values stored in words should be stored in alphabetical order. For full credit your code should visit the minimal number nodes in t. For example, in the tree t diagrammed below, the call range(t,"kangaroo","pancake",words); should store the values "kismet", "miracle", "nasty", "oily" in words, and visit only the shaded nodes shown in the diagram. "miracle" "jump" "tangy" "awful" "kismet" "nasty" "youth" "great" "pretty" "zest" "crude" "jovial" "oily" "snarl" void range(tree<string> * t, const string & first, const string & last, Vector<string> & words) // postcondition: all strings s in t, such that first <= s <= last, // are stored in words,in alphabetical order 8

template <class Type> struct Tree Type info; Tree * left; Tree * right; Tree(const Type & s, Tree * lchild = 0, Tree * rchild = 0) : info(s), left(lchild), right(rchild) ; template <class Type> struct Node Type info; Node * next; Node(const Type & s, Node * link = 0) : info(s), next(link) ; int max(int x, int y) if (x > y) return x; else return y; template <class Type> int height(tree<type> * t) if (t == 0) return -1; else return 1 + max(height(t->left),height(t->right)); recurrence solution T (n) =T( n )+O(1) O(log n) 2 T (n) =2T( n 2 )+O(1) O(n) T (n) =2T( n )+O(n) O(nlog n) 2 T (n) =T(n,1) + O(n) O(n 2 ) 9