• 一个简单二叉树的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<<"	";
        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<<"	";
        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<<"	";
    }
    
    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: 	";
        tree->preOrder(root);
        cout<<"
    inOrder: 	";
        tree->inOrder(root);
        cout<<"
    postOrder: 	";
        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$ 

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

    欢迎交流!

  • 相关阅读:
    C# 微信小程序模板消息---发送
    (转)python的 __init_, __call__, __new__ 详解
    (转)[Python]实例方法、类方法、静态方法
    (转)Python——gRPC详解及实战避坑方案(下)
    (转)Python——gRPC详解及实战避坑方案(上)
    (转)pika详解(五)登录认证及connectionParameters
    (转)pika详解(四) channel 通道
    (转)pika详解(三)SelectConnection及其他Connection
    (转)pika详解(二) BlockingConnection
    (转)pika详解 (一)
  • 原文地址:https://www.cnblogs.com/ruchicyan/p/4655945.html
Copyright © 2020-2023  润新知