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

Similar documents
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

cameron grace derek cameron

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

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

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

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

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

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

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

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

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

mypoly

Test 2: CPS 100. Owen Astrachan. November 15, 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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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)

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

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

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

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

Object Oriented Programming COP3330 / CGS5409

Cpt S 122 Data Structures. Data Structures

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

CS 216 Exam 1 Fall SOLUTION

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

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

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

CMSC 341 Lecture 10 Binary Search Trees

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

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

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

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

Largest Online Community of VU Students

Computer Science E-22 Practice Final Exam

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

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

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

CSCE 2014 Final Exam Spring Version A

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

CMPT 125: Practice Midterm Answer Key

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

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

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

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

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Ashish Gupta, Data JUET, Guna

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

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

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

3137 Data Structures and Algorithms in C++

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

Dynamic Data Structures

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

CSE 143 SAMPLE MIDTERM

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

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

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

PRACTICE MIDTERM EXAM #2

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Summer Final Exam Review Session August 5, 2009

Please submit your request directly to Noha Sinno

Solution for Data Structure

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

The time and space are the two measure for efficiency of an algorithm.

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Priority Queues (Heaps)

Lecture 10 Notes Linked Lists

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?

An Introduction to Trees

15-122: Principles of Imperative Computation, Fall 2015

Computer Science Foundation Exam

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

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

Computer Science 62. Midterm Examination

CS171 Midterm Exam. October 29, Name:

Lecture 10 Notes Linked Lists

COS 226 Algorithms and Data Structures Fall Midterm

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

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

Tutorial Letter 102/1/2012 Programming: Data Structures

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

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

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

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Dynamic Data Structures. John Ross Wallrabenstein

CS 241 Data Organization Binary Trees

Computer Science Foundation Exam

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

Largest Online Community of VU Students

Transcription:

Test 1: CPS 100 Owen Astrachan and Dee Ramm February 21, 1997 Name: Honor code acknowledgment (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 TOTAL: value 10 pts. 9 pts. 21 pts. 10 pts. 12 pts. 12 pts. 74 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. Class declarations for stacks and queues are provided 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 : (Vocabulary: 10 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. Stack (a) A rst-in, rst-out data structure (b) A last-in rst-out data structure (c) A low-level C++ construct used in implementing vectors 2. big-oh (a) A heuristic for developing large-scale, object-oriented programs eciently and on time (b) A notation that expresses an asymptotic upper bound on resource requirements such as time or space (c) A nomenclature used solely to dierentiate sorting algorithms 3. base case (a) A construct used for transporting a large musical instrument that is a member of the string family (b) The part of a recursive function that is computable without making any recursive calls (the stopping condition) (c) The zero-th element of a vector, used to anchor the vector in memory 4. segmentation violation 5. heap (a) An attempt by a non-member function to access private data which is not permitted in C++ (b) An error that can result from dereferencing a NULL or garbage pointer value (c) A design problem that result from including data in the public section rather than restricting data to be private (a) Fragmented memory resulting from too many calls of new. (b) Where memory is allocated from statically, at compile time. (c) Where memory is allocated from dynamically, at run time PROBLEM 2 : (In a x 9 points) Evaluate each of the following postx expressions 3 5 7 + * 4 6 * - 12 8 + 5 * 9 14 17 + + * 2

Find the exact value of the postx expression diagrammed below 1 2 3 n ++++ z (n-1) times PROBLEM 3 : (Frequent Occurrences 21 points) Linked lists of strings, with a header node, are implemented using the declaration for Node below. struct Node string info; Node * next; Node (const string & s, Node * ptr = 0) : info(s), next(ptr) ; Part A: 6points Write the function Count whose header is given below. Count returns the number of occurrences of key in list. For example, Count(list,"apple") should evaluate to 2 and Count(list,"cherry") should evaluate to 0 where list is shown below. "apple" "grape" "grape" "grape" "apple" int Count(Node * list, const string & key) // precondition: list has a header node // postcondition: returns # occurrences of key in list 3

Part B: 12 points Write the function Modal whose header is shown below. Modal returns the string that occurs most often (or at least as many times as any other string) in list. If there is a tie, any string that is a modal string can be returned. Return "" for an empty list. In writing Modal, you can call the function Count from Part A; assume that the function Count works as intended. string Modal(Node * list) // precondition: list has header node // postcondition: returns the modal string in list Part C: 3points For a list of N nodes, what is the complexity ofthe solution you wrote in part B using big-oh notation. Justify your answer briey. 4

PROBLEM 4 : (Spoiled Fruit 10 points) Part A: 6points Complete the function Compress below that removes duplicate nodes from a sorted linked list. In the diagram below, a list is shown before and after duplicates are removed. "apple" "apple" "grape" "grape" "grape" "apple" "grape" void Compress(Node * list) // precondition: no header node, list is sorted // postcondition: all duplicates removed while (list && list->next) // at least two nodes? if (list->info == list->next->info) // remove list->next else list = list->next; // not a dupe, goto next node Part B: (4 points) What is the complexity, using big-oh, of the function Compress; justify your answer briey. How would the complexity change if the list to be compressed (so that it contains no duplicates) is NOT sorted? 5

PROBLEM 5 : (SQL: 12 points) For this problem you can use stacks, queues (and, of course, vectors and linked lists, etc.). Declarations for the stack and queue class are at the end of the test. Postscript is a stack based language, one of the operations in postscript is ReverseN which reverses the top N elements of a stack. For example, if the top of a stack is to the left, then ReverseN(s,4) for the stack s = (1, 2, 3, 4, 5, 6, 7, 8, 9) changes the stack to s = (4, 3, 2, 1, 5, 6, 7, 8, 9). Write the function ReverseN whose header is shown below. Assume the precondition holds, don't worry about stacks that contain fewer than N elements. void Reverse(Stack<int> & s, int n) // precondition: n <= s.size(), s contains at least n elements // postcondition: top n elements of s are reversed 6

PROBLEM 6 : (Building Trees 12 points) Write a function MakeTree that converts a sorted, doubly-linked list (NO header node) into a binary search tree. The middle node of the linked list should be the root of the search tree. Assume the existence of a function MiddleNode, that returns a pointer to the middle node of a doubly-linked list, you do NOT need to write MiddleNode. DNode * MiddleNode(DNode * list) // precondition: list is NULL-terminated, NO header node // postcondition: returns pointer to middle node of list, // returns 0 if list is empty Declarations for doubly-linked lists and binary trees are given below, DNode for doubly-linked lists, TNode for binary trees. // for doubly-linked lists // for binary (search trees) struct DNode struct TNode string info; string info; DNode * prev; TNode * left; DNode * next; TNode * right; DNode(const string & s, TNode(const string & s, DNode * prior = 0, TNode * lchild = 0, DNode * after = 0) TNode * rchild = 0) : info(s), : info(s), prev(prior), left(lchild), next(after) right(rchild) ; ; You must use the following algorithm to build the search tree: 1. Find the middle node, build a root node (TNode) with an info eld the same as the middle node 2. Unlink the middle node from the list (so the next eld of the node before it points to NULL, and the prev eld of the node after it points to NULL) this step is necessary for the next step (so that MiddleNode will work) you may want to store pointers to the nodes before and after the middle node. 3. Make left and right subtrees recursively, using the list before the middle node for the left subtree and the list after the middle node for the right subtree. 4. Link the middle node back into the list (which iswhyyou may have stored pointers in step 2). (continued) 7

TNode * MakeTree(DNode * list) // precondition: list is NULL-terminated, NO header node // postcondition: returns a pointer to the root of a binary search // tree containing same values in list 8

template <class Type> class Queue public: Queue(); // construct empty queue void Enqueue(const Type & elt); void Dequeue(); void Dequeue(Type & elt); void MakeEmpty(); // insert elt (at rear) // remove first element // remove first element // clear queue to 0 elements const Type & GetFront() const; // return front (still there) bool IsEmpty( ) const; // true if empty else false int Size() const; // # of elements in queue private: ; // not shown template <class Type> class Stack public: Stack(); // construct empty stack void Push( const Type & elt); void Pop(); void Pop(Type & elt); void MakeEmpty(); // push elt onto top of stack // pop top element // pop top element and return // empty stack (no elements) const Type & Top() const; // return top element (NO pop) bool IsEmpty() const; // return true if empty, else false int Size() const; // returns # of elements in stack private: ; // not shown 9