Understanding of recursive functions and understanding of recursive functions

Source: Internet
Author: User

Understanding of recursive functions and understanding of recursive functions

Preface:

Many complex algorithms include recursive algorithms, especially for tree-like Data Structure traversal. Therefore, it is necessary to have a correct and in-depth understanding of recursive algorithms.

I. Basic concepts of recursive functions
Recursive Function Mechanism understanding: static and dynamic mechanisms for calling a function understanding: Although calling a function and called function are the same static code, however, the stack space operated by the function is independent from the stack space of the function to be called. The function status stack address varies depending on the call points, therefore, the calling and called functions at runtime are completely different in code copies and data copies. Only the returned values can be associated with the call point.
Recursive call method: directly recursively calls F1-> F1, and indirectly recursion F1-> F2-> F1. In many cases, it is a direct recursive call.
Advantages and disadvantages of recursive functions: recursive functions consume stack resources and are easy to implement (the internal call logic is complicated), but the efficiency is not as high as that of non-recursive functions.

Ii. Understanding of Internal call logic of recursive functions:
Level 1:
1. recursive functions require a termination condition.
2. recursive functions return symmetric results along the outbound stack to the termination condition.
Level 2:
1. recursive functions are the relationship between function calls and called pressure stacks. Only the called functions are called recursively, So recursive traversal is a deep traversal.
2. The termination condition of a recursive function is that the recursive function is no longer called at the end of a continuous recursive call. The condition can be if or for/while.
3. generally, recursion is used to traverse or search. If the search condition (non-termination condition) is before recursive traversal, it will be returned if not all traversal conditions are found. If the search condition (non-termination condition) after recursive traversal, all traversal is required to return the result.


3. A simulation example of a recursive algorithm that traverses the 3ds Max export Node

RecursiveFunc. h file:

#ifndef RECURSIVEFUNC_H_#define RECURSIVEFUNC_H_#include <string>#include <list>using namespace std;typedef struct tagMaxTreeNode{int m_nIndex;string m_strName;tagMaxTreeNode(int nIndex, const string& strName){m_nIndex =  nIndex;m_strName = strName;}void AddChild(tagMaxTreeNode* pChild){m_listChild.push_back(pChild);}list<tagMaxTreeNode*> m_listChild;}MaxTreeNode;template <class T>class MaxTree{public:MaxTree(){m_root = NULL;}~MaxTree(){}void AddNode(T *pParent, T *pChild){if( pParent == NULL ){m_root = pParent;return;}else{pParent->AddChild(pChild);}}void BrowseF( T* pNode){printf("BrowseF:Recursive Call nIndex: %d\n", pNode->m_nIndex);if( pNode->m_nIndex < 10 ){printf("BrowseF:Condition Hit nIndex: %d\n", pNode->m_nIndex);return;}list<T*>::iterator itrChild = pNode->m_listChild.begin();for(; itrChild != pNode->m_listChild.end(); ++itrChild){BrowseF( *itrChild);}}void BrowseL( T* pNode){printf("BrowseF:Recursive Call nIndex: %d\n", pNode->m_nIndex);list<T*>::iterator itrChild = pNode->m_listChild.begin();for(; itrChild != pNode->m_listChild.end(); ++itrChild){BrowseF( *itrChild);}if( pNode->m_nIndex < 10 ){printf("BrowseF:Condition Hit nIndex: %d\n", pNode->m_nIndex);return;}}private:  T* m_root;};#endif

Main1.cpp file:

#include "stdafx.h"#include "RecursiveFunc.h"int _tmain(int argc, _TCHAR* argv[]){MaxTreeNode node1(1, "node1");MaxTreeNode node2(10, "node2");MaxTreeNode node3(100, "node3");MaxTreeNode node4(1000, "node4");MaxTreeNode node5(10000, "node5");/*MaxTreeNode node1(1000, "node1");MaxTreeNode node2(1000, "node2");MaxTreeNode node3(100, "node3");MaxTreeNode node4(100, "node4");MaxTreeNode node5(1, "node5");*/MaxTree<MaxTreeNode> TestTree;TestTree.AddNode(NULL, &node1);TestTree.AddNode(&node1, &node2);TestTree.AddNode(&node1, &node3);TestTree.AddNode(&node2, &node4);TestTree.AddNode(&node3, &node5);printf("------------TestTree.BrowseF(&node1);-------\n");TestTree.BrowseF(&node1);printf("------------TestTree.BrowseL(&node1);-------\n");TestTree.BrowseL(&node1);return 0;}

Output result:

Main2.cpp:

#include "stdafx.h"#include "RecursiveFunc.h"int _tmain(int argc, _TCHAR* argv[]){/*MaxTreeNode node1(1, "node1");MaxTreeNode node2(10, "node2");MaxTreeNode node3(100, "node3");MaxTreeNode node4(1000, "node4");MaxTreeNode node5(10000, "node5");*/MaxTreeNode node1(10000, "node1");MaxTreeNode node2(1000, "node2");MaxTreeNode node3(100, "node3");MaxTreeNode node4(100, "node4");MaxTreeNode node5(1, "node5");MaxTree<MaxTreeNode> TestTree;TestTree.AddNode(NULL, &node1);TestTree.AddNode(&node1, &node2);TestTree.AddNode(&node1, &node3);TestTree.AddNode(&node2, &node4);TestTree.AddNode(&node3, &node5);printf("------------TestTree.BrowseF(&node1);-------\n");TestTree.BrowseF(&node1);printf("------------TestTree.BrowseL(&node1);-------\n");TestTree.BrowseL(&node1);return 0;}
Output result:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.