Review: recursion Tree traversals

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
Recursion.  Understand how the Fibonacci series is generated  Recursive Algorithms  Write simple recursive algorithms  Analyze simple recursive algorithms.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
1 Introduction to Binary Trees. 2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Recursion Chapter 2 Objectives Upon completion you will be able to:
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Binary Trees.
Recursion.
Fibonacci’s rabbits Fibonacci posed the following problem:
Insertion sort Loop invariants Dynamic memory
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion CENG 707.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
ADT description Implementations
Chapter 15 Recursion.
Introduction to Recursion
5.13 Recursion Recursive functions Functions that call themselves
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Computer Science 4 Mr. Gerb
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Hassan Khosravi / Geoffrey Tien
Cinda Heeren / Geoffrey Tien
More Computational Theory
Cinda Heeren / Geoffrey Tien
Data Structures & Algorithm Design
Recursion Chapter 10.
Cinda Heeren / Geoffrey Tien
Tree data structure.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Introduction to Trees IT12112 Lecture 05.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Tree data structure.
Algorithm design and Analysis
Tree data structure.
Recursion Chapter 11.
Hassan Khosravi / Geoffrey Tien
Search,Sort,Recursion.
Basics of Recursion Programming with Recursion
Binary Trees.
Java Programming: Chapter 9: Recursion Second Edition
Yan Shi CS/SE 2630 Lecture Notes
Recursion.
Chapter 20: Binary Trees.
Last Class We Covered Recursion Stacks Parts of a recursive function:
Tree traversals BST properties Search Insertion
Presentation transcript:

Review: recursion Tree traversals October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Rabbits! ...and recursion What happens when you put a pair of rabbits in a field? More rabbits! Let’s model the rabbit population, with a few assumptions: Newly-born rabbits take one month to reach maturity and mate Each pair of rabbits produces another pair of rabbits one month after mating Rabbits never die October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien More rabbits... How many rabbit pairs are there after 5 months? Month 1 2 3 4 5 6 Month 1: start – 1 pair Month 2: first pair are now mature and mate – 1 pair 1 1 2 3 5 8 Month 3: first pair give birth to a pair of babies – original pair + baby pair = 2 pairs Month 5: the 3 pairs from month 4, and two new pairs – 5 pairs Month 4: first pair give birth to another pair of babies, pair born in month 3 are now mature – 3 pairs Month 6: the 5 pairs from month 5, and three new pairs – 8 pairs And so on... October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Fibonacci series The nth number in the Fibonacci series, fib(n), is: 0 if n = 0, and 1 if n = 1 fib(n – 1) + fib(n – 2) for any n > 1 e.g. what is fib(23) Easy if we only knew fib(22) and fib(21) The answer is fib(22) + fib(21) What happens if we actually write a function to calculate Fibonacci numbers like this? October 05, 2017 Cinda Heeren / Geoffrey Tien

Calculating the Fibonacci series Let’s write a function just like the formula fib(n) = 0 if n = 0, 1 if n = 1, otherwise fib(n) = fib(n – 1) + fib(n – 2) int fib(int n) { if (n <= 1) return max(0, n); else return fib(n-1) + fib(n-2); } The function calls itself October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Recursive functions The Fibonacci function is recursive A recursive function calls itself Each call to a recursive method results in a separate call to the method, with its own input Recursive functions are just like other functions The invocation (e.g. parameters, etc.) is pushed onto the call stack And removed from the call stack when the end of a method or a return statement is reached Execution returns to the previous method call October 05, 2017 Cinda Heeren / Geoffrey Tien

Recursive function anatomy Recursive functions do not use loops to repeat instructions But use recursive calls, in if statements Recursive functions consist of two or more cases, there must be at least one Base case, and Recursive case October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Recursion cases The base case is a smaller problem with a known solution This problem’s solution must not be recursive Otherwise the function may never terminate There can be more than one base case And base cases may be implicit The recursive case is the same problem with smaller input The recursive case must include a recursive function call There can be more than one recursive case October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Analysis of fib(5) int fib(int n) { if (n <= 1) return max(0, n); else return fib(n-1) + fib(n-2); } 5 fib(5) 2 3 fib(4) fib(3) 1 2 1 1 fib(3) fib(2) fib(2) fib(1) 1 1 1 1 fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 1 Later in the course we will explain how this is an extremely inefficient way to compute the Fibonacci series fib(1) fib(0) October 05, 2017 Cinda Heeren / Geoffrey Tien

Example Base cases Recursive case Recursive linear search Target is found, or the end of the array is reached Recursive case Target not found, search subarray starting from next element // Recursive linear search int recLinSearch(int arr[], int next, int sz, int x) { if (next >= sz) // end of array reached return -1; else if (x == arr[next]) // target found return next; else // not found, search from different starting index return recLinSearch(arr, next + 1, sz, x); } October 05, 2017 Cinda Heeren / Geoffrey Tien

Back on Topic – binary tree traversal A traversal algorithm for a binary tree visits each node in the tree Typically, it will do something while visiting each node! Traversal algorithms are naturally recursive There are three traversal methods inOrder preOrder postOrder October 05, 2017 Cinda Heeren / Geoffrey Tien

inOrder traversal algorithm void inOrder(Node* nd) { if (nd != NULL) inOrder(nd->leftchild); visit(nd); inOrder(nd->rightchild); } The visit function would do whatever the purpose of the traversal is (e.g. print the data value of the node). October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien preOrder Traversal visit(nd); preOrder(nd->leftchild); 1 preOrder(nd->rightchild); 17 2 6 visit visit 13 27 preOrder(left) preOrder(left) preOrder(right) preOrder(right) 3 5 7 8 visit visit visit 9 16 20 39 preOrder(left) preOrder(left) preOrder(left) preOrder(right) preOrder(right) preOrder(right) visit 4 preOrder(left) preOrder(right) 11 visit preOrder(left) preOrder(right) October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien postOrder traversal postOrder(nd->leftchild); postOrder(nd->rightchild); 8 visit(nd); 17 4 7 postOrder(left) postOrder(left) 13 27 postOrder(right) postOrder(right) visit visit 2 3 5 6 postOrder(left) postOrder(left) postOrder(left) 9 16 20 39 postOrder(right) postOrder(right) postOrder(right) visit visit visit postOrder(left) 1 postOrder(right) visit postOrder(left) 11 postOrder(right) visit October 05, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Exercise What will be printed by an in-order traversal of the tree? preOrder? postOrder? inOrder(nd->leftchild); 17 visit(nd); inOrder(nd->rightchild); 9 27 6 16 20 31 12 Food for thought: which traversal will be useful for deep copy? Deep delete? 39 October 05, 2017 Cinda Heeren / Geoffrey Tien

Readings for this lesson Koffman Portions of Chapter 7.1 – 7.3 (for review) Chapter 8.2 – 8.3 (Tree traversals and binary trees) October 05, 2017 Cinda Heeren / Geoffrey Tien