C + + implementation of a simple binary tree (i)

Source: Internet
Author: User

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&gt::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&gt::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)

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.