With the array storage is similar to the previous article, just change a storage method, interested can look at the following code, specifically do not explain.
#include <iostream>#include<string>using namespacestd;classbitreenode{ public: CharData//node DataBitreenode *leftchild;//left dial hand tree pointerBitreenode *rightchild;//Right sub-tree pointerbitreenode (): leftchild (null), rightchild (null) {}~Bitreenode () {}};classbitree{Private: Bitreenode*root;//root node pointer intpos,len; stringstrtree; Bitreenode*createbitree (intpos); voidPreorder (bitreenode *t); voidInorder (bitreenode *t); voidPostorder (bitreenode *t); public: Bitree () {}; ~Bitree () {}; voidCreatetree (stringtreearray); voidPreorder (); voidinorder (); voidPostorder ();};//construct a two-fork tree and make use of the result of first order traversalvoidBitree::createtree (stringTreearray)//public functions, External Interfaces{len= Treearray.length ();//the length of the input arrayStrtree.assign (treearray);//dumps the input array into the data strtree of the classRoot = Createbitree (0);//read data starting from array 0}bitreenode*bitree::createbitree (intPos//recursive contributions at Pos location, private functions, In-class implementations{//Pass the current node in the position of the array with the parameter pos, binary tree properties 5, because the array is numbered starting from 0, the parent node is i, the child position is 2i+1,2i+2 .Bitreenode *t;//Temporary pointer T intch; if(pos>strtree.length ())returnNULL; CH=strtree[pos]; if(ch=='0') T=NULL; Else{T=NewBitreenode (); T->data = ch;//Generating root nodes if(2* (pos+1) <=Len) T->leftchild = Createbitree (2* (pos+1)-1); if(2* (pos+1)+1<=Len) T->rightchild = Createbitree (2* (pos+1)); } returnT;}//defining the First-order traversal functionvoidBitree::P Reorder ()//public functions, External Interfaces{preorder (Root);}voidBitree::P Reorder (bitreenode *t)//private functions, In-class implementations{ if(t!=NULL) {cout<< t->data;//outputs the data for the current node t, indicating that T has been accessedPreorder (t->leftchild);//the left child of the first order traverse TPreorder (t->rightchild);//the right child of the first order traversal T }}//Defining the sequence traversal functionvoidBitree::inorder ()//public functions, External Interfaces{inorder (Root);}voidBitree::inorder (bitreenode *t)//private functions, In-class implementations{ if(t) {inorder (t->leftchild);//the left child of the middle sequence traversing Tcout << t->data;//outputs the data for the current node t, indicating that T has been accessedInorder (t->rightchild);//right child with middle order traversal T }}//post-sequential traversal functionvoidBitree::P Ostorder ()//public functions, External Interfaces{postorder (Root);}voidBitree::P Ostorder (bitreenode *t)//private functions, In-class implementations{ if(t) {postorder (t->leftchild);//the left child of the post-traversal TPostorder (t->rightchild);//the right child to traverse Tcout << t->data;//outputs the data for the current node t, indicating that T has been accessed }}intMainvoid){ intt; Bitree T; stringstr; CIN>>t; for(intI=0; i<t;i++) {cin>>str; T.createtree (str); T.preorder (); cout<<endl; //T.inorder (); //cout << endl; //T.postorder (); //cout << endl; } return 0;}
Base data structure-two Fork Tree-expansion: building based on array storage