Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 27

Similar documents
Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 20

Chapter 20: Binary Trees

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

Data and File Structures Laboratory

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

Trees. A tree is a directed graph with the property

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

CS 241 Data Organization Binary Trees

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

ESC101N: Fundamentals of Computing End-sem st semester

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

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Trees (part 2) CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

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

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

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

Heaps / Priority Queues

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Binary Trees, Binary Search Trees

March 20/2003 Jayakanth Srinivasan,

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Binary Trees. Height 1

INF2220: algorithms and data structures Series 1

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

INF2220: algorithms and data structures Series 1

CS 223: Data Structures and Programming Techniques. Exam 2

Fall, 2015 Prof. Jungkeun Park

CS24 Week 8 Lecture 1

Binary Trees

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

CSE373 Midterm I Fall 2009 (Closed book, closed notes)

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

COMP : Trees. COMP20012 Trees 219

BBM 201 Data structures

DC54 DATA STRUCTURES DEC 2014

Trees. CSE 373 Data Structures

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

Introduction to C Language (M3-R )

CSCI 2132 Software Development. Lecture 19: Generating Permutations

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

CP2 Revision. theme: dynamic datatypes & data structures

Solution to CSE 250 Final Exam

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

Data Structures Question Bank Multiple Choice

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Computer Science /21/2000

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

Binary Trees. Examples:

Lecture 6: Analysis of Algorithms (CS )

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?

Binary Trees and Binary Search Trees

MLR Institute of Technology

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Recursive Data Structures and Grammars

Dynamic memory allocation

Unit III - Tree TREES

CMSC 341 Lecture 10 Binary Search Trees

Recursion, Trees and Tree Traversal. Module CE System Programming & Computer Control Systems

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

Programming II (CS300)

DATA STRUCTURES USING C

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

CSI33 Data Structures

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

CSCI 2132 Software Development. Lecture 18: Functions

C: How to Program. Week /Apr/16

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

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

CMSC351 - Fall 2014, Homework #2

TREES. Trees - Introduction

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

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

ECE264 Fall 2013 Exam 3, November 20, 2013

We have the pointers reference the next node in an inorder traversal; called threads

CSC 467 Lecture 13-14: Semantic Analysis

BBM 201 DATA STRUCTURES

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

Cpt S 122 Data Structures. Data Structures Trees

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

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

STUDENT LESSON AB30 Binary Search Trees

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Procedural programming with C

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

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

CSL 201 Data Structures Mid-Semester Exam minutes

CSCI 171 Chapter Outlines

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

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

Topic Binary Trees (Non-Linear Data Structures)

Transcription:

Recursion Data and File Structures Laboratory http://www.isical.ac.in/~dfslab/2017/index.html DFS Lab (ISI) Recursion 1 / 27

Definition A recursive function is a function that calls itself. The task should be decomposable into sub-tasks that are smaller, but otherwise identical in structure to the original problem. The simplest sub-tasks (called the base case) should be (easily) solvable directly, i.e., without decomposing it into similar sub-problems. DFS Lab (ISI) Recursion 2 / 27

Recursive vs. iterative implementations 1 int factorial ( int n ) { 2 int prod; 3 4 if (n < 0) return (-1); 5 prod = 1; 6 while (n > 0) { 7 prod *= n; 8 n = n - 1; 9 } 10 return (prod); 11 } DFS Lab (ISI) Recursion 3 / 27

Recursive vs. iterative implementations 1 int factorial ( int n ) /* Recursive implementation */ 2 { 3 if (n < 0) return (-1); /* Error condition */ 4 if (n == 0) return (1); /* Base case */ 5 return(n * factorial(n-1)); /* Recursive call */ 6 } DFS Lab (ISI) Recursion 4 / 27

Z curve. Problem statement. Consider a 2-D matrix of size 2 m 2 m. The entries of the matrix are, in row-major order, 1, 2, 3,..., 2 2m. Print the entries of the matrix in Z-curve order (as shown in the picture below).. DFS Lab (ISI) Recursion 5 / 27

Z curve: structure (0, 0) Problem structure: Base case: single element Recursive structure: break given square into 4 sub-squares process squares in Z-curve order (N 1, N 1) DFS Lab (ISI) Recursion 6 / 27

Z curve: code I void z_curve(int top_left_row, int top_left_column, int bottom_right_row, int bottom_right_column, int **matrix) { /* Base case */ if (top_left_row == bottom_right_row && top_left_column == bottom_right_column) { printf("%d ", matrix[top_left_row][top_left_column]); return; } /* Recurse */ /* upper-left sub-square */ z_curve(top_left_row, top_left_column, (top_left_row + bottom_right_row)/2, (top_left_column + bottom_right_column)/2, matrix); DFS Lab (ISI) Recursion 7 / 27

Z curve: code II /* upper-right sub-square */ z_curve(top_left_row, (top_left_column + bottom_right_column)/2 + 1 (top_left_row + bottom_right_row)/2, bottom_right_column, matrix); /* lower-left sub-square */ z_curve((top_left_row + bottom_right_row)/2 + 1, top_left_column, bottom_right_row, (top_left_column + bottom_right_column)/2, matrix); } /* lower-right sub-square */ z_curve((top_left_row + bottom_right_row)/2 + 1, (top_left_column + bottom_right_column)/2 + 1, bottom_right_row, bottom_right_column, matrix); return; DFS Lab (ISI) Recursion 8 / 27

Permutations. Algorithm. To generate all permutations of 1, 2, 3,..., n, do the following: 1. Generate all permutations of 2, 3,..., n, and add 1 to the beginning. 2. Generate all permutations of 1, 3, 4,..., n and add 2 to the beginning..... n. Generate all permutations of 1, 2,..., n 1 and add n to the beginning. DFS Lab (ISI) Recursion 9 / 27

Permutations: code void permute(int *A, int k, int n) { int i; if(k==n) { for (i = 0; i < n; i++) { printf("%d ", A[i]); } putchar( \n ); return; } for(i = k; i < n; i++){ SWAP(A, i, k); permute(a, k+1, n); SWAP(A, k, i); } } return; DFS Lab (ISI) Recursion 10 / 27

Binary trees. (Recursive) Definition. A binary tree over a domain D is either: the empty set (called an empty binary tree); or. a 3-tuple S 1, S 2, S 3 where S 1 D, (called the root) and S 2 and S 3 are binary trees over D (called the left and right subtree resp.) DFS Lab (ISI) Recursion 11 / 27

Binary tree properties Height: h Number of nodes: n Number of leaves: l DFS Lab (ISI) Recursion 12 / 27

Binary tree traversals Preorder Inorder Postorder DFS Lab (ISI) Recursion 13 / 27

Binary tree implementation Conventional implementation: typedef struct tnode { DATA d; struct tnode *left, *right; struct tnode *parent; // optional } TNODE; One malloc per node Nodes may be scattered all over the heap area DFS Lab (ISI) Recursion 14 / 27

Binary tree implementation Alternative implementation: typedef struct tnode { DATA d; int left, right; int parent; //optional } TNODE; One initial malloc and reallocs as needed All nodes located within the same array DFS Lab (ISI) Recursion 15 / 27

Binary tree implementation Alternative implementation: typedef struct tnode { DATA d; int left, right; int parent; //optional } TNODE; One initial malloc and reallocs as needed All nodes located within the same array Initially: root = -1 DATA left right free 0 1-1 1 2-1 2 3-1 3 4-1.. n-1-1 -1 DFS Lab (ISI) Recursion 15 / 27

Binary tree implementation Alternative implementation: root = 0 DATA left right A 0 A 1 2 1 B 3 4 2 C -1 5 3 D -1-1 4 E -1-1 5 F -1-1 free 6 7-1 D B E C F.. n-1-1 -1 DFS Lab (ISI) Recursion 16 / 27

Problems Day I 1. Towers of Hanoi: see https://en.wikipedia.org/wiki/tower_of_hanoi CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=228623 2. Write a recursive function with prototype int C(int n, int r); to compute the binomial coefficient using the following definition: ) ( ) n 1 ( n r = r ) + Supply appropriate boundary conditions. ( n 1 r 1 DFS Lab (ISI) Recursion 17 / 27

Problems Day II 3. Define a function G(n) as: 0 if n = 0 1 if n = 1 G(n) = 2 if n = 2 G(n 1) + G(n 2) + G(n 3) if n 3 Write recursive and iterative (i.e., non-recursive) functions to compute G(n). DFS Lab (ISI) Recursion 18 / 27

Problems Day III 4. What does the following function compute? int f ( int n ) { int s = 0; while (n--) s += 1 + f(n); return s; } 5. http://cse.iitkgp.ac.in/~abhij/course/lab/pds/spring15/ A4.pdf DFS Lab (ISI) Recursion 19 / 27

Problems Day IV For the following problems, some test cases (example trees) are available from the course home page. You may adapt the following code snippet to read in a tree and store it in an array (using the Alternative Implementation). scanf("%u", &numnodes); if (NULL == (tree = (NODE *) malloc(numnodes * sizeof(node)))) { fprintf(stderr, "Out of memory\n"); exit(1); } for (node = tree, i = 0; i < numnodes; node++, i++) scanf("%d %d %d", &(node->data), &(node->left), &(node->right)); 6. Given a binary tree with integer-valued nodes, and a target value, determine whether there exists a root-to-leaf path in the tree such that the sum of all node values along that path equals the target. Modify your program to consider all paths, not just root-to-leaf paths. DFS Lab (ISI) Recursion 20 / 27

Problems Day V 7. Given a binary tree stored in an array (as in the Alternative Implementation), and the indices of two nodes in the tree, find the index of the node that is the lowest common ancestor of the given nodes. 8. Write a program to print a binary tree, rotated anti-clockwise by 90 on the screen. For example, for the tree on slide 16, your output should like something like: A C B F E D Now, try to write a program that will print the same tree in the following format: DFS Lab (ISI) Recursion 21 / 27

Problems Day VI ----A---- --B-- C-- D E F DFS Lab (ISI) Recursion 22 / 27

Appendix: storage issues, activation records DFS Lab (ISI) Recursion 23 / 27

How does the computer handle function calls? 1 void main(void) 2 {... 3 m = f(x, y*z); 4... 5 } 6 7 int f(int a, int b) 8 {... 9 if (a > 0) 10 p = g(b); 11 else 12 p = h(b / 2); 13 return p; 14 } 15 16 int g(int m) 17 {... } 18 19 int h(int n) 20 {... } DFS Lab (ISI) Recursion 24 / 27

How does the computer handle function calls? 1 void main(void) 2 {... 3 m = f(x, y*z); 4... 5 } 6 7 int f(int a, int b) 8 {... 9 if (a > 0) 10 p = g(b); 11 else 12 p = h(b / 2); 13 return p; 14 } 15 16 int g(int m) 17 {... } 18 19 int h(int n) 20 {... } 1. Let a = x, b = y*z. 2. Execute the statements in f(). (a) If a is positive, let m = b. Execute the statements in g(). (b) Otherwise, let n = b/2. Execute the statements in h(). (c) In either case, return the value stored in p (obtained from g() or h()). 3. Continue from line 4. DFS Lab (ISI) Recursion 24 / 27

Storage for local variables Local variables stored in a region of memory called an activation record Need to keep track of nested calls DFS Lab (ISI) Recursion 25 / 27

Storage for local variables Local variables stored in a region of memory called an activation record Need to keep track of nested calls Where are the activation records stored? DFS Lab (ISI) Recursion 25 / 27

Storage for local variables Local variables stored in a region of memory called an activation record Need to keep track of nested calls Where are the activation records stored? simple solution: one block of memory per function does not work for recursive functions DFS Lab (ISI) Recursion 25 / 27

Storage for local variables Local variables stored in a region of memory called an activation record Need to keep track of nested calls Where are the activation records stored? simple solution: one block of memory per function does not work for recursive functions better solution: one block of memory per function call activation records stored in a chunk of memory called activation stack when a function is called, its activation record is added to the end of the activation stack; when function returns, its activation record is removed. works for recursive functions DFS Lab (ISI) Recursion 25 / 27

Review question slide I Which of read data1, read data2, read data3 is best? 1 typedef struct { 2 char name[64]; 3 int roll, rank; 4 float percent; 5 } STUDENT; 6 7 STUDENT *read_data1(void) 8 { STUDENT s; 9 scanf("%s %d %d %f", 10 &(s.name[0]), &(s.roll), 11 &(s.rank), &(s.percent)); 12 return &s; 13 } DFS Lab (ISI) Recursion 26 / 27

Review question slide II 14 15 STUDENT read_data2(void) 16 { STUDENT s; 17 scanf("%s %d %d %f", 18 &(s.name[0]), &(s.roll), 19 &(s.rank), &(s.percent)); 20 return s; 21 } 22 23 STUDENT *read_data3(student *s) 24 { scanf("%s %d %d %f", 25 &(s->name[0]), &(s->roll), 26 &(s->rank), &(s->percent)); 27 return s; 28 } DFS Lab (ISI) Recursion 27 / 27