Binary Tree Code (more comprehensive)

Source: Internet
Author: User
# Include <iostream> # include <vector> # include <list> using namespace STD; // node struct typedef struct node {int data; node * leftchild; node * rightchild; boolleftvisited; boolrightvisited; node () {int DATA =-1; leftchild = NULL; rightchild = NULL; leftvisited = false; rightvisited = false;} node, * pnode; //************************************** **************************************** // Name: createchild // Desc: Create a subtree //******* **************************************** * ***************************** Void createchild (node * & root, vector <int >:: iterator & beginiter, vector <int >:: iterator & enditer) {If (beginiter! = Enditer) {int tempdata = * beginiter ++; If (tempdata! =-1) {root = new node; root-> DATA = tempdata; createchild (root-> leftchild, beginiter, enditer ); // create the left subtree createchild (root-> rightchild, beginiter, enditer); // create the right subtree} else {root = NULL ;}} else {root = NULL ;}} //************************************** **************************************** // Name: createtree // DESC) //************************************** ***************************** * ********** Node * createtree (node * root, vector <int> & datavec) {If (datavec. size () <1) return NULL; vector <int>: iterator beginiter = datavec. begin (); vector <int >:: iterator enditer = datavec. end (); root = NULL; createchild (root, beginiter, enditer); Return root ;} //************************************** **************************************** // Name: displaytree // Desc: Binary display //******************************* **************************************** * ****** Void displaytree (node * root) {If (root! = NULL) {cout <"node:" <root-> data <""; if (root-> leftchild! = NULL) {cout <"leftchild:" <root-> leftchild-> data <";} If (root-> rightchild! = NULL) {cout <"rightchild:" <root-> rightchild-> data <";}cout <Endl; displaytree (root-> leftchild ); displaytree (root-> rightchild );}} //************************************** **************************************** // Name: firstvisite // Desc: First root traversal (recursion) //************************************** **************************************** void firstvisite (node * root) {If (root! = NULL) {cout <root-> data <"; firstvisite (root-> leftchild); firstvisite (root-> rightchild );}} //************************************** **************************************** // Name: centervisite // Desc: Root traversal (recursion) //************************************** **************************************** void centervisite (node * root) {If (root! = NULL) {centervisite (root-> leftchild); cout <root-> data <""; centervisite (root-> rightchild );}} //************************************** **************************************** // Name: aftervisite // Desc: Post Root traversal (recursion) //************************************** **************************************** void aftervisite (node * root) {If (root! = NULL) {aftervisite (root-> leftchild); aftervisite (root-> rightchild); cout <root-> data <"";}} //************************************** **************************************** // Name: resettree // Desc: resets a binary tree, convenient one-time traversal //*********************************** **************************************** * ** void resettree (node * root) {If (root! = NULL) {root-> leftvisited = false; root-> rightvisited = false; resettree (root-> leftchild); resettree (root-> rightchild );}} //************************************** **************************************** // Name: _ firstvisite // Desc: Root traversal (non-recursion) //************************************** **************************************** void _ firstvisite (node * tree) {resettree (tree); typedef vector <node *> nodestack; nodestac K stack; stack. push_back (tree); // initialize the stack while (stack. Size ()> 0) {node * topnode = stack. Back (); If (! Topnode-> leftvisited &&! Topnode-> rightvisited) {cout <topnode-> data <"";} If (topnode-> leftchild! = NULL &&! Topnode-> leftvisited) {stack. push_back (topnode-> leftchild); topnode-> leftvisited = true;} else if (topnode-> rightchild! = NULL &&! Topnode-> rightvisited) {stack. push_back (topnode-> rightchild); topnode-> rightvisited = true;} else {stack. pop_back ();}}} //************************************** **************************************** // Name: _ firstvisite // Desc: non-recursive root Traversal method 2 //******************************* **************************************** * ****** void _ firstvisite (node * tree) {typedef vector <node *> nodestack; nodestack stack; Node * Curnode = tree; while (! Stack. Empty () | curnode! = NULL) {While (curnode! = NULL) {cout <curnode-> data <"; stack. push_back (curnode); curnode = curnode-> leftchild;} If (! Stack. empty () {curnode = stack. back (); curnode = curnode-> rightchild; stack. pop_back ();}}} //************************************** **************************************** // Name: _ centervisit // Desc: Root traversal (non-recursion) //************************************** **************************************** void _ centervisite (node * tree) {resettree (tree); typedef vector <node *> nodestack; nodestack stack; stack. push_back (Tre E); // initialize while (stack. Size ()> 0) {node * topnode = stack. Back (); If (topnode-> leftvisited &&! Topnode-> rightvisited) {cout <topnode-> data <"";} If (topnode-> leftchild! = NULL &&! Topnode-> leftvisited) {stack. push_back (topnode-> leftchild); topnode-> leftvisited = true;} else {If (topnode-> rightchild! = NULL &&! Topnode-> rightvisited) {If (topnode-> leftchild = NULL & topnode-> rightchild! = NULL) {cout <topnode-> data <""; // only the right child node is available on one side.} stack. push_back (topnode-> rightchild); topnode-> rightvisited = true;} else {If (topnode-> leftchild = NULL & topnode-> rightchild = NULL) {cout <topnode-> data <";} stack. pop_back ();}}}} //************************************** **************************************** // Name: _ centervisite // Desc: non-recursive root Traversal method 2 //******************************** ***************** * *************************** Void _ centervisite (node * tree) {typedef vector <node *> nodestack; nodestack stack; node * curnode = tree; while (! Stack. Empty () | curnode! = NULL) {While (curnode! = NULL) {stack. push_back (curnode); curnode = curnode-> leftchild;} If (! Stack. empty () {curnode = stack. back (); cout <curnode-> data <"; curnode = curnode-> rightchild; stack. pop_back ();}}} //************************************** **************************************** // Name: _ aftervisite // Desc: Post-order traversal (non-recursion) //************************************** **************************************** void _ aftervisite (node * tree) {resettree (tree); typedef vector <node *> nodestack; nodestack s Tack; stack. push_back (tree); // initialize while (stack. size () {node * topnode = stack. back (); If (topnode-> leftvisited & topnode-> rightvisited) {cout <topnode-> data <"" ;}if (topnode-> leftchild! = NULL &&! Topnode-> leftvisited) {stack. push_back (topnode-> leftchild); topnode-> leftvisited = true;} else if (topnode-> rightchild! = NULL &&! Topnode-> rightvisited) {stack. push_back (topnode-> rightchild); topnode-> rightvisited = true ;} else {// If (topnode-> leftchild = NULL | topnode-> rightchild = NULL) {cout <topnode-> data <";} stack. pop_back ();}}} //************************************** **************************************** // Name: _ aftervisite // Desc: non-recursive root Traversal method 2 //******************************** *********************** * ********************* Void _ aftervisite (node * tree) {typedef vector <node *> stacknode; stacknode stack; node * curnode; // the current node * prenode = NULL; // The previous accessed node stack. push_back (tree); While (! Stack. Empty () {curnode = stack. Back (); If (curnode-> leftchild = NULL & curnode-> rightchild = NULL) | (prenode! = NULL & (prenode = curnode-> leftchild | prenode = curnode-> rightchild) {cout <curnode-> data <""; // if no child node exists at the current node or the child node has been accessed to the stack. pop_back (); prenode = curnode;} else {If (curnode-> rightchild! = NULL) {stack. push_back (curnode-> rightchild);} If (curnode-> leftchild! = NULL) {stack. push_back (curnode-> leftchild );}}}} //************************************** **************************************** // Name: levelvisite // Desc: hierarchical traversal //************************************ **************************************** ** void levelvisite (node * tree) {typedef list <node *> queuenode; queuenode queue; queue. push_back (tree); While (queue. size ()> 0) // from top to bottom, from left to right {node * curnode = queue. fron T (); queue. pop_front (); cout <curnode-> data <""; if (curnode-> leftchild! = NULL) {queue. push_back (curnode-> leftchild);} If (curnode-> rightchild! = NULL) {queue. push_back (curnode-> rightchild );}}} //************************************** **************************************** // Name: caculateleafnum // Desc: count the number of leaf nodes //********************************* **************************************** * *** int caculateleafnum (node * tree) {If (tree = NULL) {return 0;} If (tree-> leftchild = NULL & tree-> rightchild = NULL) // isolated point {return 1;} // recursive int sum = 0; s Um + = caculateleafnum (tree-> leftchild); sum + = caculateleafnum (tree-> rightchild); Return sum ;} //************************************** **************************************** // Name: caculateallnodenum // Desc: count the number of all nodes //********************************** **************************************** * *** int caculateallnodenum (node * tree) {/* Static int sum = 0; If (tree! = NULL) {sum + = 1; caculateallnodenum (tree-> leftchild); caculateallnodenum (tree-> rightchild);} */INT sum = 0; If (tree! = NULL) {sum = 1; sum + = caculateallnodenum (tree-> leftchild); sum + = caculateallnodenum (tree-> rightchild);} return sum ;} //************************************** **************************************** // Name: caculatedepth // Desc: depth of Binary Tree Calculation //********************************** **************************************** * *** int caculatedepth (node * tree) {int leftdepth = 0; int rightdepth = 0; If (tree! = NULL) {leftdepth = 1; leftdepth + = weight (tree-> leftchild); rightdepth = 1; rightdepth + = caculatedepth (tree-> rightchild);} return leftdepth> rightdepth? Leftdepth: rightdepth ;} //************************************** **************************************** // Name: caculatewidth // Desc: calculate the width of a binary tree //********************************** **************************************** * *** int caculatewidth (node * tree) {If (tree = NULL) {return 0;} typedef list <node *> queuenode; queuenode queue; unsigned int width = 0; queue. push_back (tree); While (queue. size ()> 0) {uns Igned int size = queue. size (); For (unsigned int I = 0; I <size; ++ I) // All nodes on the previous layer are listed, and press the node {node * curnode = queue. front (); queue. pop_front (); If (curnode-> leftchild! = NULL) {queue. push_back (curnode-> leftchild);} If (curnode-> rightchild! = NULL) {queue. push_back (curnode-> rightchild) ;}} width = max (width, size); // compare with each size, whichever is needed} return width ;} //************************************** **************************************** // Name: release // Desc: release resources //************************************ **************************************** ** void release (node * tree) {If (tree! = NULL) {release (tree-> leftchild); release (tree-> rightchild); Delete tree; tree = NULL ;}} int main () {// data input vector <int> datavec; inttempvalue; cout <"Enter the first sequence extension sequence of the binary tree (-1 is blank):" <Endl; while (CIN> tempvalue) {datavec. push_back (tempvalue);} // create node * root = NULL for a binary tree; root = createtree (root, datavec); // displaytree (Root) for a binary tree ); // recursive first root traversal firstvisite (Root); cout <Endl; // recursive root traversal centervisite (Root); cout <Endl; // recursive post-root traversal aftervisite (Root); cout <Endl; cout <"--------------------------" <Endl; // non-recursive first root traversal _ firstvisite (Root ); cout <Endl; // non-recursive first root traversal 2 _ firstvisite (Root); cout <Endl; // non-recursive root traversal _ centervisite (Root ); cout <Endl; // root Traversal method in non-recursion; 2 _ centervisite (Root); cout <Endl; // root traversal after non-recursion _ aftervisite (Root ); cout <Endl; // non-recursive root Traversal method 2 _ aftervisite (Root); cout <Endl; // layered traversal levelvisite (Root); cout <Endl; // calculate the number of leaf nodes. cout <caculateleafnum (Root) <Endl; // calculate the number of all nodes. cout <caculateallnodenum (Root) <Endl; // calculate the binary tree depth cout <caculatedepth (Root) <Endl; // calculate the binary tree width cout <caculatewidth (Root) <Endl; // release the resource release (Root); System ("pause"); Return 0 ;}

Other operations:

Node * midlasttotree (const string & midin, const string & lastin) // It is known that the binary tree performs sequential traversal and {// The post-sequential traversal to obtain the binary linked list structure if (midin. empty () | lastin. empty () {return 0;} Char rootc = * (lastin. end ()-1); // locate the root node // divide the string midin into two parts: String: size_type rootindex = midin. find_first_of (rootc); string leftmidin = midin. substr (0, rootindex); string rightmidin = midin. substr (rootindex + 1); // divide the string lastin into two parts: String leftlastin; string ri Ghtlastin; string: const_iterator curiter = lastin. Begin (); string: const_iterator enditer = lastin. End (); For (; curiter! = Enditer; ++ curiter) {char c = * curiter; If (leftmidin. find_first_of (c )! = String: NPOs) {leftlastin. push_back (c);} else if (rightmidin. find_first_of (c )! = String: NPOs) {rightlastin. push_back (c) ;}} node * tree = new node; tree-> C = rootc; // recursive tree-> left = midlasttotree (leftmidin, leftlastin ); tree-> right = midlasttotree (rightmidin, rightlastin); Return tree;} node * firstmidtotree (const string & midin, const string firstin) // known binary tree in-order traversal and {// first-order traversal sequence, find the binary tree chain table structure if (midin. empty () | firstin. empty () {return 0;} Char rootc = * firstin. begin (); // splits the string midin into two parts: String:: Size_type rootindex = midin. find_first_of (rootc); string leftmidin = midin. substr (0, rootindex); string rightmidin = midin. substr (rootindex + 1); // split the string firstin into two parts: String leftfirstin, rightfirstin; string: const_iterator curiter = firstin. begin (); string: const_iterator enditer = firstin. end (); While (curiter! = Enditer) {char c = * curiter ++; If (leftmidin. find_first_of (c )! = String: NPOs) {leftfirstin. push_back (c);} else if (rightmidin. find_first_of (c )! = String: NPOs) {rightfirstin. push_back (c) ;}} node * tree = new node; tree-> C = rootc; // recursive tree-> left = firstmidtotree (leftmidin, leftfirstin ); tree-> right = firstmidtotree (rightmidin, rightfirstin); Return tree ;}

Train of Thought 3 for non-recursive post-root traversal:

Void postorder2 (bintree * root) // non-recursive post-order traversal {stack <btnode *> S; bintree * P = root; btnode * temp; while (P! = NULL |! S. Empty () {While (P! = NULL) // search down the left subtree until a node without the left subtree is displayed {btnode * BTN = (btnode *) malloc (sizeof (btnode )); BTN-> btnode = P; BTN-> isfirst = true; S. push (BTN); P = p-> lchild;} If (! S. empty () {temp = S. top (); S. pop (); If (temp-> isfirst = true) // indicates that it is the first time that it appears on the top of the stack {temp-> isfirst = false; S. push (temp); P = temp-> btnode-> rchild ;} else // appears at the top of the stack for the second time {cout <temp-> btnode-> data <"; P = NULL ;}}}}

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.