• 一个简单二叉树的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$ 

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

    欢迎交流!

  • 相关阅读:
    AndroidUI 控件命名格式
    VoIP常见编码的带宽计算
    Hessian 原理分析
    关于异步,同步,阻塞与非阻塞
    dubbo 官方参考手册~备案(防止哪天阿里一生气把dubbo给删除了)
    企业常用的RPC框架比较
    SpringMVC整合Hessian访问远程服务
    Hessian与Webservice的区别
    Dubbo与Zookeeper、Spring整合使用
    java.lang.ClassNotFoundException: org.I0Itec.zkclient.exception.ZkNoNodeException 异常 如何处理
  • 原文地址:https://www.cnblogs.com/ruchicyan/p/4655945.html
Copyright © 2020-2023  润新知