Please submit your request directly to Noha Sinno

Similar documents
Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

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

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

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

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

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

ECE250: Algorithms and Data Structures Midterm Review

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

Basic Data Structures (Version 7) Name:

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

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

Matriculation number:

We've tried to include the common errors and grading standard for every question.

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

What will the output of the following code snippet be? Why? What is a safer alternate implementation?

CMSC351 - Fall 2014, Homework #2

CS 241 Data Organization Binary Trees

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

[ DATA STRUCTURES ] Fig. (1) : A Tree

12 Abstract Data Types

COMP 524 Spring 2018 Midterm Thursday, March 1

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)

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

INF2220: algorithms and data structures Series 1

Frequently asked Data Structures Interview Questions

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

CmpSci 187: Programming with Data Structures Spring 2015

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

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

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

Exam Data structures DAT036/DAT037/DIT960

3137 Data Structures and Algorithms in C++

Course Review. Cpt S 223 Fall 2009

UNIVERSITY REGULATIONS

Programming Abstractions

CS 216 Exam 1 Fall SOLUTION

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CSE 214 Computer Science II Heaps and Priority Queues

! 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)

Largest Online Community of VU Students

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;

EE 368. Week 6 (Notes)

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

CS301 - Data Structures Glossary By

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

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CSCE 2014 Final Exam Spring Version A

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

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

Computer Science Foundation Exam

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Chapter 20: Binary Trees

Computer Science Foundation Exam

CSE 373 Spring Midterm. Friday April 21st

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

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

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

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

DATA STRUCTURES AND ALGORITHMS

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Assignment 8 CSCE 156/156H/RAIK 184H Spring 2017

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

COSC160: Data Structures Heaps. Jeremy Bolton, PhD Assistant Teaching Professor

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

CMSC 341 Lecture 14: Priority Queues, Heaps

Second Examination Solution

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

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

CS 104 (Spring 2014) Final Exam 05/09/2014

CP2 Revision. theme: dynamic datatypes & data structures

COS 226 Algorithms and Data Structures Fall Midterm

Course Review. Cpt S 223 Fall 2010

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Lecture 15 Binary Search Trees

Data Structures Question Bank Multiple Choice

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

The questions will be short answer, similar to the problems you have done on the homework

An Introduction to Trees

Course Review for Finals. Cpt S 223 Fall 2008

Algorithm Design and Analysis

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

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

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

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

CMSC 341 Leftist Heaps

Programming II (CS300)

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Chapter 9 STACK, QUEUE

8. Binary Search Tree

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Dictionaries. Priority Queues

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

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

Transcription:

Note to Students: This file contains sample solutions to the term test. Please read the solutions carefully. Make sure that you understand why the solutions given here are correct, that you understand the mistakes that you made (if any), and that you understand why your mistakes were mistakes. Remember that although you may not agree completely with the marking scheme used to grade your paper, it was followed the same way for all students. We will remark your test only if you clearly demonstrate that there is a need for this. For all remark requests: Please submit your request directly to Noha Sinno (noha.sinno@alum.utoronto.ca) Your request must be a document which contains a clear description of why there is a need for remark and a scanned copy of the solution you had provided to the question in doubt Please remember that your question will be completely remarked and there is a chance that more marks maybe deducted Page 1 of 10

Question 1. [30 marks] Please provide short answers to each one of the following sub-questions. Part (a) [1 mark] What is the purpose of function pointers? Function pointers store addresses of function handles. These are useful for generic implementation and abstraction. Part (b) [2 marks] Consider the following code snippet. What will be the result stored in the variable res. Specify your answer in decimal base. int res; int leftopr = 13; int rightopr = 10; res=leftopr & (rightopr << 1); The result is 4. Part (c) [3 marks] Suppose that struct Cart *c is a pointer pointing to a valid structure variable in memory consisting of two members int weight and struct Cart * nextcart that are also populated with valid values. List at least three different ways in which you can access the weight of the nextcart member starting from c. c->nextcart->weight; *c.nextcart->weight; *(*c.nextcart).weight; *(c->nextcart).weight; Part (d) [1 mark] Consider the following code snippet in which the structure Cart has the same definition as in the previous question. Are there any issues with it? Why or why not? struct Cart c; struct Cart * cptr=&c; cptr=(struct Cart *)malloc(sizeof(struct Cart)); free(cptr); Nothing is wrong with the code. c is a local variable which will be removed from memory once this function completes execution. Hence, reassigning the pointer variable which originally pointed to the local variable will not cause any problems as reference to that variable is still available via the variable name c. The dynamically created variable is also freed immediately. Hence, there will be no issues associated with dangling pointers or memory leaks. Page 2 of 10

Part (e) [2 marks] What is the purpose of asymptotic complexity analysis (big-oh)? When a problem is NP-hard, what does that imply? Part (f) Complexity analysis provides a good idea about the amount of resourced requires especially when the problem is large. NP-hard means that the problem cannot be solved in deterministic time when n is large. [4 marks] What are the advantages of using a linked list over arrays and vice versa? Include in your answer the complexity of insertion, deletion and accessing content in O(.) notation and n where n is the number of elements already inserted. Part (g) Linked lists: dynamic growth, no restriction on the size. Insertion is O(n), deletion is O(n) and accessing is O(p) if you know it is located at the p th node. O(n) is also acceptable. Arrays: easier to access and but are fixed in size. Insertion is O(n) or O(n-p) is the element is located at index p, deletion is O(n p) or O(n) and accessing an element is O(1) if you know it is located at the p th index. [4 marks] What are the differences and similarities between queues and stacks? Similarities: Queues and stacks are linear data structures. Operations are O(1). Differences: Due to the different queueing policies (FIFO and LIFO), insertion and deletion operations are different. The remaining questions are based on Fig. 1. Root in each tree is the top-most node. Figure 1: Tree diagrams Page 3 of 10

Part (h) [4 marks] Consider T1 in this question: Is this a heap, binary search tree, neither or both? Why or why not? What is the height of this tree? Is this tree full, complete or neither? Why or why not? List the order in which elements will be printed when the tree is traversed in pre-order. Part (i) Neither: Not a heap as the tree is not complete and not a BST as the elements are not inserted in the BST order. 3 Neither: Not full as node 9 has one child. Not complete as level 2 is not completely filled with nodes. 10-5-2-1-0-4-9-7 [5 marks] Consider T2 in this question: Is this a heap, binary search tree, neither or both? Why or why not? What is the height of this tree? Is this tree full, complete or neither? Why or why not? List the order in which elements will be printed when the tree is traversed in in-order. Do you notice anything unique? Part (j) BST: nodes are listed in the BST order. Not a heap. 3 Full and complete: All nodes have 2 or 0 children. All levels are filled except for the last level and in the last level nodes are filled from left to right. 1-5-6-10-11-15-16-17-18. Printed in ascending order [4 marks] Consider T3 in this question: Is this a heap, binary search tree, neither or both? Why or why not? What is the height of this tree? Is this tree full, complete or neither? Why or why not? List the order in which elements will be printed when the tree is traversed in level-order. Page 4 of 10

BST and heap: Node 5 has a smaller value than the parent and this ordering heeds both the BST and heap ordering properties. 1 Complete: Not full as node 10 has only one child. This is a complete tree as all levels (i.e. level 0) is filled with the maximal number of nodes possible (1 node) and in the last level there is only one node in the left most position. 10-15. Page 5 of 10

Question 2. [10 marks] In this question, you will derive the computational complexity class g(n) of the provided code snippet with respect to n. Assume that n = 2 k where k is a positive integer. Do ensure that you include all steps and a formal proof to show that the cost f(n) of executing this function indeed falls into the complexity class g(n). O(n). Solution is similar to Review Problem (1f). Page 6 of 10

Question 3. [10 marks] Implement the void pop(struct Stack * s, struct Data * d) operation that can be performed on a stack using only queues. The Stack structure is defined as follows: struct Stack{ struct Queue * q1; struct Queue * q2; int count; ; The Data structure is defined as follows: struct Data{ int data; ; If the stack is empty, then set the data member of the content pointed to by d to -1. You can assume that you are provided with the definitions of the following interface functions: void enqueue(struct Queue * q, struct Data d); This function inserts into the queue q a node containing the data d void dequeue(struct Queue * q, struct Data * d); This function removes from the queue q a node and indirectly returns the content to the original function call via d which points to valid space in memory int isempty(struct Queue * q); This function returns 1 if the q contains no nodes and 0 otherwise You can assume that the arguments passed to the pop function are pointers to valid spaces in memory which are initialized appropriately. There are two restrictions. One is that you must use two queues and the other is that only the interface functions provided above must be used to access the queues (i.e. you must not directly access the content of the queues). Page 7 of 10

void pop(struct Stack * s, struct Data * d){ struct Queue * tempq; int numnodes=s->count; if (s->count!=0) { while (numnodes>1) { dequeue(s->q1, d); enqueue(s->q2,*d); numnodes--; dequeue(s->q1, d); tempq=s->q1; s->q1=s->q2; s->q2=tempq; s->count--; else{ (*d).data=-1; Page 8 of 10

Question 4. [10 marks] In this question, you will implement the function void convertbsttoheap(struct Node * bstroot, struct Data * heaparray) which copies data contained within each node in a Binary Search Tree (BST) (represented by the root pointer bstroot) into an array represented by the single pointer heaparray so that the resulting array is a sequential representation of a heap. You can assume that heaparray is a dynamically allocated array containing sufficient space to store MAXSIZE number of struct Data variables and that MAXSIZE is much larger than the number of nodes in the BST. The Node and Data structures are defined as follows: struct Node{ struct Node * lchild; struct Node * rchild; Struct Data d; ; struct Data{ int data; ; You can implement helper functions if necessary. Note that more than one heap can be constructed using the provided BST (i.e. there is no unique heap representation). Just ensure that the resulting sequential representation preserves the heap ordering and structural properties. Following is an example of a BST. Figure 2: Tree diagrams Hint: What tree traversal results in the listing of elements in a BST in ascending order? How can these elements be stored into the array so that this is a sequential representation of a heap? void insertrevinorder(struct Node * root, struct Data * ha, int * count) { if (root!=null) { insertrevinorder(root->rchild, ha, count); ha[*count]=root->d; *count=(*count)+1; printf("hello %d data %d\n", *count,root->d.data); insertrevinorder(root->lchild, ha, count); Page 9 of 10

CSC 190 H1S Test #2 Winter 2016 void convertbsttoheap(struct Node * bstroot, struct Data * ha) { int count=1; insertrevinorder(bstroot, ha, &count); Page 10 of 10