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

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

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

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

cameron grace derek cameron

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

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

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

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: Compsci 100. Owen Astrachan. November 12, 2008

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

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

Compsci 100. Owen Astrachan April 14, 2010

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

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

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

Chapter 20: Binary Trees

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

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

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

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

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

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

CS 223: Data Structures and Programming Techniques. Exam 2

Section 1: True / False (2 points each, 30 pts total)

Test 2 Practice : Compsci 201

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

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

Largest Online Community of VU Students

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

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

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

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

Traversing Trees with Iterators

Traversing Trees with Iterators

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

CMSC 341 Lecture 10 Binary Search Trees

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

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

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

// a stack is printed from bottom (leftmost) to top (rightmost) System.out.println(stk);

EECS 311 Data Structures Midterm Exam Don t Panic!

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Test 1: CPS 100. Owen Astrachan. October 1, 2004

A set of nodes (or vertices) with a single starting point

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

(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

CSCE 2014 Final Exam Spring Version A

PROBLEM 1 : (Trouble?: (14 pts)) Part A: (6 points) Consider the function Trouble below. int Trouble (const Vector<int> & numbers, int size) if (size

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

APCS :: Winter Quarter Exam Review Packet

CS350: Data Structures Tree Traversal

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CS 315 Data Structures mid-term 2

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

Programming II (CS300)

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Algorithm and Data Structures, Supplemental Solution 2010

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

the pointer range [first, last) into the tree

Lecture 15 Notes Binary Search Trees

Computer Science Foundation Exam

2. (5 Points) When calculating a combination (n choose k) the recursive definition is n k =1 if

COS 226 Algorithms and Data Structures Fall Midterm

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Lecture 15 Binary Search Trees

CMSC 341 Leftist Heaps

CS102 Binary Search Trees

Solution to CSE 250 Final Exam

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

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

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

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;

Lecture Notes on Binary Search Trees

Binary Trees and Binary Search Trees

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

Second Examination Solution

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

CS 216 Exam 1 Fall SOLUTION

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

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

Test 1: CPS 08 Owen Astrachan February 17, 1995 Name: Honor code acknowledgement (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Extra T

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

Expression Trees and the Heap

Topic Binary Trees (Non-Linear Data Structures)

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

INF2220: algorithms and data structures Series 1

Sample Exam 1 Questions

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Chapter 5. Binary Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Binary Search Trees Part Two

Section 05: Solutions

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Q1 Q2 Q3 Q4 Q5 Q6 Total

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

TREES. Trees - Introduction

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Fall, 2015 Prof. Jungkeun Park

Transcription:

Test 1: CPS 100 Owen Astrachan February 23, 2000 Name: Login: Honor code acknowledgment (signature) Problem 1 value 19 pts. grade Problem 2 24 pts. Problem 3 8 pts. Problem 4 9 pts. Problem 5 6 pts. TOTAL: 66 pts. 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 files. Assume that all the header files we ve discussed are included in any code you write. Class/struct declarations for nodes for trees and lists are on the last page of the test. You can remove this page to make it easier to have a reference to these classes when needed. 1

PROBLEM 1 : (Check it twice) Part A, 4 points Write the function nodecount that returns the number of nodes in its parameter list. int nodecount(node * list) // pre: list is NULL/0 terminated // post: returns number of nodes in list Part B, 6 points Write the function freelist that returns all nodes in a list to the heap (by calling delete). void freelist(node * & list) // pre: list is NULL/0 terminated // post: returns all nodes of list to heap (using delete) // list is NULL/0 2

Part C, 9 points The function vec2list below constructs a linked-list storing the same values that are in a vector. Currently, its postcondition isn t satisfied because the elements of the linked list are in the wrong order, they are backwards. Node * vec2list(const tvector<string>& a) // post: returns linked list with same values as a in same order Node * front = 0; int k; for(k=0; k < a.size(); k++) front = new Node(a[k],front); return front; // return pointer to front of list As written, the function has complexity O(n) for a list of n nodes. Three methods are proposed for fixing the function so that its postcondition is satisified. Next to each method below indicate whether it works correctly. You must draw a picture of the list returned if the vector contains the values (1, 2, 3, 4) in that order. Draw a picture for each method. I. Before returning front, add this code tstack<node *> s; Node * temp = 0; while (front!= 0) s.push(front); front = front->next; while (! s.isempty()) temp = s.top(); s.pop(); temp->next = front; front = temp; II. Before returning front, add this code tqueue<node *> q; Node * temp = 0; while (front!= 0) q.enqueue(front); front = front->next; while (! q.isempty()) temp = q.front(); q.dequeue(); temp->next = front; front = temp; III. Run the for loop from a.size()-1 down to zero rather than as written: for(k=a.size()-1; k >= 0; k--) front = new Node(a[k],front); 3

PROBLEM 2 : (Bean Trees) The tree shown below is a binary search tree. "lima" "chickpea" "navy" "black" "garbanzo" "pinto" Part A, 10 points 1. What nodes of the tree above are leaves? 2. What is the postorder traversal of the tree above 3. Add the following strings, in order, to the tree. Draw new nodes attached to the tree above. Add butter, kidney, soy, and red in that order. 4. What are the minimum and maximum heights of a search tree storing 2048 = 2 11 different strings? Part B, 8 points Write the function swapcopy that creates a new tree that s a swapped copy of its parameter and returns a pointer to the root of the new tree. In a swapped copy, nodes with two-children are copied by interchanging the copies of the left and right subtrees. Nodes with no children or one child are simply copied (no swapping). For the tree above, the swapped copy is shown below. "lima" "navy" "chickpea" "pinto" "garbanzo" "black" (continued ) 4

Tree * swapcopy(tree * t) // post: returns a pointer to the root of a tree that s a swapped copy of t Part C, 6 points For this problem, the root is at level zero, the root s children are at level one, and for any node, the node s level is one more than its parent s level. Write levelcount that returns the number of nodes on the specified level. For example, for the bean-tree diagrammed on the previous page, the call levelcount(t,1) should evaluate to 2 (chickpea and navy are on level 1);the call levelcount(t,2) should evaluate to 3; and the call levelcount(t,4) should evaluate to 0. int levelcount(tree * t, int level) // pre: 0 <= level // post: returns # nodes at specified level in t 5

PROBLEM 3 : (A Recurring Nightmare) Part A, 4 points Write a recurrence relation that describes the running time of the function value below in the average case that trees are approximately balanced. For extra credit (3 points) solve the recurrence. int numnodes(tree * t) // post: return number of nodes in t if (t == 0) return 0; return 1 + numnodes(t->left) + numnodes(t->right); int value(tree * t) if (t == 0) return 0; int lcount = numnodes(t->left); int rcount = numnodes(t->right); if (lcount > rcount) return 1 + value(t->left); else return 1 + value(t->right); Part B, 4 points What is the complexity (using big-oh) of the code below in terms of n. Justify your answer. int sum=0; for(k=0; k < n; k += 2) for(j=1; j <= k; j *= 3) sum += j*k; 6

PROBLEM 4 : (Big Oaks Grow) A tree S is a subset of another tree T if every value of S is contained in T. There may be values in T that are not in S, but every value stored in S must also stored in T for S to be a subset of T. The empty tree is a subset of every tree (there are no values in the empty tree not in another tree). For search trees, the function below correctly determines if a single value is contained in a tree. bool contains(tree * t, const string& s) // pre: t is a search tree // post: returns true if t contains s, else return false if (t == 0) return false; else if (t->info == s) return true; else if (t->info < s) return contains(t->right,s); else return contains(t->left,s); Part A, 6 points Write the function subset that returns true if search tree s is a subset of search tree t, and returns false otherwise. Hint: traverse s in any order calling contains for each value in s. bool subset(tree * s, Tree * t) // pre: s and t are both search rees // post: returns true if s is a subset of t, false otherwise Part B, 3 points What is the complexity of the code you wrote in Part A. In answering the question assume s contains n nodes and t contains m nodes, and that trees are roughly balanced (average case). The answer should be in terms of both n and m. Justify your answer briefly. 7

PROBLEM 5 : (MultiSet (6 points)) Multiset A is a subset of multiset B if every element of A (occurring k times in A) occurs at least k times in B. For example ("ant", "bear", "cat", "ant") is a subset of ("ant", "bear", "cat", "ant", "ant", "bear", "dog") but it s not a subset of ("ant", "bear", "cat", "cat", "cat", "cat") because ant occurs only once in the second set. A class MSSubset (next page) can be used to determine if a MultiSet a is a subset of another MultiSet b as follows. MultiSet a,b; // fill a and b with values MSSubset sub(a); b.apply(sub); if (sub.issubset()) cout << "a is a subset of b" << endl; You must write the body of MSSubset::apply so that the class works as intended. The header file and member functions are on the last page. void MSSubset::apply(const string & word, int count) // sets mystatusok to false when appropriate 8

struct Node string info; Node * next; Node (const string& s, Node * link) : info(s), next(link) ; struct Tree string info; Tree * left; Tree * right; Tree(const string& s, Tree * lptr, Tree * rptr) : info(s), left(lptr), right(rptr) ; #include "msapplicant.h" #include "multiset.h" class MSSubset : public MSApplicant public: MSSubset(const MultiSet& set); bool issubset() const; virtual void apply(const string & word, int count); private: const MultiSet& myset; bool mystatusok; ; MSSubset::MSSusbset() : myset(set), mystatusok(true) bool MSSubset::isSubset() const // post: returns false if subset relationship doesn t hold // else returns true return mystatusok; 9