Recursive transfer Stack
Use the stack to implement recursion . cpp
#include <stack> #include <iostream>using namespace std;int printn (int n) {if (n>0) {cout << N;return PRINTN (n-1);}} void Printns_shunxu (int n) {stack<int> mystack; Aaa:if (n > 0) {mystack.push (n); while (!mystack.empty ()) {cout << mystack.top (); Mystack.pop ();} n-= 1; goto AAA;}} void Printns_nixu (int n) {stack<int> mystack; Aaa:if (n > 0) {mystack.push (n); n-= 1;goto AAA;} while (!mystack.empty ()) {cout << mystack.top (); Mystack.pop ();}} int get100 (int i) {if (!i) {return 0;} else{return get100 (i-1) + I;}} int getn (int i) {stack<int> mystack;int res = 0; Aa:if (i) {mystack.push (i); I--;goto AA;} while (!mystack.empty ()) {res + = Mystack.top (); Mystack.pop ();} return res;} void to2 (int num) {if (num) {cout << num% 2;to2 (NUM/2);}} void MainA () {//cout << get100 (+) << Endl;printns_nixu (9); Printns_shunxu (9);cout<< "\ n" <<getn << "\ n"; to2; Cin.get ();}
Double-layer recursive transfer stack . cpp
#include <iostream> #include <stack>using namespace std;int getf (int n) {if (N==1 | | n==2) {return 1;} Else{return getf (n-1) + GETF (n-2);}} int GETFF (int n) {int *p = new Int[n];p [0] = p[1] = 1;for (int i = 2; i < n;i++) {P[i] = P[i-1] + p[i-2];} return p[n-1];} int getfff (int n) {stack<int> mystack;int F1, f2, f3;f1 = F2 = 1;int i = 2; Abc:if (i<n) {Mystack.push (F1); Mystack.push (f2); F3 = 0;while (!mystack.empty ()) {f3+= mystack.top (); Mystack.pop ();} F3 = f2 + f1;f1 = f2;//rotation F2 = F3;i++;goto ABC;} Return F3;} int getffff (int n) {int f1, f2, f3;f1 = F2 = 1;for (int i = 2; i < n; i++) {F3 = F2 + f1;f1 = f2;//rotation F2 = f3;} Return F3;} void Mainfg () {cout << getf (Ten) << endl;cout << GETFF (Ten) << endl;cout << getfff (Ten) << Endl;cin.get ();}
Stack emulation recursive function call . cpp
#include <iostream> #include <stack>//recursion, order, reverse, stack eat a spit one, order, once eat and spit, reverse//recursion, Data retention intermediate results, function pointer retention operation//Hanoi, Fibonacci sequence recursive computation expressions , Stacks, (Xiong, 3 people do a problem)//Tan Sheng, Hanoi, Li Guirong, Fibonacci, Liu Yimin using namespace std;struct datas{int n;void (*p) (int);}; void Printn (int n) {if (n > 0) {cout << n;return printn (n-1);}} void print (int n) {cout << n;} 1+100void printall (int n) {stack<datas> mystack; Aaa:if (n > 0) {datas S1;S1.N = N;S1.P = Print;mystack.push (S1); while (!mystack.empty ()) {Datas stemp = Mystack.top (); Ste MP.P (STEMP.N); Mystack.pop ();} N-= 1;goto AAA;}} void Main () {printall (ten); Cin.get ();}
Two fork TreeImplement
#include <iostream> #include <string> #include <stack>using namespace Std;struct mystruct{int nodedata=0; MyStruct *pleft=nullptr; MyStruct *pright = nullptr;} btree,*pbtree;//, pre-order, sequential, recursive traversal, non-recursive traversal//lookup, modify, delete, insert, sort mystruct * insertnode (mystruct *proot,int num) {if (proot== nullptr) {MyStruct *pnew = new Mystruct;pnew->nodedata = Num;proot = pnew;} else if (num <= proot->nodedata) {PR Oot->pleft = Insertnode (proot->pleft, num);} else {proot->pright = Insertnode (proot->pright, num);} return proot;} int Findmax (mystruct *proot) {int max =-99999; MyStruct * Pcurr = proot;//Record root node mystruct * mystack[100];//pointer data int top = 0;while (top! = 0 | | Pcurr! = nullptr) {while (pcur R! = nullptr) {mystack[top++] = Pcurr;pcurr = Pcurr->pleft;} if (Top > 0) {top--;p Curr = mystack[top];///cout << "" << pcurr->nodedata << endl;if (max< PCU Rr->nodedata) {max = Pcurr->nodedata;} Pcurr = Pcurr->pright;}} return Max;} void Zhong (MyStruct *proot) {if (proot!=NULLPTR) {if (proot->pleft!=nullptr) {zhong (proot->pleft);} cout << "" << proot->nodedata << endl;if (proot->pright!= nullptr) {zhong (proot->pright);}}} void Stackzhong (MyStruct *proot) {//stack<mystruct> mystack; MyStruct * Pcurr = proot;//Record root node mystruct * mystack[100];//pointer data int top = 0; while (top!=0 | | Pcurr!=nullptr) {while (Pcurr! = nullptr) {mystack[top++] = Pcurr; Pcurr = pcurr->pleft;} if (top >0) {top--; pcurr = Mystack[top]; cout << "" << pcurr->nodedata << Endl; Pcurr = pcurr->pright; }}}void Stackzhonga (mystruct *proot) {//stack<mystruct> mystack; MyStruct * Pcurr = proot;//record root node stack<mystruct *> mystack;while (!mystack.empty () | | Pcurr! = nullptr) {while (Pcurr ! = nullptr) {Mystack.push (pcurr);p curr = Pcurr->pleft;} if (!mystack.empty ()) {Pcurr = Mystack.top (); cout << "" << pcurr->nodedata << Endl;mystack.pop ();p C Urr = Pcurr->pright;}}} void Show (MyStruct *proot, INT N) {if (proot==nullptr) {return;} else{show (Proot->pleft, n + 1); for (int i = 0; i < n;i++) {cout << "";} cout << proot->nodedata << endl;show (proot->pright, n + 1);}} int Getyenum (mystruct *proot)//leaf node {int left = 0;int right = 0;if (proot==nullptr) {return 0;} if (proot->pleft==nullptr && proot->pright==nullptr) {return 1;} left = Getyenum (Proot->pleft); right = Getyenum (Proot->pright), return left + right;} int getheight (mystruct *proot) {int height = 0;int left = 0;int right = 0;if (Proot = = nullptr) {return 0;} left = GetHeight (proot->pleft), right = GetHeight (proot->pright), height = left > right? Left:right;return height + 1;} void CEng (MyStruct *proot) {if (Proot ==nullptr) {return;} MyStruct * Myq[100];int tou = 0;int wei = 0; MyStruct * Pcurr = nullptr;myq[wei++] = proot;//into queue first node, queued while (tou!=wei) {Pcurr = myq[tou];tou++;//outbound cout << PCU Rr->nodedata << endl;if (pcurr->pleft!=nullptr) {myq[wei++] = pcurr->pleft;//enqueued}if (pcurr->pright! = nullptr) {myq[wei++] = pcurr->pright;//queue}}}void MainA () {mystruct *proot;// Root MyStruct sarray[100];p root = sarray;//0 1 2 3 4--99//for (int i = 1; I <= 100;i++) {sarray[i]. Nodedata = i;} 2 * i + 2<<99for (int i = 0; I <= 50;i++) {if (i<= (99-1)/2) {sarray[i].pleft = &sarray[2 * i + 1];} if (i<= (99-2)/2) {sarray[i].pright = &sarray[2 * i + 2];}} Show (Proot, 1); Cin.get ();} int GETBA (mystruct *proot,int num) {if (proot==nullptr) {return 0;} if (proot->pleft!=nullptr && proot->pleft->nodedata==num) {return proot->nodedata;} if (proot->pright! = nullptr && proot->pright->nodedata = = num) {return proot->nodedata;} GETBA (Proot->pleft, num); GETBA (proot->pright, num);} int GetLeft (mystruct *proot, int num) {if (Proot = = nullptr) {return 0;} if (proot->pright && proot->pright->nodedata = num) {if (proot->pleft) {return proot->pleft- >nodedata;}} GetLeft (Proot->pleft, Num); GetLeft (proot->pright, num);} void main213213 () {mystruct *proot;//root mystruct s1; MyStruct S2; MyStruct S3; MyStruct S4; MyStruct S5; MyStruct S6; MyStruct S7; MyStruct s8;proot = &s1;s1. Nodedata = 1;s2. Nodedata = 2;s3. Nodedata = 3;s4. Nodedata = 4;s5. Nodedata = 5;s6. Nodedata = 16;s7. Nodedata = 7;s8. Nodedata = 8;s1.pleft = &s2;s1.pright = &s3;s2.pleft = &s4;s2.pright = &s5;s3.pleft = &s6;s3.pRight = &s7;cout << Findmax (proot) << endl;cin.get ();} void Mainasds () {mystruct *proot;//root mystruct s1; MyStruct S2; MyStruct S3; MyStruct S4; MyStruct S5; MyStruct S6; MyStruct S7; MyStruct s8;proot = &s1;s1. Nodedata = 1;s2. Nodedata = 2;s3. Nodedata = 3;s4. Nodedata = 4;s5. Nodedata = 5;s6. Nodedata = 6;s7. Nodedata = 7;s8. Nodedata = 8;s1.pleft = &s2;s1.pright = &s3;s2.pleft = &s4;s2.pright = &s5;s3.pleft = &s6;s3.pRight = &s7;//s4.pleft = &s8;ceng (proot); cout << "\n\n\n\n\n\n\n"; cout << getyenum (proot) << "\n\n\n "; cout <<GetHeight (proot) << "\n\n\n";//show (Proot, 1); Zhong (proot); cout << "\n\n\n\n"; Stackzhong (proot); cout << "\n\n\n\n"; Stackzhonga (Proot); Cin.get ();} void Main () {MyStruct *proot=nullptr;//root for (int i = 6; i <; i++) {proot = Insertnode (Proot, i);} for (int i = 5; I >=0; i--) {proot = Insertnode (Proot, i);} Show (Proot, 1); Cin.get ();}
"C + + institute" 0906-recursive/two-tree implementation