Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review: recursion Tree traversals

Similar presentations


Presentation on theme: "Review: recursion Tree traversals"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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


Download ppt "Review: recursion Tree traversals"

Similar presentations


Ads by Google