Solution to CSE 250 Final Exam

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

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

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

Data Structures and Algorithms

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

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Largest Online Community of VU Students

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?

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Midterm solutions. n f 3 (n) = 3

COMP 250 Fall recurrences 2 Oct. 13, 2017

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

Analysis of Algorithms

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

CSE 333 Midterm Exam 2/14/14

Analysis of Algorithms

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

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

CS 216 Exam 1 Fall SOLUTION

INF2220: algorithms and data structures Series 1

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lecture 6: Analysis of Algorithms (CS )

Advanced Set Representation Methods

Binary Trees. Height 1

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Unit III - Tree TREES

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

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

Tree Travsersals and BST Iterators

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

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

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

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

Problem Set 5 Due: Friday, November 2

Binary Trees, Binary Search Trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

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

Computer Science Foundation Exam

UNIVERSITY REGULATIONS

Why Do We Need Trees?

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CSCI2100B Data Structures Trees

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

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Final Exam Solutions PIC 10B, Spring 2016

CSCI Trees. Mark Redekopp David Kempe

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Fall, 2015 Prof. Jungkeun Park

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

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

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

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

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

Binary Trees and Binary Search Trees

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

Principles of Algorithm Design

Basic Data Structures (Version 7) Name:

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

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

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

Programming II (CS300)

Binary Trees

Trees, Part 1: Unbalanced Trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Lecture: Analysis of Algorithms (CS )

Data Structures and Algorithms

Lecture 13: AVL Trees and Binary Heaps

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

Data Structures Question Bank Multiple Choice

Section 05: Solutions

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Binary Search Trees. Analysis of Algorithms

ECE250: Algorithms and Data Structures Midterm Review

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

Chapter 20: Binary Trees

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

CS102 Binary Search Trees

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

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

UNIVERSITY REGULATIONS

Course Review for Finals. Cpt S 223 Fall 2008

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

CMSC 341 Lecture 10 Binary Search Trees

Recall: Properties of B-Trees

Transcription:

Solution to CSE 250 Final Exam Fall 2013 Time: 3 hours. December 13, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do not use any extra paper. The space given per question is a lot more than sufficient to answer the question. Please be brief. Longer answers do not get more points! No electronic devices of any kind. You can open your textbook and notes Please leave your UB ID card on the table This booklet must not be torn or mutilated in any way and must not be taken from the exam room Please stop writing when you are told to do so. We will not accept your submission otherwise. If you wanted to, you can answer the extra credit question without answering all of the other questions Your name: Your UB ID: - - The rest of this page is for official use only. Do not write on the page beyond this point. Problem Score Problem Score Problem Score name and id Problem 1 Problem 2 (2 max) (10 max) (4 max) Problem 3 Problem 4 Problem 5 (4 max) (8 max) (8 max) Problem 6 Problem 7 Problem 8 (8 max) (8 max) (4 max) Problem 9 Problem 10 Problem 11 (4 max) (8 max) (8 max) Problem 12 Problem 13 Problem 14 (8 max) (8 max) (8 max) Total Score: (100 max) 1

Problem 1 (10 points). Mark the correct choice(s) or give a brief answer. Each question is worth 1 point. All codes are in C++. 1. Of the following C++ statements, which ones are both declarations and definitions? (Mark all that apply.) int foo(int b) { return b*b; long i; char a= h ; bool easy; typedef mytype string; struct BSTNode; 2. Consider the following fragment char name[] = { D, a, v, i, d, \0, B, l, a, i, n, e, \0 ; cout << name; What does cout print? David Blaine David Blaine None of the above 3. Consider the following fragment int a[] = {1, 2, 3, 4; *(a+2) += 3; What is the value of a[2] after the fragment is executed? 1 2 3 4 5 6 4. Consider the following fragment int a[] = {1, 2, 3, 4; int* p = a+1; p++; (*p)++; What is the value of a[2] after the fragment is executed? 1 2 3 4 5 6 5. Will the following program compile without error? YES NO #include <iostream> using namespace std; int foo(int i) { return i; int main() { int a = 2; int *b = &a; cout << foo(*b) << endl; return 0; 2

6. Suppose you wanted to make use of terminal control routines and the Lexer class I gave and all your codes are put in yourprog.cpp. Write down one g++ compilation line that will compile term control.cpp and produce the object file term control.o g++ -c term_control.cpp 7. Write one line of C++ that defines a new type, named mytype, which represents a pointer to a function that takes two chars and returns nothing. typedef void (*mytype)(char, char); 8. Write one C++ statement that defines a variable mymap, where mymap is a map from string to pointer to function that has two int arguments and returns an int. std::map<string, int (*)(int, int)> mymap; 9. To print all keys of a BST in decreasing order, we use the following traversal order In- Post- Pre- Reverse In- Reverse Post- Reverse Pre- 10. In the B-Tree data structure we discussed in class, the last pointer of each leaf node points to its sibling node on the right (except for the rightmost leaf node, whose last pointer points to NULL). Explain why those pointers point that way? So range queries can be answered quicker. 3

Problem 2 (4 points). Order the following functions in increasing order of asymptotic growth rate. You don t have to explain how you get the order. Solution. n (log n) 3, n, 2 n+10, n 2 (log n) 3, n 3/4, n 2 n, n n. n, n 3/4, n (log n) 3, n n, n 2 (log n) 3, 2 n+10, n 2 n. Problem 3 (4 points). Use the recurrence tree method to solve the recurrence T (n) = 6T (n/2) + n. As usual, you can assume T (k) = O(1) for k 10. Draw the tree. Show your work. Solution. (You should draw the figure.) ( n ) k 1 ( T (n) = 6 k T 2 k + 6 i n ) 2 i i=0 ( n ) k 1 = 6 k T 2 k + n 3 i ( n ) = 6 k T 2 k ( n ) = 6 k T 2 k = 6 k T i=0 k 1 + n i=0 3 i + n 3k 1 2 ( n 2 k ) + Θ(3 k n). Select k such that n/2 k = 1 or k = log 2 n. This means 3 k = n log 2 3 6 k = n log 2 6. Thus, T (n) = n log 2 6 T (1) + Θ(n log 2 3 n) = Θ(n log 2 6 ). 4

Problem 4 (8 points). You can assume that using namespace std; is at the top of the file. 1. (4) Write a C++ function iterative range count() that takes a stack st of int and an integer a as arguments, and returns the number of integers in st that are at least a. int iterative_range_count(stack<int> st, int a) { int count = 0; while (!st.empty()) { if (st.top() >= a) count++; st.pop(); return count; 2. (4) Write a C++ function recursive range count() that solves the same problem recursively. int recursive_range_count(stack<int> st, int a) { if (st.empty()) return 0; int tmp = st.top() >= a? 1 : 0; st.pop(); return tmp + recursive_range_count(st, a); 5

Problems 5-7 make use of the following linked list Node structure: struct Node { int key; Node* next; Node(int k=0, Node* n=null) : key(k), next(n) {; ; Problem 5 (8 points). Write a function median element that takes a head pointer to a NULLterminated singly linked list whose keys are sorted in increasing order. All keys are distinct. The function returns a pointer to the node which stores the median key of all keys. If there are n keys then the median key is the n/2 -smallest key. (If the list is empty then NULL is returned.) For example, if the input list is a.1 b.5 c.7 d.8 NULL then a pointer to b is returned (n = 4 in this case). As another sample, if the input list is a.1 b.5 c.7 d.8 e.10 NULL then a pointer to c is returned (n = 5 in this case). Most importantly, you can only use one while loop in your function, and that s the only looping structure you can use. In particular, the easy solution of looping through to find n, and traverse the second time to report the median is not valid. (Hint: recall the linked-list cycle detection problem I discussed in class.) Node* median_element(node* head) { if (head == NULL) return NULL; Node *slow = head, *fast = head; while (fast->next!= NULL && fast->next->next!= NULL) { slow = slow->next; fast = fast->next->next; return slow; 6

Problem 6 (8 points). Write a function fold list() that takes a head pointer to a NULL-terminated singly linked list consisting of Nodes. The function modifies the list in the following way: it cuts the list in half, moves the first half to the end, and returns the head pointer to the new list. Only pointer manipulation is allowed. If the list has an odd number of elements, then the middle element belongs to the first half. If the list is empty, NULL is returned. Feel free to call the median element() function from Problem 5. For example, if the input list is then the output list is a.4 b.1 c.3 d.6 e.19 f.14 NULL d.6 e.19 f.14 a.4 b.1 c.3 NULL, and a pointer to d is returned. And, if the input list is a.4 b.1 c.3 d.6 e.19 NULL then the output list is and a pointer to d is returned. d.6 e.19 a.4 b.1 c.3 NULL, Node* fold_list(node* head) { if (head == NULL head->next == NULL) return head; Node* first_tail = median_element(head); Node* second_tail = head; while (second_tail->next!= NULL) second_tail = second_tail->next; Node* new_head = first_tail->next; first_tail->next = NULL; second_tail->next = head; return new_head; 7

Problem 7 (8 points). Write a function difference() that takes two head pointers to Node as arguments. Each head pointer points to the head element of a NULL-terminated singly linked list which contains distinct keys sorted in increasing order. (The keys within each list are distinct, but keys in different lists aren t necessarily different.) The function difference() returns a new sorted linked list of Nodes that contain all keys of the two input linked lists that are not in common between the two lists. You have to keep the input linked lists intact. For example, if the two input lists are Then, the output list is a.1 b.4 c.7 d.16 NULL e.2 f.4 g.9 h.16 i.17 NULL a.1 e.2 c.7 g.9 i.17 NULL and a pointer to a is returned. (I used a, e, c, g, i because they are not the same nodes as the ones from the input lists.) Write the function so that its asymptotic running time linear in the total input list size. Node* difference(node* head1, Node* head2) { Node* head = NULL; while (head1!= NULL && head2!= NULL) { if (head1->key < head2->key) { head = new Node(head1->key, head); head1 = head1->next; else if (head1->key > head2->key) { head = new Node(head2->key, head); head2 = head2->next; else { // they are equal head1 = head1->next; head2 = head2->next; // at most one of the following while loops will ever execute while (head1!= NULL) { head = new Node(head1->key, head); head1 = head1->next; while (head2!= NULL) { head = new Node(head2->key, head); head2 = head2->next; return reverse_sll(head); // copy from lecture 13 s slides Node* reverse_sll(node* head) { Node *prev = NULL, *temp; while (head!= NULL) { temp = head->next; head->next = prev; prev = head; head = temp; return prev; 8

Problem 8 (4 points). Draw the binary tree that has the following inorder and postorder sequences Inorder : 4 15 6 10 8 7 5 Postorder: 4 6 15 7 8 5 10 10 / \ 15 5 / \ / 4 6 8 \ 7 Problem 9 (4 points). Suppose we insert node 3 into the following AVL tree; Show where 3 is inserted at first, then circle the node that becomes imbalanced and explain how the re-balance step works to keep the AVL property. Draw the resulting AVL tree. 2 [2] <-- imbalanced, Right-Left case / \ / \ do a right rotate at 8 1 8 [insert 3] 1 8 then a left rotate at 2 / \ / \ 4 9 4 9 / 3 after right rotate at 8 after left rotate at 2 2 4 / \ / \ 1 4 2 8 / \ / \ \ 3 8 1 3 9 \ 9 9

Problem 10 (8 points). (a) Suppose we splay node 6 in the following tree, what does the resulting tree look like? Draw all the intermediate trees after each zig-zig, zig-zag, or zig baby step and label the step with the name zig-zig, zig-zag, or zig. 4 4 6 / \ / \ / \ 2 10 2 10 4 10 / \ zig-zig / \ zig-zag / / \ 8 11 ------> 6 11 ------> 2 7 11 / \ \ \ 7 9 7 8 / \ \ 6 8 9 \ 9 (b) Consider the following Binary Search Tree. Is it an AVL Tree? Why or why not? Put a color red (R) or black (B) next to each node so that it is a red-black tree. (The x are NULL nodes.) 40 it is an AVL tree, because at every / \ node the height of the left differs 30 50 from the height of the right subtree / \ / \ by at most 1 10 35 x 60 / \ / \ / \ 8 15 x x x x / \ / \ x x x x 40B / \ 30R_ 50B / \ / \ 10B 35B xb 60R / \ / \ / \ 8R 15R xb xb xb Bx / \ / \ xb xbxb xb 10

Problems 10 to 14 make use of the following binary search tree node structure: struct BSTNode { int key; BSTNode* left; BSTNode* right; BSTNode* parent; ; Problem 11 (8 points). Write a function that takes a pointer to the root of a binary search tree with the above BSTNode structure, and that removes the node with the minimum key from the tree. Note that the root pointer is passed by reference. If the minimum node is the root node then the root pointer has to be modified properly. All your code has to be contained within the function, do not write any auxiliary function. // this is virtually the same as a question in the sample exam void delete_min(bstnode*& root) { if (root == NULL) return; BSTNode* min = root; while (min->left!= NULL) min = min->left; BSTNode* p = min->parent; // NULL if min == root if (p == NULL) { root = root->right; // new root root->parent = NULL; else { p->left = min->right; if (min->right!= NULL) min->right->parent = p; delete min; // don t forget to release memory 11

Problem 12 (8 points). Write a function common ancestors() that takes two (not necessarily distinct) pointers to BSTNodes in a BST and returns a vector of pointers to nodes in the tree which are the common ancestors of the two given nodes. (Note that by definition a node is an ancestor of itself.) vector<bstnode*> common_ancestors(bstnode* node1, BSTNode* node2) { stack<bstnode*> s1, s2; while (node1!= NULL) { s1.push(node1); node1 = node1->parent; while (node2!= NULL) { s2.push(node2); node2 = node2->parent; vector<bstnode*> ret; while (!s1.empty() &&!s2.empty()) { if (s1.top() == s2.top()) { ret.push(s1.top()); s1.pop(); s2.pop(); else { break; return ret; 12

Problem 13 (8 points). (a) Write a function size() that takes the root pointer of a binary tree with the BSTNode structure and returns the size of the tree, which is the number of non-null nodes in the tree. size_t size(bstnode* root) { if (root == NULL) return 0; return 1 + size(root->left) + size(root->right); (b) Write a function rank() that takes a pointer node to a node (not necessarily the root) of a binary search tree with the BSTNode structure and returns the rank of that node. The rank of a node is k if the node s key is the kth smallest key among all keys. You can assume that all keys in the tree are distinct. You should make use of the size() function from part (a). If node is NULL then 0 is returned. size_t rank(bstnode* node) { if (node == NULL) return 0; size_t r = 1 + size(node->left); while (node->parent!= NULL) { if (node->parent->right == node) { r += 1 + size(node->parent->left); node = node->parent; return r; 13

Problem 14 (8 points). Write a function last node depth k() that takes the root pointer of a binary tree with the BSTNode structure, a non-negative integer k and returns a pointer to the last node at depth k of the tree. (Last as we go from left to right.) The root node is at depth 0, the root s children are at depth 1, and so on. If k is greater than the largest depth, then NULL is returned. For example, consider the tree drawn in Problem 10b. The last node at depth 1 is node 50, at depth 2 is node 60, at depth 3 is 15, at any depth > 3 is NULL. // this is a slight variation of a question from the sample exam BSTNode* last_node_depth_k(bstnode* root, size_t k) { if (root == NULL) return NULL; // boundary case size_t current_depth = 0; deque<btnode*> q; q.push_front(root); size_t c=1; // c = of nodes at current depth still in q size_t n=0; // n = # of nodes at next depth already in q while (!q.empty()) { BTNode* cur = q.back(); // head of q q.pop_back(); c--; if (cur->left!= NULL) { q.push_back(cur->left); n++; if (cur->right!= NULL) { q.push_back(cur->right); n++; if (c == 0) { if (current_depth == k) return cur; c = n; n = 0; current_depth++; return NULL; 14