Long time no contact with the binary tree, write this as practiced hand, the next will be more detailed to achieve the two-fork tree of the various functions and applications.
/** binarytree.cpp* Author:qiang xiao* time:2015-07-17*/#include<iostream>#include<string>using namespacestd;template<classElemtype>classbinarynode{ Public: Elemtype data; Binarynode<elemtype>*Leftchild; Binarynode<elemtype>*Rightchild; Binarynode (); Binarynode (ConstElemtype Elem, binarynode<elemtype>* left = NULL, binarynode<elemtype>* right =NULL); voidPrint ()Const;}; Template<classElemtype>voidBinarynode<elemtype>::p rint ()Const{cout<< This->data <<Endl;} Template<classElemtype>Binarynode<ElemType>:: Binarynode () { This->leftchild =NULL; This->rightchild =NULL;} Template<classElemtype>Binarynode<elemtype>::binarynode (ConstElemtype Elem, binarynode<elemtype>* left, binarynode<elemtype>*Right ) { This->data =Elem; This->leftchild =Left ; This->rightchild =Right ;}/**************************************************************************************************/Template<classElemtype>classbinarytree{Private: Binarynode<elemtype>*Root; Public: BinaryTree (); BinaryTree (Binarynode<elemtype>*R);//~binarytree ();binarynode<elemtype>* Getroot ()Const; BOOLIsEmpty ()Const; voidPreorder (binarynode<elemtype>* R)Const; voidInorder (binarynode<elemtype>* R)Const; voidPostorder (binarynode<elemtype>* R)Const;}; Template<classElemtype>BinaryTree<elemtype>::binarytree (binarynode<elemtype>*R) {Root=R;} Template<classElemtype>BinaryTree<ElemType>:: BinaryTree () {root=NewBinarynode<elemtype>();} Template<classElemtype>Binarynode<elemtype>* Binarytree<elemtype>::getroot ()Const{ returnRoot;} Template<classElemtype>BOOLBinarytree<elemtype>::isempty ()Const{ returnroot==false;} Template<classElemtype>voidBinarytree<elemtype>::p Reorder (binarynode<elemtype>* R)Const{ if(r==NULL)return; cout<<r->data<<"\ t"; Preorder (R-leftchild); Preorder (R-rightchild);} Template<classElemtype>voidBinarytree<elemtype>::inorder (binarynode<elemtype>* R)Const{ if(r==NULL)return; Inorder (R-leftchild); cout<<r->data<<"\ t"; Inorder (R-rightchild);} Template<classElemtype>voidBinarytree<elemtype>::p Ostorder (binarynode<elemtype>* R)Const{ if(r==NULL)return; Postorder (R-leftchild); Postorder (R-rightchild); cout<<r->data<<"\ t";}intMain () {Binarynode<string>* right1=Newbinarynode<string> ("RIGHT1"); Binarynode<string>* left=Newbinarynode<string> (" Left", NULL, right1); Binarynode<string>* right=Newbinarynode<string> (" Right"); Binarynode<string>* root =Newbinarynode<string> ("ROOT", left, right); BinaryTree<string>* tree=Newbinarytree<string>(root); cout<<"preorder: \ t"; Tree-preorder (root); cout<<"\ninorder: \ t"; Tree-inorder (root); cout<<"\npostorder: \ t"; Tree-Postorder (root); cout<<Endl; return 0;}
Here is the result of the operation:
[email protected]:~/c/datastructure$./BINARYTREE.O Preorder: ROOT left RIGHT1 Right Inorder: left RIGHT1 root right postorder: RIGHT1 left right root [email protected]
This version is the primary version, and the traversal takes the recursive approach. The next step will continue to improve!
Welcome to the Exchange!
C + + implementation of a simple binary tree (i)