一个简单二叉树的C++实现(一)

很久没有接触二叉树了,写这个当作练手,接下来会比较详细地实现二叉树的各个功能及应用。

/*
*   BinaryTree.cpp
*   Author: Qiang Xiao
*   Time:   2015-07-17
*/

#include<iostream>
#include<string>
using namespace std;


template<class ElemType>
class BinaryNode{
public:
    ElemType data;
    BinaryNode<ElemType>* leftChild;
    BinaryNode<ElemType>* rightChild;

    BinaryNode();
    BinaryNode(const ElemType elem, BinaryNode<ElemType>* left = NULL, BinaryNode<ElemType>* right = NULL);
    void print() const;
};

template<class ElemType>
void BinaryNode<ElemType>::print() const{
    cout << this->data << endl;
}

template<class ElemType>
BinaryNode<ElemType>::BinaryNode(){
    this->leftChild = NULL;
    this->rightChild = NULL;
}

template<class ElemType>
BinaryNode<ElemType>::BinaryNode(const ElemType elem, BinaryNode<ElemType>* left, BinaryNode<ElemType>* right){
    this->data = elem;
    this->leftChild = left;
    this->rightChild = right;
}

/**************************************************************************************************/
template<class ElemType>
class BinaryTree{
    private:
    BinaryNode<ElemType>* root;
    public:
    BinaryTree();
    BinaryTree(BinaryNode<ElemType>* r);
//    ~BinaryTree();
    BinaryNode<ElemType>* getRoot() const;
    bool isEmpty() const;
    void preOrder(BinaryNode<ElemType>* r) const;
    void inOrder(BinaryNode<ElemType>* r) const;
    void postOrder(BinaryNode<ElemType>* r) const;
};


template<class ElemType>
BinaryTree<ElemType>::BinaryTree(BinaryNode<ElemType>* r){
    root= r;
}


template<class ElemType>
BinaryTree<ElemType>::BinaryTree(){
    root= new BinaryNode<ElemType>();
}

template<class ElemType>
BinaryNode<ElemType>* BinaryTree<ElemType>::getRoot() const{
    return root;
}

template<class ElemType>
bool BinaryTree<ElemType>::isEmpty() const{
    return root== false;
}

template<class ElemType>
void BinaryTree<ElemType>::preOrder(BinaryNode<ElemType>* r) const{
    if(r== NULL)
    return;
    cout<<r->data<<"\t";
    preOrder(r->leftChild);
    preOrder(r->rightChild);
}


template<class ElemType>
void BinaryTree<ElemType>::inOrder(BinaryNode<ElemType>* r) const{
    if(r== NULL)
    return;
    inOrder(r->leftChild);
    cout<<r->data<<"\t";
    inOrder(r->rightChild);
}

template<class ElemType>
void BinaryTree<ElemType>::postOrder(BinaryNode<ElemType>* r) const{
    if(r== NULL)
    return;
    postOrder(r->leftChild);
    postOrder(r->rightChild);
    cout<<r->data<<"\t";
}

int main(){
    BinaryNode<string>* right1= new BinaryNode<string>("RIGHT1");
    BinaryNode<string>* left= new BinaryNode<string>("LEFT", NULL, right1);
    BinaryNode<string>* right= new BinaryNode<string>("RIGHT");
    BinaryNode<string>* root = new BinaryNode<string>("ROOT", left, right);

    BinaryTree<string>* tree= new BinaryTree<string>(root);
    cout<<"preOrder: \t";
    tree->preOrder(root);
    cout<<"\ninOrder: \t";
    tree->inOrder(root);
    cout<<"\npostOrder: \t";
    tree->postOrder(root);
    cout<<endl;

    return 0;
}

下面是运行结果:

xiaoq@xq-ubun:~/C/DataStructure$ ./BinaryTree.o 
preOrder:     ROOT    LEFT    RIGHT1    RIGHT    
inOrder:      LEFT    RIGHT1    ROOT    RIGHT    
postOrder:    RIGHT1    LEFT    RIGHT    ROOT    
xiaoq@xq-ubun:~/C/DataStructure$ 

 

这个版本是初级版本,遍历采用递归方式。接下来将不断完善!

欢迎交流!

转载于:https://www.cnblogs.com/ruchicyan/p/4655945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值