Stack needs to be used in the middle-order traversal of the tree, and queue needs to be used in the hierarchy traversal. below is the structure of the tree:
Code implementation:
# Include <stdio. h> # include <stdlib. h> // treenode /////////////////////////////////// /// // typedef struct treenode {char m_cval; treenode * m_pleft; treenode * m_pright; treenode (char cval );~ Treenode () ;}; treenode: treenode (char cval) {m_cval = cval; m_pleft = 0; m_pright = 0;} treenode ::~ Treenode () {} // stack /////////////////////////////////// /// // class stack {public: stack (INT iamount = 10 );~ Stack (); // return 1 means succeeded, 0 means failed. int POP (treenode * & pval); int push (treenode * pval); int top (treenode * & pval); // 1 means not null, 0 means null. int notnull (); Private: treenode ** m_ppdata; int m_icount; int m_iamount;}; Stack: Stack (INT iamount) {m_ppdata = new treenode * [iamount]; m_icount = 0; m_iamount = iamount;} Stack ::~ Stack () {Delete m_ppdata;} int Stack: Pop (treenode * & pval) {If (m_icount> 0) {-- m_icount; pval = m_ppdata [m_icount]; return 1;} return 0;} int Stack: Push (treenode * pval) {If (m_icount <m_iamount) {m_ppdata [m_icount] = pval; ++ m_icount; return 1;} return 0;} int Stack: Top (treenode * & pval) {If (m_icount> 0 & m_icount <= m_iamount) {pval = m_ppdata [m_iCount-1]; return 1;} return 0;} int Stack: notnull (){ If (m_icount! = 0) return 1; return 0 ;} // queue ///////////////////////////////////// /// ///class queue {public: queue (INT nmounts );~ Queue (); // operator int enqueue (treenode * node); int dequeue (treenode * & node); int isfull (); int isempty (); Private: treenode ** Q; int front; int rear; int totalnode;}; queue: Queue (INT nmounts = 10) {q = new treenode * [nmounts]; totalnode = nmounts; rear = 0; front = 0;} queue ::~ Queue () {Delete Q;} int queue: enqueue (treenode * node) {If (! Isfull () {q [rear] = node; Rear = (Rear + 1) % totalnode;} else {// printf ("queue is full! \ N "); Return 0;} return 1;} int queue: dequeue (treenode * & node) {If (! Isempty () {node = Q [Front]; front = (front + 1) % totalnode;} else {// printf ("queue is empty! \ N "); Return 0;} return 1;} int queue: isfull () {If (Rear + 1) % totalnode = front) {return 1 ;} return 0;} int queue: isempty () {If (Rear = front) {return 1;} return 0;} int main (INT argc, char * argv []) {treenode Na ('A'); treenode Nb ('B'); treenode NC ('C'); treenode Nd ('D '); treenode NE ('E'); treenode NF ('F'); treenode Ng ('G'); treenode NH ('H'); treenode Ni ('I '); treenode nJ ('J'); treenode NK ('k'); treenode NL ('l'); NA. m_pleft = & nb; NA. m_pright = & NC; Nb. m_pright = & Nd; nd. m_pright = & ng; NC. m_pleft = & ne; NC. m_pright = & NF; NF. m_pright = & nH; NH. m_pleft = & Ni; NH. m_pright = & NJ; Ni. m_pleft = & NK; Ni. m_pright = & nL; stack st; // traverse treenode * pval = & NA; int ipopped = 0; while (pval! = 0) {If (pval-> m_pleft! = 0 & ipopped = 0) {st. Push (pval); pval = pval-> m_pleft; ipopped = 0;} else if (pval-> m_pright! = 0) {printf ("% C", pval-> m_cval); pval = pval-> m_pright; ipopped = 0;} else {printf ("% C ", pval-> m_cval); If (0 = ST. pop (pval) break; ipopped = 1 ;}} printf ("\ n"); // level traversal pval = & NA; queue; while (pval! = NULL) {If (pval-> m_pleft! = NULL & pval-> m_pright! = NULL) {queue. enqueue (pval-> m_pleft); queue. enqueue (pval-> m_pright);} else if (pval-> m_pleft! = NULL) {queue. enqueue (pval-> m_pleft);} else if (pval-> m_pright! = NULL) {queue. enqueue (pval-> m_pright);} printf ("% C", pval-> m_cval); If (0 = queue. dequeue (pval) {break ;}} printf ("\ n"); Return 0 ;}