C ++ implements binary tree pre-order traversal, post-order traversal, middle-order traversal, and sequence traversal (no recursion required)

Source: Internet
Author: User

/* <Br/> complete mytree </P> <p> non-recursive preorder, midorder, aftorder, layerorder <br/> */</P> <p> # include <iostream. h> <br/> # include <assert. h> </P> <p> # define out </P> <p> ///////////////////// //////////////////////////////////////// /// // <br/> // stack //////////////////// /// // <br /> ////////////////////////////////////// /// // <br/> template <class T> Cl Ass stack; <br/> template <class T> <br/> class stacknode <br/> {<br/> T data; <br/> stacknode * pnext; </P> <p> friend class Stack <t>; <br/> }; </P> <p> template <class T> <br/> class Stack <br/> {<br/> PRIVATE: <br/> stacknode <t> * m_lptmp; <br/> stacknode <t> * m_lptop; </P> <p> int m_ncount; </P> <p> Public: <br/> stack () <br/>{< br/> m_ncount = 0; <br/> m_lptop = m_lptmp = NULL; <br/>}</P> <p> PRIVATE: <br/> Stack (const stack <t> &); </P> <p> stack & operator = (const stack <t> &); </P> <p> public: <br/> // clear the stack <br/> bool clearstack (); </P> <p> // number of elements in the stack (0 indicates null) <br/> int getlength () const; </P> <p> // stack entry <br/> bool push (const T &); </P> <p> // output stack <br/> bool POP (out T &); </P> <p> // obtain the top element of the stack <br/> bool gettop (out T &); <br/> }; <br/> //////////////////////////////////// // <br/> // stack member function implementation <br/> // clear the stack <br/> template <CIA Ss t> <br/> bool stack <t>: clearstack () <br/>{< br/> assert (m_ncount> = 0 ); </P> <p> If (m_ncount = 0) <br/> {<br/> return true; <br/>}</P> <p> while (m_ncount> 0) <br/>{< br/> m_lptmp = m_lptop-> pnext; <br/> Delete m_lptop; <br/> m_lptop = m_lptmp-> pnext; <br/>}</P> <p> // If the stack space is released, if the stack structure is correct, true is returned. <br/> If (0 = m_ncount & m_lptop = NULL) <br/>{< br/> return true; <br/>}</P> <p> // if the chain Table Structure exception return false <br/> m_lptop = m_lptmp = NULL; <br/> m_ncount = 0; <br/> return false; <br/>}</P> <p> // return the number of elements in the stack (0 indicates null) <br/> template <class T> <br/> int stack <t>: getlength () const <br/>{< br/> return m_ncount; <br/>}</P> <p> // stack entry <br/> template <class T> <br/> bool stack <t> :: push (const T & Data) <br/>{< br/> m_lptmp = new stacknode <t>; <br/> If (m_lptmp = NULL) <br/>{< br/> return false; <br/>}</P> <P> m_lptmp-> DATA = data; <br/> m_lptmp-> pnext = m_lptop; <br/> m_lptop = m_lptmp; <br/> m_ncount ++; </P> <p> return true; <br/>}</P> <p> // stack output <br/> template <class T> <br/> bool stack <t> :: pop (out T & Data) <br/>{< br/> If (m_ncount = 0) <br/>{< br/> return false; <br/>}</P> <p> m_lptmp = m_lptop-> pnext; <br/> DATA = m_lptop-> data; <br/> Delete m_lptop; <br/> m_lptop = m_lptmp; <br/> m_ncount --; </P> <P> return true; <br/>}</P> <p> // obtain the top element of the stack. <br/> template <class T> <br/> bool stack <t> :: gettop (out T & Data) <br/>{< br/> If (m_ncount = 0) <br/>{< br/> return false; <br/>}</P> <p> DATA = m_lptop-> data; <br/> return false; <br/>}</P> <p> //////////////////////////// //////////////////////////////////////// //// <br/> // queue ///////////////////////////// /// // <br/> //////// ////////// //////////////////////////////////////// /////////// </P> <p> template <class T> class queue; <br/> template <class T> <br/> class quenode <br/> {<br/> T data; <br/> quenode * pnext; </P> <p> friend class queue <t>; <br/> }; </P> <p> template <class T> <br/> class queue <br/>{< br/> PRIVATE: <br/> quenode <t> * m_lpfront; // team header <br/> quenode <t> * m_lptail; // team end <br/> int m_ncount; // count the number of nodes in the queue </P> <p> PRIVATE: <br /> Queue <t> & operator = (queue <t> &); <br/> Queue (queue <t> &); </P> <p> public: <br/> Queue (): m_lpfront (null), m_lptail (null), m_ncount (0) {}< br/> ~ Queue () <br/>{< br/> clearque (); <br/>}</P> <p> public: <br/> // enter the team <br/> bool enque (const T & data ); </P> <p> // team-out <br/> bool exitque (out T & data ); </P> <p> // get queue length <br/> int getlenth () <br/> {<br/> return m_ncount; <br/>}</P> <p> // gets the header node of the team, but does not leave the site <br/> bool getfront (out T & Data) <br/>{< br/> If (m_lpfront! = NULL) <br/>{< br/> DATA = m_lpfront-> data; </P> <p> return true; <br/>}</P> <p> return false; <br/>}</P> <p> // clear the queue <br/> void clearque () <br/> {<br/> // use the team end pointer as a secondary pointer to delete each node at a time <br/> while (m_lpfront) <br/>{< br/> m_lptail = m_lpfront-> pnext; <br/> Delete m_lpfront; <br/> m_lpfront = m_lptail; </P> <p> m_ncount --; <br/>}</P> <p> assert (m_ncount = 0 ); <br/> assert (m_lpfront = NULL & m_lptail = Null); <br/>}< br/> }; </P> <p> ///////////////////////////////// //// <br/> // queue member function implementation <br/> // queue entry <br/> template <typename T> <br/> bool queue <t>:: enque (const T & Data) <br/>{< br/> assert (m_ncount> = 0 ); </P> <p> // if the first node is in the queue, it is both the header node and the End Node. <br/> If (m_ncount = 0) <br/>{< br/> m_lpfront = m_lptail = new quenode <t>; </P> <p> // if the new node memory allocation fails <br/> If (m_lptail = NULL) <br/>{< br/> m_lptail = m_lpfront = NULL; <br/> m_ncount = 0; </P> <p> return false; <br/>}</P> <p> // The first node is successfully queued. <br/> m_lptail-> pnext = NULL; <br/> m_lptail-> DATA = data; </P> <p> m_ncount ++; </P> <p> return true; <br/>}</P> <p> // if it is not the first node to join the queue, insert it to the end of the team <br/> m_lptail-> pnext = new quenode <t>; <br/> If (m_lptail-> pnext = NULL) <br/>{< br/> return false; <br/>}</P> <p> m_lptail = m_lptail-> pnext; </P> <p> m_lptail-> pnext = NULL; <br/> M _ Lptail-> DATA = data; </P> <p> m_ncount ++; </P> <p> return true; <br/>}</P> <p> // departure <br/> template <typename T> <br/> bool queue <t> :: exitque (out T & Data) <br/>{< br/> assert (m_ncount> = 0); </P> <p> If (m_ncount = 0) <br/>{< br/> return false; <br/>}</P> <p> quenode <t> * lptmp = m_lpfront-> pnext; </P> <p> DATA = m_lpfront-> data; <br/> Delete m_lpfront; <br/> m_lpfront = lptmp; </P> <p> m_ncount --; </P> <p> // Be careful when the last node is out! Remember to do the following! <Br/> If (m_ncount = 0) <br/>{< br/> m_lptail = NULL; <br/>}< br/> return true; </P> <p >}</P> <p> ///////////////////////// //////////////////////////////////////// /// // <br/> // Binary Tree /////////////////////////// //// // <br/> /////// //////////////////////////////////////// /// // </P> <p> // Binary Tree node class <br/> template <typename T> <br/> struct treenode <br/> {<br/> T data; <Br/> treenode * lplchild; <br/> treenode * lprchild; </P> <p> int m_nheight; <br/> }; </P> <p> // binary tree class <br/> template <typename T> <br/> class binarytree <br/>{< br/> PRIVATE: <br/> treenode <t> * m_lproot; <br/> int m_nheight; </P> <p> PRIVATE: <br/> binarytree (const binarytree <t> &); <br/> binarytree & operator = (binarytree &); </P> <p> public: <br/> binarytree (): m_lproot (null), m_nheight (0) {}</P> <p> Public: <br // Add a node (the subtree of the lpinsertnode) <br/> treenode <t> * addnode (const T & Data, treenode <t> * lpinsertnode ); </P> <p> // delete a subtree (the default parameter indicates clearing the subtree) <br/> bool delsubtree (const treenode <t> * lpnode = m_lproot ); </P> <p> //... <br/> typedef void (binarytree <t>: * funptr) (treenode <t> * lpcurnode ); </P> <p> // display tree <br/> void printnode (treenode <t> * lpcurnode) <br/> {<br/> cout <"node data:" <lpcurnode-> data <'/t' <"Layer:" <lpcurnode-> m_nheight <Endl; <br/>}</P> <p> //////////////////////////// //////////////////////////////////////// // <br/> // four types of non-Recursive Implementation tree traversal: preorder, midorder, aftorder, layerorder // <br/> ///////////////////////////////// /// // <br/> // first traverse <br/> void preorder (treenode <t> * lpcurnode, funptr = printnode); </P> <p> // sequential traversal <br/> void midorder (treenode <t> * Lpcurnode, funptr = printnode); </P> <p> // subsequent traversal <br/> void aftorder (treenode <t> * lpcurnode, funptr = printnode ); </P> <p> // sequence traversal <br/> void layerorder (treenode <t> * lpcurnode, funptr = printnode); <br/> }; </P> <p> ///////////////////////////////// /// <br/> // implement the binary tree member function </P> <p> // Add a node (as the subtree of the lpinsertnode) <br/> // If the insertion is successful, the pointer to the new node is returned. If the insertion fails, null is returned. <br/> template <typename T> <br/> treenode <t> * binarytre E <t>: addnode (const T & Data, treenode <t> * lpinsertnode) <br/>{< br/> treenode <t> * lptmpnode = NULL; </P> <p> // Insert a new node to the empty tree <br/> If (m_lproot = NULL) <br/> {<br/> // If the insert is valid (that is, insert data at the root node) <br/> If (lpinsertnode = NULL) <br/>{</P> <p> lptmpnode = new treenode <t>; <br/> lptmpnode-> DATA = data; <br/> lptmpnode-> lplchild = NULL; <br/> lptmpnode-> lprchild = NULL; <br/> lptmpnode-> m_nheight = 1; </ P> <p> m_lproot = lptmpnode; <br/> m_nheight = 1; </P> <p> // return the pointer of the newly inserted node <br/> return m_lproot; <br/>}< br/> // If the selected Insertion Location is invalid, null is returned. <br/> else return NULL; <br/>}</P> <p> // determines whether the new node is inserted when the tree is not empty. <br/> // If the selected insert position is valid, the insert value is returned. Otherwise, null is returned and no operation is performed. </P> <p> // If the selected node is empty <br/> else if (lpinsertnode = NULL) <br/>{< br/> return NULL; <br/>}</P> <p> // search for the left subtree of lpinsertnode first, then retrieve the right subtree, and locate the location where new nodes can be inserted. <br/>/ /If the left and right subtree are full, the false value is returned. <br/> else if (lpinsertnode-> lplchild = NULL) <br/>{< br/> lptmpnode = new treenode <t>; <br/> lptmpnode-> DATA = data; <br/> lptmpnode-> lplchild = NULL; <br/> lptmpnode-> lprchild = NULL; <br/> lptmpnode-> m_nheight = lpinsertnode-> m_nheight + 1; </P> <p> lpinsertnode-> lplchild = lptmpnode; <br/>}< br/> else if (lpinsertnode-> lprchild = NULL) <br/> {<br/> lptmpnode = new treenode <T >;< br/> lptmpnode-> DATA = data; <br/> lptmpnode-> lplchild = NULL; <br/> lptmpnode-> lprchild = NULL; <br/> lptmpnode-> m_nheight = lpinsertnode-> m_nheight + 1; </P> <p> lpinsertnode-> lprchild = lptmpnode; <br/>}< br/> // The Insertion Location is invalid (the left and right children of the selected node are not empty, and the insertion operation cannot be completed) <br/> else <br/>{< br/> // insertion failed, return NULL <br/> return NULL; <br/>}</P> <p> // Number of update layers <br/> If (lptmpnode-> m_nheight> This-> m_nheight) <br/> {< Br/> This-> m_nheight = lptmpnode-> m_nheight; <br/>}< br/> // return the pointer of the newly inserted node <br/> return lptmpnode; </P> <p >}</P> <p> // deletes a subtree (the default parameter indicates clearing the subtree) <br/> template <typename T> <br/> bool binarytree <t>: delsubtree (const treenode <t> * lpnode) <br/>{< br/> return false; <br/>}</P> <p> // first traverse <br/> template <typename T> <br/> void binarytree <t> :: preorder (treenode <t> * lpcurnode, funptr fun) <br/>{< br/> If (fun = NULL) <Br/>{< br/> return; <br/>}</P> <p> stack <treenode <t> *> S; </P> <p> If (lpcurnode = NULL) <br/>{< br/> return; <br/>}</P> <p> DO <br/> {<br/> // access the root node <br/> (this-> * Fun) (lpcurnode); </P> <p> // right subtree into the stack <br/> If (lpcurnode-> lprchild! = NULL) <br/>{< br/> S. push (maid); <br/>}</P> <p> // left subtree into Stack <br/> If (maid-> lplchild! = NULL) <br/>{< br/> S. push (lpcurnode-> lplchild); <br/>}</P> <p >}while (S. pop (lpcurnode )); <br/>}</P> <p> // median traversal <br/> template <typename T> <br/> void binarytree <t> :: midorder (treenode <t> * lpcurnode, funptr fun) <br/>{< br/> If (fun = NULL) <br/>{< br/> return; <br/>}</P> <p> stack <treenode <t> *> stack; </P> <p> while (true) <br/> {<br/> // keep walking to the leaf node in the lower left corner <br/> while (lpcurnode! = NULL) <br/>{< br/> stack. push (lpcurnode); <br/> maid; <br/>}</P> <p>/out stack, solve the problem <br/> If (stack. pop (lpcurnode) = false) <br/>{< br/> // If the stack is empty, the problem is completely solved. <br/> return; <br/>}</P> <p> (this-> * Fun) (lpcurnode); <br/> maid-> lprchild; <br/>}</P> <p >}< br/> // subsequent traversal <br/> template <typename T> <br/> void binarytree <t>:: aftorder (treenode <t> * lpcurnode, funptr Fun) <br/>{< br/> If (fun = NULL) <br/>{< br/> return; <br/>}</P> <p> stack <treenode <t> *> stack; <br/> treenode <t> * lpflagnode = NULL; </P> <p> while (true) <br/>{< br/> while (lpcurnode! = NULL) <br/>{< br/> stack. push (lpcurnode); <br/> maid; <br/>}</P> <p>/out stack, solve the problem <br/> If (stack. pop (lpcurnode) = false) <br/>{< br/> // If the stack is empty, the problem is completely solved. <br/> return; <br/>}</P> <p> If (lpcurnode-> lprchild = lpflagnode <br/> | lpcurnode-> lprchild = NULL) <br/>{< br/> (this-> * Fun) (lpcurnode); <br/> lpflagnode = lpcurnode; </P> <p> lpcurnode = NULL; <br/>}< br/> el Se <br/> {<br/> stack. push (lpcurnode); <br/> maid-> lprchild; <br/>}</P> <p> // sequence traversal <br/> template <typename T> <br /> void binarytree <t>:: layerorder (treenode <t> * lpcurnode, funptr fun) <br/>{< br/> If (fun = NULL) <br/>{< br/> return; <br/>}</P> <p> queue <treenode <t> *> que; <br/> que. enque (lpcurnode); <br/> while (que. exitque (lpcurnode) <br/>{< br/> (this-> * Fun) (maid); <br/> If (maid-> lplchild! = NULL) <br/>{< br/> que. enque (lpcurnode-> lplchild); <br/>}</P> <p> If (lpcurnode-> lprchild! = NULL) <br/>{< br/> que. enque (lpcurnode-> lprchild ); <br/>}</P> <p> /////////////// //////////////////////////////////////// /// // <br/> // test ////////////// //////////////////////////////////////// ///// <br/> ////////////////////////////// //////////////////////////////////////// //// </P> <p> void main () <br/>{< br/> binarytree <int> Bt; </P> <p> // insert data into the tree type <br/> // 1 <br/> // 2 5 <br/> // 3 4 6 <br/> treenode <int> * tmp1 = NULL, * tmp2 = NULL; </P> <p> tmp1 = BT. addnode (1, null); </P> <p> tmp2 = BT. addnode (2, tmp1); <br/> BT. addnode (3, tmp2); <br/> BT. addnode (4, tmp2); </P> <p> tmp2 = BT. addnode (5, tmp1); <br/> BT. addnode (6, tmp2 ); </P> <p> cout <">>>>>>>>>>>>>>>>>>>>>>>" <Endl; <br/> cout <"sequential traversal:" <Endl; <br/> BT. preorder (tmp1 ); </P> <p> cout <">>>>>>>>>>>>>>>>>>>>>>>" <Endl; <br/> cout <"sequential traversal:" <Endl; <br/> BT. midorder (tmp1 ); </P> <p> cout <">>>>>>>>>>>>>>>>>>>>>>>" <Endl; <br/> cout <"sequential traversal:" <Endl; <br/> BT. aftorder (tmp1 ); </P> <p> cout <">>>>>>>>>>>>>>>>>>>>>>>" <Endl; <br/> cout <"sequence traversal:" <Endl; <br/> BT. layerorder (tmp1); </P> <p >}< br/>

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.