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

Similar documents
Final Exam Solutions PIC 10B, Spring 2016

Tree Travsersals and BST Iterators

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

Chapter 20: Binary Trees

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

Solution to CSE 250 Final Exam

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Maps; Binary Search Trees

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

CSCE 2014 Final Exam Spring Version A

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

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Summer Final Exam Review Session August 5, 2009

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

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Friday Four Square! 4:15PM, Outside Gates

CS 216 Exam 1 Fall SOLUTION

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

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

(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

CSCI-1200 Data Structures Test 3 Practice Problem Solutions

Binary Search Trees. EECS 214, Fall 2018

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

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

20. Inheritance and Polymorphism

Binary Trees, Binary Search Trees

Binary Search Tree (2A) Young Won Lim 5/17/18

CS 171: Introduction to Computer Science II. Binary Search Trees

Data Structures Question Bank Multiple Choice

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

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

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Introduction to Computer Science Midterm 3 Fall, Points

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

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

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?

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

CMSC 341 Lecture 10 Binary Search Trees

Largest Online Community of VU Students

CS350: Data Structures Heaps and Priority Queues

Lecture 2. Binary Trees & Implementations. Yusuf Pisan

TREES. Trees - Introduction

Compsci 100. Owen Astrachan April 14, 2010

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Q1 Q2 Q3 Q4 Q5 Q6 Total

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

STUDENT LESSON AB30 Binary Search Trees

Computer Science Foundation Exam

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Data Structures Lab II. Binary Search Tree implementation

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

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

Binary Search Trees Part Two

Code No: R Set No. 1

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

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

Computer Science II Fall 2009

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

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

CS 106X Sample Final Exam #2

Final Examination CSE 100 UCSD (Practice)

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

An Introduction to Trees

CS350: Data Structures Binary Search Trees

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Data Structures in Java

Second Examination Solution

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

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

Binary Trees. Examples:

CSCE 206: Structured Programming in C++

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

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Polymorphism Part 1 1

Computer Science Foundation Exam

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

Computer Science Foundation Exam

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Data Structures and Algorithms

Module 8: Binary trees

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

Programming II (CS300)

Computer Programming II Python

8. Binary Search Tree

CS 350 : Data Structures Binary Search Trees

CSCE Practice Midterm. Data Types

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

Midterm Exam 5 April 20, 2015

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

Computer Science E-22 Practice Final Exam

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

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

04-24/26 Discussion Notes

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

(the bubble footer is automatically inserted into this space)

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

Exercise 1.1 Hello world

Data Structures and Algorithms

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

Transcription:

Final Exam PIC 10B, Spring 2016 Name: Student ID: Section: Discussion 3A (2:00 2:50 with Kelly) Discussion 3B (3:00 3:50 with Andre) I attest that the work presented in this exam is my own. I have not used any outside resources that might afford me an unfair advantage in taking this exam. Signature:

Directions: You have 180 minutes to answer the following ten (10) questions. In the code provided, you may assume that all necessary header files are included (e.g., #include <iostream>, #include <string>, etc.). Each question is worth 10 points. Problem Score 1 /10 2 /10 3 /10 4 /10 5 /10 6 /10 7 /10 8 /10 9 /10 10 /10 Total /100

Problem 1. (10 pts) Consider the Fraction class, whose partial declaration was given by 1 class Fraction { 2 public : 3 Fraction ( int num, int den ); 4... 5 int num ; // numerator of the Fraction 6 int den ; // denominator of the Fraction 7 } Use operator overloading to declare and define the operator < so that the following code will compile and run: 1 Fraction a, b; 2... 3 if(a < b) { 4 std :: cout << " a is smaller " << std :: endl ; 5 }

Problem 2. (10 pts) You are writing software for a car dealership and you need to represent the various parts of the business in your code. In particular, the car dealership deals with the following objects, each of which will be represented by a class: 1 Vehicle, VehiclePart, SportsCar, RacingTire, Truck, Car, Tire, SteeringWheel (a) Draw an inheritance diagram that shows the relationships between the classes listed above. (4 pts)

(b) The class Vehicle has a member function void test drive(), which allows potential buyers to take the vehicle for a test drive. For a SportsCar, the buyer can reserve time on a test track. Use polymorphism to declare the the function test drive() in the Car class so that the code 1 Car * ferrari = new SportsCar (); 2 ferrari -> test_drive (); calls the SportsCar version of test drive() rather than the Car version. (2 pts) Declare the class SportsCar as derived class from Car, and override the test drive() function. The overridden test drive() function should (1) call the base class (Car) version of test drive(), then print the message "Reserving the test track for your sports car.". (4 pts)

Problem 3. (10 pts) You are given a file data.dat which contains entries consisting of names and ages. For example, the contents of data.dat could be Eve 24 Bob 27 Alice 22 Dave 25 You may assume that each line contains one name followed by one or more whitespace characters followed by a positive integer. The first letter of each name is capitalized, and the names are unique. (a) Write a piece of code that reads data.dat and stores the contents to a std::map where the keys are the names, and the values are the ages. (5 pts)

(b) Write a piece of code that prints the contents of the std::map created in part (a) to std::cout such that (1) the names are in alphabetical order and (2) all the ages are aligned in a second column. For example after reading data.dat as on the previous page, your program should print Alice 22 Bob 27 Dave 25 Eve 24 You may assume that no name contains more than 10 characters. (5 pts)

Problem 4. (10 pts) Consider the definition of the Dragon class: 1 class Dragon { 2 public : 3 Dragon () { 4 std :: cout << " You made a dragon!" << std :: endl ; 5 } 6 7 Dragon () { 8 std :: cout << " You slayed a dragon!" << std :: endl ; 9 } 10 }; (a) What is the output of the following code? (7 pts) 1 void make_ dragons () { 2 Dragon first ; 3 Dragon * second = new Dragon (); 4 } 5 6 int main () { 7 make_ dragons (); 8 return 0; 9 } (b) What is the error with the function void make dragons()? (3 pts)

Problem 5. (10 pts) Consider an std::vector<int> v of size n which is known to contain only values 1 through n (values may appear more than once in v, and some values may not appear at all). (a) Write a function void print missing values(std::vector<int>& v) which prints the values 1 through n which do not appear in v using the following procedure: for each i = 1, 2,..., n, see if v contains i using linear search. If i is found, do nothing; otherwise print i. (4 pts)

(b) Use Big O notation to describe the runtime of print missing values implemented as described in part (a). (2 pts) (c) Describe (without writing any code) how you could improve the runtime of print missing values to O(n log n). (2 pts) (d) Is it possible to implement print missing values in time O(n)? How or why not? (2 pts)

Problem 6. (10 pts) Consider an std::vector<int> v. (a) Assuming that v is sorted (smallest to largest), define a function 1 size_t find ( std :: vector <int >& v, int val, size_t min, size_t max ); which employs binary search to return the index of val if val is contained in v between indices min and max. The function find should return -1 if val is not found. The function find may be either defined recursively or iteratively. (8 pts) (b) Use Big O notation to describe the run time of find when v contains n (sorted) entries. You do not need to justify your answer. (2 pts)

Problem 7. (10 pts) Consider the (simplified) TreeNode class below. 1 class TreeNode { 2 public : 3 TreeNode ( int val ); 4 5 TreeNode * parent ; 6 TreeNode * left_ child ; 7 TreeNode * right_ child ; 8 int value ; 9 }; Implement a function 1 TreeNode * insert ( TreeNode * root, int val ); which searches a Binary Search Tree (BST) for val. If val is found in the BST, insert returns a pointer to the TreeNode containing val. If val is not found, insert should create a new TreeNode with value equal to val, and insert it into the BST so as to maintain the BST properties. You may assume that the TreeNode* root passed to insert is not nullptr.

Problem 8. (10 pts) (a) Starting from an empty Binary Search Tree (BST), draw the BST after inserting in order 3, 1, 8, 5, 4, 7, 6, 9, 2. (4 pts)

(b) For the BST you created in part (a), write the output of printing the contents of the BST during an in-order traversal. (2 pts) (c) For the BST you created in part (a), write the output of printing the contents of the BST during an pre-order traversal. (2 pts) (d) For the BST you created in part (a), write the output of printing the contents of the BST during an post-order traversal. (2 pts)

Problem 9. (10 pts) Consider the function void puzzle(int n) defined by 1 void puzzle ( unsigned int n) { 2 if(n == 1) { 3 std :: cout << "1" << std :: endl ; 4 return ; 5 } 6 7 std :: cout << n << ", "; 8 9 if(n % 2 == 0) { 10 puzzle (n /2); 11 } 12 13 else { 14 puzzle (3 * n + 1); 15 } 16 } (a) Write the output of puzzle for the following inputs (8 pts): puzzle(2): puzzle(3): puzzle(7): puzzle(21): (b) Which best describes the run-time of puzzle(n) when n = 2 k? (circle one) (2 pts) 1. O(1) 2. O(log k) 3. O(k) 4. O(k 2 ) 5. O(2 k ) Fun fact: it is unknown whether the function puzzle(int n) eventually stops for all positive integers n!

Problem 10. (10 pts) Arithmetic expressions involving integers and the operations + and * can be represented using a binary tree as follows. Each internal node (i.e., a node which is not a leaf) stores a single operator, while each leaf stores a single value. For example the tree * + 3 1 2 encodes the expression (1 + 2) * 3. A binary tree constructed in this way is known as an expression tree. To evaluate an expression tree, start at the root, and evaluate the left and right subtrees recursively. Apply the operator at the root to the values returned from the left and right children, and return the new value. Evaluation of a leaf simply returns its value. Consider the class ExpTreeNode defined below: 1 class ExpTreeNode { 2 public : 3 ExpTreeNode (); 4 virtual int evaluate () { return 0;} 5 6 ExpTreeNode * parent ; 7 ExpTreeNode * left_ child ; 8 ExpTreeNode * right_ child ; 9 }; (a) Define and implement a class derived from ExpTreeNode called ValueNode which additionally stores an int value. For ValueNode, redefine the evaluate() function to return the node s value. (2 pts)

(b) Define and implement a class derived from ExpTreeNode called OperatorNode which stores a char op, the operator associated with the node (+ or *). Redefine the evaluate() function for an OperatorNode so that the node evaluates its left and right children, applies the operator specified by op, and returns the result. (4 pts)

(c) What is the result of evaluating the expression tree below? (2 pts) * + + 2 4 1 + 3 5 (d) Write the contents of the tree from part (c) using a post-order traversal. Evaluate the resulting expression as an expression in postfix (reverse Polish) notation. (2 pts) (e) Do you recognize the result of an in-order traversal of the expression tree from (c)? It may be helpful if you include an open parenthesis before processing left children and a closing parenthesis after processing right children... (0 pts)

(blank page)