Creation and traversal of binary tree

Source: Internet
Author: User

I wrote a binary tree operation, referring to the other people's writing, found that should be a good study of other people's wording:

Source Text Address:

http://hi.baidu.com/klcstudy/blog/item/5fdf49b5b57d62ce37d3ca18.html/cmtid/203d9f10fe11210c203f2e8e

Paste the Reference code:

  #include <iostream> using namespace std; Definition of binary tree node class template< Class t> struct Btnode {T data; Btnode <T> * lchild,*rchild; Btnode (t nodevalue = t (), btnode<t>* leftnode = null,btnode<t>* Rightnode = NULL):d ata (nodevalue), Lchild (lef tnode), Rchild (Rightnode) {}//can select the default constructor for the parameter}; The establishment of the binary tree template < Class t> void Createbintree (btnode<t> * &root) {btnode<t>* p = root; btnode<t>* K; T NodeValue; cin>>nodevalue; if (nodevalue==-1) {root=null;} else {root=new btnode<t> (); root->data = NodeValue; Createbintree (root->Lch ILD); Createbintree (Root->rchild); }}//************************************************************************************//Two fork Tree of the first order traversal template < Class t> void Preorder (btnode<t> * & P) {if (p) {Cout<<p->data<< ""; Preorder (P->lchild); Preorder (P->rchild); }}//**************************************************************************************//Two fork tree in the middle sequence traversal template <class t> void Inorder (btnode<t> * & P) {if (p) {inorder (p->lchild); cout<<p->data<< "" ; Inorder (P->rchild); }}//**************************************************************************************//Two fork tree after the post-traversal template <class t> void Levelorder (btnode<t> *& p) {if (p) {Levelorder (p->lchild); Levelorder (p->Rchild); c out<<p->data<< "";}} The number of nodes in the statistic binary tree template <class t> int Countnode (btnode<t> * & P) {if (p = = NULL) return 0; return 1+countnode (P->lchild) +countn Ode (P->rchild); }//***********************************************************************************//Find the depth of the binary tree template< Class t> int Depth (BtnodE<t> *& p) {if (p = = NULL) return-1; int h1 = depth (p->lchild); int h2 = depth (p->rchild); if (H1&GT;H2) re Turn (h1+1); return h2+1; }//***********************************************************************************//Two cross-tree destruction operation template< Class t> btnode<t>* Destroy (btnode<t>* p)//Extinction function, used to destroy each node in the binary tree {if (p) {return destroy (P->lchild); ret Urn Destroy (P->rchild); Delete p; }}//********************************************************************************//main function Design int main () {BTNode <int> * RootNode = NULL; int choiced = 0; while (true) {System ("CLS"); cout<< "/n/n/n---main interface---/n/n/n"; cout<< "1, create two-fork-Tree 2, first-order traverse binary tree/n"; cout<< "3, Middle sequence Traversal binary Tree 4, post-order traversal binary tree/n "; cout<< "5, Statistics node total 6, view tree depth/n"; cout<< "7, destroy the binary tree 0, exit/n/n"; cout<< "Please select the operation:"; cin>>choiced; if (choiced = = 0) return 0; else if (choiced = = 1) {System ("CLS"); cout<< "Please enter each node, return confirmation, and end with-1:/n"; Createbintree (RootNode);} else if (choiced = = 2) {System ("CLS "); cout<< "Sequential traversal of Binary tree results:/n"; Preorder (RootNode); cout<<endl; System ("pause"); } else if (choiced = = 3) {System ("CLS"); cout<< "middle order traversal binary tree Result:/n"; inorder (RootNode); cout<<endl; system ("Pause" ); } else if (choiced = = 4) {System ("CLS"); cout<< "post-traverse binary tree results:/n"; Levelorder (RootNode); cout<<endl; System (" Pause "); } else if (choiced = = 5) {System ("CLS"); int count = Countnode (rootNode); cout<< "The total number of nodes in the binary tree is" <<count<<end L System ("pause"); } else if (choiced = = 6) {System ("CLS"); int dep = depth (rootNode); cout<< "depth of this binary tree is" <<dep<<endl; system ( "Pause"); } else if (choiced = = 7) {System ("CLS"); cout<< "Binary tree has been destroyed. /n "; Destroy (RootNode); cout<<endl; System ("pause"); } else {System ("CLS"); cout<< "/n/n/n/n/n/t error selection. /n "; } } }

such as 5
/     /
3 8
/ /     / /
2 4 6 9
/ /   / / / /    / /
1-1-1-1-17-1-1
/ /              / /
-1-1-1-1

Then the sequence entered is: 5321-1-1-14-1-186-17-1-19-1-1

Note: Because the two fork tree to be created must be a full two fork tree, each end node needs to end with-1.

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.