Understanding of Recursive functions

Source: Internet
Author: User
Tags addchild

Objective:

There are many complex algorithms that contain recursive algorithms, especially in the context of tree-shaped data structure traversal, so it is necessary to understand the recursive algorithm correctly in depth.

First, the basic concept of recursive function
Recursive function mechanism Understanding: Call function of static and dynamic mechanism understanding: Call function and called function is the same static code, but the runtime is run by the function of the stack space is independent of the call function stack space, the call point is different, the function state stack address is also different, so the runtime Both the calling function and the called function are completely different on the copy of the code or the copy of the data, only through the return value and the call point .
Recursive invocation form: Direct recursive call f1->f1, indirect recursive f1->f2->f1, in many cases is a direct recursive call.
Recursive function advantages and disadvantages: The recursive function compared to consume the stack resources, the implementation is simple (internal call logic is relatively complex), but the efficiency of the non-recursive version of the efficiency is high.

Second, the recursive function internal invocation logic understanding:
Understanding Level 1:
1. A recursive function requires a termination condition.
2. Recursive function to the termination condition, the stack is returned symmetrically along the route.
Understanding Level 2:
1. The recursive function is the function call and the called Pressure stack relationship, only the found will be recursive call, so recursive traversal is a deep traversal .
2. The termination condition of a recursive function is the case that the recursive function is no longer called at the end of a continuous recursive invocation , and can be an if judgment or a for/while judgment.
3. The general recursion is to traverse or find, if the Find condition (non-terminating condition) in the recursive traversal in front, then do not have all the traversal to find will return, if the search condition (non-terminating condition) after recursive traversal, then need to all traverse to return .


A city simulation example of recursive algorithm for traversing 3ds max export nodes

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 <) {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 (Ten, "Node2"); Maxtreenode node3 ("Node3"); Maxtreenode node4 ("Node4"); Maxtreenode Node5 (10000, "Node5");/*maxtreenode node1 ("Node1"); Maxtreenode Node2 ("Node2"); Maxtreenode node3 ("Node3"); Maxtreenode node4 ("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);p rintf ("------------Testtree.browsef (&node1);-------\ n"); Testtree.browsef (&node1);p rintf ("------------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 (Ten, "Node2"); Maxtreenode node3 ("Node3"); Maxtreenode node4 ("Node4"); Maxtreenode Node5 (10000, "Node5"); */maxtreenode Node1 (10000, "Node1"); Maxtreenode Node2 ("Node2"); Maxtreenode node3 ("Node3"); Maxtreenode node4 ("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);p rintf ("------------Testtree.browsef (&node1);-------\ n"); Testtree.browsef (&node1);p rintf ("------------Testtree.browsel (&node1);-------\ n"); Testtree.browsel (&node1); return 0;}
Output Result:



Understanding of Recursive functions

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.