2013-03-17---Binary tree recursion, non-recursive implementation (with code) depth, number of leaf nodes, row by line print binary tree

Source: Internet
Author: User

Last night did not send the article, a long story ah, yesterday did not know is csdn problem, or my question, I visited a half-day, visit not on the website Ah, later 12 points more, went to sleep. The previous article said that the first order of the two-fork tree, the middle sequence, the post-sequence traversal problem, this time or the simple point, said the calculation of the depth of the binary tree, and the number of leaf nodes

int CEng (Node *proot)//Calculate the number of layers, recursive implementation {int left = 0;int right = 0;int res = 0;if (Proot = NULL) {return 0;} if (proot->pleft! = NULL) {left = CEng (Proot->pleft);} if (proot->pright! = NULL) {right = CEng (proot->pright);} res = left > right? Left:right;return res + 1;}

Class Cengnode{public:int n = 0; Node *p = NULL; Cengnode (int now, Node *p): N (now), p (p) {}};int cengstack (Node *proot)//calculation depth, non-recursive implementation {stack<cengnode> Mystack; Node *pnow = Proot;mystack.push (Cengnode (1, proot)), int res = 0;while (false = = Mystack.empty ()) {Cengnode now = mystack.to P (); Mystack.pop (); if (NOW.N > Res) {res = NOW.N;} if (now.p->pright! = NULL) {Mystack.push (Cengnode (NOW.N + 1, now.p->pright));} if (now.p->pleft! = NULL) {Mystack.push (Cengnode (NOW.N + 1, now.p->pleft));}} return res;}

int getyenum (node *proot)//Compute the number of leaf nodes, recursive implementation {int left = 0;int right = 0;if (Proot = nullptr) {return 0;} if (Proot->pleft = = NULL && Proot->pright = = null) {return 1;} if (proot->pleft! = nullptr) {left = Getyenum (Proot->pleft);} if (proot->pright! = nullptr) {right = Getyenum (proot->pright);} return left + right;}

int getyenumstack (node *proot)//Compute the number of leaf nodes, non-recursive implementation {Stack<node *> mystack; Node *pnow = Null;mystack.push (proot), int res = 0;while (false = = Mystack.empty ()) {Pnow = Mystack.top (); Mystack.pop (); if ( Pnow! = null) {if (Pnow->pleft = = NULL && Pnow->pright = = null) {res + = 1;} Else{if (pnow->pright! = NULL) {Mystack.push (pnow->pright);} if (pnow->pleft! = NULL) {Mystack.push (pnow->pleft);}}} return res;}

void Hengprintqueue (Node *proot)//progressive print, non-recursive, queue implementation {Queue<node *> Myqueue;myqueue.push (proot); Node *pnow = Null;while (false = = Myqueue.empty ()) {Pnow = Myqueue.front (); Myqueue.pop (); if (pnow! = NULL) {cout << pn Ow->data << ""; if (pnow->pleft! = NULL) {Myqueue.push (pnow->pleft);} if (pnow->pright! = NULL) {Myqueue.push (pnow->pright);}}}

void Hengprintstack (Node *proot)//progressive print, non-recursive, stack implementation {Stack<node *> mystack;stack<node *> mystacktemp; Mystack.push (Proot); Node *pnow = Null;bool flag = True;while (false = = Mystacktemp.empty () | | false = = Mystack.empty ()) {if (false = = MYSTACK.E Mpty ()) {Pnow = Mystack.top (); Mystack.pop (); if (pnow! = NULL) {cout << pnow->data << "; Mystacktemp.push ( Pnow->pleft); Mystacktemp.push (pnow->pright);}} Else{while (False = = Mystacktemp.empty ()) {Mystack.push (Mystacktemp.top ()); Mystacktemp.pop ();}}}

Oneself feel these code, also is the binary tree inside relatively simple bar, concrete does not explain, believe everybody still can understand, recursion of good understanding, stack is our manual simulation process of recursion.

Talk about the feelings of these days, agreed to the end of the 15th C + + Ah, today are 17th, in fact, this thing can not be a quick buck ah, really can only slowly, from the beginning of the last month, 18th C + +, unknowingly over 1 months, in fact, now feel C + + language is OK, mainly basic skills, data structure, There are design patterns, because the previous use of other languages to do the project, so in fact, the design pattern is OK, mainly data structure, tree and graph, there are n sorting algorithm, so these days really found more learning things ah, in short, this is to go past, I will try to do before June, time is long enough, specific to see themselves.

Foundation is not strong shake, if want to better offer, good work will strive Ah, come on. Tomorrow also to adjust the study plan, review, we engage, engage in cocos2dx, engage in data structure, design patterns,

I went to brush my teeth and slept for a while.

2013-03-17---Binary tree recursion, non-recursive implementation (with code) depth, number of leaf nodes, row by line print binary tree

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.