Review of the Lectures 21-26, 30-32

Similar documents
Priority Queues and Huffman Trees

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

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

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?

Computer Science E-22 Practice Final Exam

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

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

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

Data Structures and Algorithms 2018

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

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

Largest Online Community of VU Students

CSCE 2014 Final Exam Spring Version A

Course Name: B.Tech. 3 th Sem. No of hours allotted to complete the syllabi: 44 Hours No of hours allotted per week: 3 Hours. Planned.

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Table of Contents. Chapter 1: Introduction to Data Structures... 1

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

Expression Trees and the Heap

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

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Q1 Q2 Q3 Q4 Q5 Q6 Total

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

COS 226 Midterm Exam, Spring 2009

MLR Institute of Technology

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Solution to CSE 250 Final Exam

INSTITUTE OF AERONAUTICAL ENGINEERING

Algorithms and Data Structures CS-CO-412

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

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

COMP 250 Winter 2010 Exercises 6 - trees March 2010

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

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

Binary Trees. Height 1

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

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

Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total

Midterm solutions. n f 3 (n) = 3

CS302 Data Structures using C++

Lecture 6 Sorting and Searching

MIDTERM Preparation. CSS 343: Data Structures Algorithms, and Discrete Math II

(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

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

S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear

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

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

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

DS ata Structures Aptitude

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

EE 368. Weeks 5 (Notes)

Data Structures and Algorithms for Engineers

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

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

Huffman Coding. (EE 575: Source Coding Project) Project Report. Submitted By: Raza Umar. ID: g

Sorting Pearson Education, Inc. All rights reserved.

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

PESIT Bangalore South Campus Department of MCA Course Information for

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

CMSC 341 Lecture 15 Leftist Heaps

Visit ::: Original Website For Placement Papers. ::: Data Structure

CS-301 Data Structure. Tariq Hanif

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

Course Review for Finals. Cpt S 223 Fall 2008

CMSC 341 Lecture 10 Binary Search Trees

About this exam review

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

EECS 281 Homework 3 KEY Fall 2004

CMSC 341 Lecture 15 Leftist Heaps

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

CS323: Data Structures and Algorithms Midterm Exam (October 17, 2016) Name:

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

Data Structures And Algorithms

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Lecture 6: Analysis of Algorithms (CS )

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

O(n): printing a list of n items to the screen, looking at each item once.

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

( ) 1 B. 1. Suppose f x

Lecture: Analysis of Algorithms (CS )

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

TREES. Trees - Introduction

Priority Queues. Chapter 9

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Friday Four Square! 4:15PM, Outside Gates

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

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

Code No: R Set No. 1

CS 8391 DATA STRUCTURES

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

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

Transcription:

Review of the Lectures 21-26, 30-32 1 The Final Exam Monday 11 December, BSB 337, from 8AM to 10AM 2 Examples of Questions recursion and memoization enumeration trees, binary search trees, Huffman codes hashing and sorting MCS 360 Lecture 42 Introduction to Data Structures Jan Verschelde, 6 December 2017 Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 1 / 41

Review of the Lectures 21-26, 30-32 1 The Final Exam Monday 11 December, BSB 337, from 8AM to 10AM 2 Examples of Questions recursion and memoization enumeration trees, binary search trees, Huffman codes hashing and sorting Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 2 / 41

The Final Exam Monday 11 December, BSB 337, from 8AM to 10AM. The exam is closed book. No calculators, no computers. This review has the focus on the first 18 lectures, the material before the first midterm exam: 1 Lectures 21 to 26, 30 to 32, chapters 7 to 10 in the textbook. 2 Recursion, memoization, and enumeration. 3 Trees and sorting algorithms. 4 Focus on data structure concepts, not so much on C++ programming. Questions on this review are representative, but the list is by no means exhaustive. Review the quizzes, homework problems, and midterm exams. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 3 / 41

Review of the Lectures 21-26, 30-32 1 The Final Exam Monday 11 December, BSB 337, from 8AM to 10AM 2 Examples of Questions recursion and memoization enumeration trees, binary search trees, Huffman codes hashing and sorting Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 4 / 41

define a recursive function 1 Chebyshev polynomials C n, with deg(c n ) = n, are defined recursively as: C 0 (x) = 1, C 1 (x) = x, for n > 1 : C n (x) = 2xC n 1 (x) C n 2 (x). 1 Define a recursive function with prototype double C ( int n, double x ); that returns the value of the n-th Chebyshev polynomial at x. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 5 / 41

execute a recursive definition 1 Chebyshev polynomials C n, with deg(c n ) = n, are defined recursively as: C 0 (x) = 1, C 1 (x) = x, for n > 1 : C n (x) = 2xC n 1 (x) C n 2 (x). 2 Simulate the call C(5,1.0). Leave the formulas, do not evaluate them. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 6 / 41

memoization 1 Chebyshev polynomials C n, with deg(c n ) = n, are defined recursively as: C 0 (x) = 1, C 1 (x) = x, for n > 1 : C n (x) = 2xC n 1 (x) C n 2 (x). 3 Make a more efficient version of your function via memoization, by storing the results of previous function calls in a vector, maintained by the function. The vector is initialized to hold up to n = 50 when calling the function with n = 1. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 7 / 41

use a stack to make an interative version 1 Chebyshev polynomials C n, with deg(c n ) = n, are defined recursively as: C 0 (x) = 1, C 1 (x) = x, for n > 1 : C n (x) = 2xC n 1 (x) C n 2 (x). 4 Use this stack to give an iterative version of this function. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 8 / 41

make an interative version without a stack 1 Chebyshev polynomials C n, with deg(c n ) = n, are defined recursively as: C 0 (x) = 1, C 1 (x) = x, for n > 1 : C n (x) = 2xC n 1 (x) C n 2 (x). 5 Without using a stack, give an iterative version of this function. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 9 / 41

Review of the Lectures 21-26, 30-32 1 The Final Exam Monday 11 December, BSB 337, from 8AM to 10AM 2 Examples of Questions recursion and memoization enumeration trees, binary search trees, Huffman codes hashing and sorting Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 10 / 41

enumeration 2 To enumerate all numbers divisible by three, we propose a function with prototype void numbers3 ( int n, int k, vector<int> accu, int radix, int sum ); /* Writes all possible numbers of n digits long, every digit is in the range 0..radix-1, where the sum of the digits is divisible 3. The parameter k controls the recursion depth. */ Give a recursive definition of this function. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 11 / 41

the base case void numbers3 ( int n, int k, vector<int> accu, int radix, int sum ) { if(k == n) { if(sum % 3 == 0) { for(int i=0; i<n; i++) cout << accu[i]; cout << endl; } } else // the general case Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 12 / 41

the general case } else { for(int i=0; i<radix; i++) { accu[k] = i; numbers3(n, k+1, accu, radix, sum+i); } } Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 13 / 41

more enumeration 3 To enumerate all possible license plates, we propose a function with prototype void plates ( int n, int k, string accu ); /* Generates and prints all license plates of n characters, accumulating in accu. The k controls the recursion depth. */ Give a recursive definition of this function. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 14 / 41

the base case void plates ( int n, int k, string accu ) { if(k == n) cout << accu << endl; else { // the general case } } Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 15 / 41

the general case for(int i=0; i<26; i++) // letters { char letter = A +i; plates(n, k+1, accu + letter); } for(int i=0; i<10; i++) // numbers { char number = 0 +i; plates(n, k+1, accu + number); } Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 16 / 41

Review of the Lectures 21-26, 30-32 1 The Final Exam Monday 11 December, BSB 337, from 8AM to 10AM 2 Examples of Questions recursion and memoization enumeration trees, binary search trees, Huffman codes hashing and sorting Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 17 / 41

expression trees 4 Use a tree to convert the expression 3*a/(2 + b/4)/c + (5*a)*(b-2) into postfix form. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 18 / 41

expression trees 3*a/(2 + b/4)/c + (5*a)*(b-2) + 3*a/(2+b/4)/c 5*a*(b-2) + / * * 3 a / 2+b/4 c * 5 a b + / * 3*a (2+b/4)/c 5*a b-2-2 Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 19 / 41

expression trees 3*a/(2 + b/4)/c + (5*a)*(b-2) + / * * 3 a + / c 2 b/4 * 5 a b - 2 + / * * 3 a + / 2 / c b 4 * 5 a b - 2 Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 20 / 41

postorder traversal 3*a/(2 + b/4)/c + (5*a)*(b-2) + * 3 a / * / * - + c 5 a b 2 / b 4 A postorder traversal yields the postfix expression: 3 a * 2 b 4 / + c / 5 a * b 2 - * + 2 Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 21 / 41

Huffman codes 5 Create a Huffman code for the message "structure". 1 Give code to use the appropriate data structure of the STL to make a frequency table for the characters in the message. 2 Given the frequency table, make the Huffman code. Draw all intermediate stages in the creation of the Huffman code. 3 Show how to encode and decode the message. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 22 / 41

code to make a frequency table with STL map map<char, int> frequency_table ( string s ) { map<char, int> M; } for(int i=0; i<s.size(); i++) { char c = s[i]; if(m.find(c) == M.end()) M[c] = 1; else M[c]++; } return M; Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 23 / 41

contracting frequency tables 5 Create a Huffman code for the message "structure". f ( ) f ( ) f ( ) f ( ) f ( ) c 1 s 1 r 2 u 2 rt 4 e 1 ce 2 t 2 sce 3 usce 5 s 1 r 2 u 2 rt 4 r 2 t 2 sce 3 t 2 u 2 u 2 Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 24 / 41

making the tree f ( ) f ( ) f ( ) f ( ) f ( ) c 1 s 1 r 2 u 2 rt 4 e 1 ce 2 t 2 sce 3 usce 5 s 1 r 2 u 2 rt 4 r 2 t 2 sce 3 t 2 u 2 u 2 0 1 rt usce Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 25 / 41

splitting the leaf usce f ( ) f ( ) f ( ) f ( ) f ( ) c 1 s 1 r 2 u 2 rt 4 e 1 ce 2 t 2 sce 3 usce 5 s 1 r 2 u 2 rt 4 r 2 t 2 sce 3 t 2 u 2 u 2 0 1 rt 0 1 u sce 0 1 rt usce Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 26 / 41

splitting the leaf rt f ( ) f ( ) f ( ) f ( ) f ( ) c 1 s 1 r 2 u 2 rt 4 e 1 ce 2 t 2 sce 3 usce 5 s 1 r 2 u 2 rt 4 r 2 t 2 sce 3 t 2 u 2 u 2 0 1 0 1 0 1 r t u sce 0 1 rt 0 1 u sce Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 27 / 41

splitting the leaf sce f ( ) f ( ) f ( ) f ( ) f ( ) c 1 s 1 r 2 u 2 rt 4 e 1 ce 2 t 2 sce 3 usce 5 s 1 r 2 u 2 rt 4 r 2 t 2 sce 3 t 2 u 2 u 2 0 1 0 1 0 1 r t u 0 1 s ce 0 1 0 1 0 1 r t u sce Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 28 / 41

splitting the leaf ce f ( ) f ( ) f ( ) f ( ) f ( ) c 1 s 1 r 2 u 2 rt 4 e 1 ce 2 t 2 sce 3 usce 5 s 1 r 2 u 2 rt 4 r 2 t 2 sce 3 t 2 u 2 u 2 1 0 1 0 1 r t u 0 1 s 0 1 c e Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 29 / 41

a Huffman code for structure char code r 00 t 01 u 10 s 110 c 1110 e 1111 0 1 0 1 0 1 r t u 0 1 s 0 1 c e The encoding of structure is 11001001011100110001111. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 30 / 41

binary search trees 6 Consider the sequence 18 12 3 21 33 27 30. 1 Insert the elements in the sequence into a binary search tree. 2 Delete 21 from the binary search tree. 3 Delete 18 from the binary search tree. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 31 / 41

inserting numbers into a binary search tree consider the sequence 18 12 3 21 33 27 30. 12 18 3 12 18 3 18 12 21 18 12 21 3 33 18 12 21 3 33 27 18 12 21 3 33 27 30 Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 32 / 41

deleting a number from a binary search tree Delete 21 from: 18 12 21 3 33 27 30 3 12 18 33 27 30 Observe that 21 had no left child: replacing 21 with its right child yields a binary search tree. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 33 / 41

deleting a number from a binary search tree Delete 18 from: 3 12 18 33 27 30 3 12 33 27 30 12 3 33 27 30 Observe that 18 had two children: replace 18 with the largest in its left child: 12 12 has no right child, its removal is by replacement by its left child. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 34 / 41

Review of the Lectures 21-26, 30-32 1 The Final Exam Monday 11 December, BSB 337, from 8AM to 10AM 2 Examples of Questions recursion and memoization enumeration trees, binary search trees, Huffman codes hashing and sorting Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 35 / 41

hashing 7 What is open addressing? What is chaining? Explain the differences between open addressing and chaining. Give an example of both techniques to illustrate the differences. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 36 / 41

sorting algorithms 7 Consider the sequence 43 19 12 31 62 81 77 10. 1 Sort the numbers in ascending order with bubble sort. Show all intermediate passes. 2 Sort the sequence with quicksort. Show all intermediate stages in the sorting algorithm. 3 Show how to sort the numbers, while selecting each time the smallest element in the sequence and swapping if necessary. 4 Sort the sequence by inserting in a binary search tree. 5 Show how to build the heap in heapsort. Once the heap is built show how removal produces a sequence in ascending order. 6 Apply Shell sort to the sequence, using values 5,3, and 1 for the gap. 7 Show how to apply merge sort to this sequence. 8 Sort the sequence with insertion sort. Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 37 / 41

balance of a tree 8 Give the code to compute the balance of a tree, using the definitions typedef struct node tree; struct node { int info; tree *left; tree *right; }; and the prototype void write_balance ( int k, tree *t ); /* writes the info and balance of every node in inorder traversal, k controls the depth of the recursion (call with k == 0) */ Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 38 / 41

the function write_balance void write_balance ( int k, tree *t ) /* writes the info and balance of every node in inorder traversal, k controls the depth of the recursion (call with k == 0) */ { if(t!= NULL) { for(int i=0; i<k; i++) cout << " "; cout << t->info << " balance : " << balance(t) << endl; write_balance(k+1,t->left); write_balance(k+1,t->right); } } Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 39 / 41

the function balance int balance ( tree *t ); // returns the balance of the tree int balance ( tree *t ) { if(t == NULL) return 0; else return depth(t->left) - depth(t->right); } Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 40 / 41

computing the depth of a tree int depth ( tree *t ) { if(t == NULL) return -1; else if(t->left == NULL) return 1+depth(t->right); else if(t->right == NULL) return 1+depth(t->left); else { int L = depth(t->left); int R = depth(t->right); if(l > R) return L + 1; else return R + 1; } } Introduction to Data Structures (MCS 360) Review Four L-42 6 December 2017 41 / 41