SlideShare a Scribd company logo
Class No.14  Data Structures http://ecomputernotes.com
Recursive Call Recall that a stack is used during function calls. The caller function places the arguments on the stack and passes control to the called function. Local variables are allocated storage on  the call stack. Calling a function itself makes no difference as far as the call stack is concerned.  http://ecomputernotes.com
Stack Layout during a call Here is stack layout when function F calls function F (recursively): During execution of F After call At point of call http://ecomputernotes.com Parameters(F) Local variables(F) Return address(F) Parameters(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) sp sp sp
Recursion: preorder preorder(14) 14 ..preorder(4) 4 ....preorder(3) 3 ......preorder(null) ......preorder(null) ....preorder(9) 9 ......preorder(7) 7 ........preorder(5) 5 ..........preorder(null) ..........preorder(null) ........preorder(null) ......preorder(null) http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
Recursion: preorder ..preorder(15) 15 ....preorder(null) ....preorder(18) 18 ......preorder(16) 16 ........preorder(null) ........preorder(17) 17 ..........preorder(null) ..........preorder(null) ......preorder(20) 20 ........preorder(null) ........preorder(null) http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
Recursion: inorder inorder(14) ..inorder(4) ....inorder(3) ......inorder(null) 3 ......inorder(null) 4 ....inorder(9) ......inorder(7) ........inorder(5) ..........inorder(null) 5 ..........inorder(null) 7 ........inorder(null) 9 ......inorder(null) 14 http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
Recursion: inorder ..inorder(15) ....inorder(null) 15 ....inorder(18) ......inorder(16) ........inorder(null) 16 ........inorder(17) ..........inorder(null) 17 ..........inorder(null) 18 ......inorder(20) ........inorder(null) 20 ........inorder(null) http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
Non Recursive Traversal We can implement  non-recursive versions of the preorder, inorder and postorder traversal by using an explicit stack. The stack will be used to store the tree nodes in the appropriate order. Here, for example, is the routine for inorder traversal that uses a stack. http://ecomputernotes.com
Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack;  TreeNode<int>* p; p = root; do { while( p != NULL ) { stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty  http://ecomputernotes.com 
Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack;  TreeNode<int>* p; p = root; do { while( p != NULL ) { stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty  http://ecomputernotes.com 
Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack;  TreeNode<int>* p; p = root; do { while( p != NULL ) { stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty  http://ecomputernotes.com 
Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;;   // go back & traverse right subtree p = p->getRight();  } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;;   // go back & traverse right subtree p = p->getRight();  } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;;   // go back & traverse right subtree p = p->getRight();  } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;;   // go back & traverse right subtree p = p->getRight();  } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
Nonrecursive Inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left-to-right order. http://ecomputernotes.com
Level-order Traversal Level-order:  14  4  15  3  9  18  7  16  20  5  17 http://ecomputernotes.com 14 4 9 7 3 5 15 18 16 20 17

More Related Content

PPT
computer notes - Data Structures - 14
PDF
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
PPT
Computer notes - Binary Search Tree with Strings
PPTX
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
PDF
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
PDF
Bytes in the Machine: Inside the CPython interpreter
PDF
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
computer notes - Data Structures - 14
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
Computer notes - Binary Search Tree with Strings
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
Bytes in the Machine: Inside the CPython interpreter
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython

What's hot (16)

PDF
Diving into byte code optimization in python
PPTX
Function therory
PDF
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
DOCX
as400 built in function- %REPLACE
PDF
Pseudo dynamic immutable records in C++
PPTX
Reverse Engineering: C++ "for" operator
PDF
TDOH 南區 WorkShop 2016 Reversing on Windows
PPTX
Qt Translations
PDF
Rust LDN 24 7 19 Oxidising the Command Line
PPT
커널코드분석 20140621(head.s restart)
ODP
CompilersAndLibraries
PPTX
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
ODP
HailDB: A NoSQL API Direct to InnoDB
PDF
Stack queue
PDF
PPT
computer notes - Data Structures - 8
Diving into byte code optimization in python
Function therory
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
as400 built in function- %REPLACE
Pseudo dynamic immutable records in C++
Reverse Engineering: C++ "for" operator
TDOH 南區 WorkShop 2016 Reversing on Windows
Qt Translations
Rust LDN 24 7 19 Oxidising the Command Line
커널코드분석 20140621(head.s restart)
CompilersAndLibraries
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
HailDB: A NoSQL API Direct to InnoDB
Stack queue
computer notes - Data Structures - 8
Ad

Similar to Computer notes - Recursive (20)

PPT
BST.ppt
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 13
PPTX
Tree traversal techniques
PPT
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PPTX
Graphtraversals Data Structures and its types. Different types of graphs are ...
PDF
Please write the C++ code that would display the exact same output a.pdf
PDF
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
PPTX
unit 4 for trees data structure notes it is
PDF
1. Add a breadth-first (level-order) traversal function to the binar.pdf
PPT
PDF
Tree Traversals A tree traversal is the process of visiting.pdf
PPT
Data Structure and Algorithms Binary Tree
DOCX
#include iostream using namespace std; const int nil = 0; cl.docx
PDF
Tree and binary tree
PDF
LEC 6-DS ALGO(updated).pdf
PDF
Tree Data Structure by Daniyal Khan
PPTX
Binary tree
BST.ppt
computer notes - Data Structures - 15
computer notes - Data Structures - 13
Tree traversal techniques
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Graphtraversals Data Structures and its types. Different types of graphs are ...
Please write the C++ code that would display the exact same output a.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
unit 4 for trees data structure notes it is
1. Add a breadth-first (level-order) traversal function to the binar.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
Data Structure and Algorithms Binary Tree
#include iostream using namespace std; const int nil = 0; cl.docx
Tree and binary tree
LEC 6-DS ALGO(updated).pdf
Tree Data Structure by Daniyal Khan
Binary tree
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 4
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
computer notes - Data Structures - 30
computer notes - Data Structures - 39
computer notes - Data Structures - 11
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 31
computer notes - Data Structures - 4
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PPTX
Cell Types and Its function , kingdom of life
PPTX
master seminar digital applications in india
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
GDM (1) (1).pptx small presentation for students
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Supply Chain Operations Speaking Notes -ICLT Program
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH
Anesthesia in Laparoscopic Surgery in India
TR - Agricultural Crops Production NC III.pdf
PPH.pptx obstetrics and gynecology in nursing
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
Cell Types and Its function , kingdom of life
master seminar digital applications in india
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx

Computer notes - Recursive

  • 1. Class No.14 Data Structures http://ecomputernotes.com
  • 2. Recursive Call Recall that a stack is used during function calls. The caller function places the arguments on the stack and passes control to the called function. Local variables are allocated storage on the call stack. Calling a function itself makes no difference as far as the call stack is concerned. http://ecomputernotes.com
  • 3. Stack Layout during a call Here is stack layout when function F calls function F (recursively): During execution of F After call At point of call http://ecomputernotes.com Parameters(F) Local variables(F) Return address(F) Parameters(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) sp sp sp
  • 4. Recursion: preorder preorder(14) 14 ..preorder(4) 4 ....preorder(3) 3 ......preorder(null) ......preorder(null) ....preorder(9) 9 ......preorder(7) 7 ........preorder(5) 5 ..........preorder(null) ..........preorder(null) ........preorder(null) ......preorder(null) http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
  • 5. Recursion: preorder ..preorder(15) 15 ....preorder(null) ....preorder(18) 18 ......preorder(16) 16 ........preorder(null) ........preorder(17) 17 ..........preorder(null) ..........preorder(null) ......preorder(20) 20 ........preorder(null) ........preorder(null) http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
  • 6. Recursion: inorder inorder(14) ..inorder(4) ....inorder(3) ......inorder(null) 3 ......inorder(null) 4 ....inorder(9) ......inorder(7) ........inorder(5) ..........inorder(null) 5 ..........inorder(null) 7 ........inorder(null) 9 ......inorder(null) 14 http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
  • 7. Recursion: inorder ..inorder(15) ....inorder(null) 15 ....inorder(18) ......inorder(16) ........inorder(null) 16 ........inorder(17) ..........inorder(null) 17 ..........inorder(null) 18 ......inorder(20) ........inorder(null) 20 ........inorder(null) http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
  • 8. Non Recursive Traversal We can implement non-recursive versions of the preorder, inorder and postorder traversal by using an explicit stack. The stack will be used to store the tree nodes in the appropriate order. Here, for example, is the routine for inorder traversal that uses a stack. http://ecomputernotes.com
  • 9. Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack; TreeNode<int>* p; p = root; do { while( p != NULL ) { stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty http://ecomputernotes.com 
  • 10. Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack; TreeNode<int>* p; p = root; do { while( p != NULL ) { stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty http://ecomputernotes.com 
  • 11. Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack; TreeNode<int>* p; p = root; do { while( p != NULL ) { stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty http://ecomputernotes.com 
  • 12. Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
  • 13. Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
  • 14. Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
  • 15. Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << &quot; &quot;; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL ); } http://ecomputernotes.com 
  • 16. Nonrecursive Inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 14 4 9 7 3 5 15 16 17 18 20
  • 17. Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
  • 18. Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
  • 19. Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
  • 20. Traversal Trace recursive inorder inorder(14) ..inorder(4) ....inorder(3) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 http://ecomputernotes.com 
  • 21. Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left-to-right order. http://ecomputernotes.com
  • 22. Level-order Traversal Level-order: 14 4 15 3 9 18 7 16 20 5 17 http://ecomputernotes.com 14 4 9 7 3 5 15 18 16 20 17

Editor's Notes

  • #4: End of lecture 13.
  • #23: End of lecture 14