ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

二叉树C++实现

2020-05-01 16:52:56  阅读:262  来源: 互联网

标签:Comparable const 实现 binarysearchtree C++ 二叉树 return NULL BinaryNode


二叉搜索树的代码实现,有插入、查找、删除等基本功能。

需要注意的是,当类中有私有类pClass且在类外声明的成员函数的返回值是pClass类型的时候,需要在pClass前加typename。比如说

template <typename Comparable> typename binarysearchtree<Comparable>::BinaryNode* binarysearchtree<Comparable>::findMax(BinaryNode* t)const {
    if (t == NULL) return NULL;
    if (t->right == NULL) return t;
    return findMin(t->right);
}

这里的typename如果去掉的话就会报错

warning C4346:  “BinaryNode”: 依赖名称不是类型
error C2061:  语法错误: 标识符“BinaryNode”

 

这么做的原因,我目前还没搞清楚。

#pragma once
template <typename Comparable> class binarysearchtree {
public:
    binarysearchtree() { BinaryNode* root = nullptr; }
    binarysearchtree(const binarysearchtree& rhs) { this = rhs; }
    ~binarysearchtree() { makeEmpty(); }

    const Comparable& findMin() const { return findMin(root); }
    const Comparable& findMax() const { return findMax(root); }
    bool contains(const Comparable& x) const;
    bool isEmpty() const { return root == nullptr; }
    void printTree() const {
        printTree(root, 0);
    }

    void makeEmpty() {
        makeEmpty(root);

    }
    void insert(const Comparable& x);
    void remove(const Comparable& x);

    const binarysearchtree& operator=(const binarysearchtree& rhs) {
        if (*this != rhs) {
            makeEmpty();
            root = clone(rhs.root);
        }
        return &this;
    }
    
private:
    struct BinaryNode {
        Comparable element;
        BinaryNode* left;
        BinaryNode* right;

        BinaryNode(const Comparable& theElement, BinaryNode* lt, BinaryNode* rt) :
            element(theElement), left(lt), right(rt) {}
    };

    BinaryNode* root;

    void insert(const Comparable& x, BinaryNode*& t) const;
    void remove(const Comparable& x, BinaryNode*& t) const;
    BinaryNode* findMin(BinaryNode* t)const;
    BinaryNode* findMax(BinaryNode* t)const;
    bool contains(const Comparable& x, BinaryNode* t) const;
    void makeEmpty(BinaryNode*& t);
    void printTree(BinaryNode* t, const int& depth)const;
    BinaryNode* clone(BinaryNode* t) const;
};

template <typename Comparable> bool binarysearchtree<Comparable>::contains(const Comparable& x) const {
    return contains(x, root);
}

template <typename Comparable> void binarysearchtree<Comparable>::insert(const Comparable& x) {
    insert(x, root);
}

template <typename Comparable> void binarysearchtree<Comparable>::remove(const Comparable& x) {
    remove(x, root);
}

template <typename Comparable> bool binarysearchtree<Comparable>::contains(const Comparable& x, BinaryNode* t) const {
    if (t == NULL) return false;
    else if (t->element > x) return contains(x, t->left);
    else if (t->element < x) return contains(x, t->right);
    else return true;
}

template <typename Comparable> typename binarysearchtree<Comparable>::BinaryNode* binarysearchtree<Comparable>::findMin(BinaryNode* t)const{
    if (t == NULL) return NULL;
    if (t->left == NULL) return t;
    return findMin(t->left);
}

template <typename Comparable> typename binarysearchtree<Comparable>::BinaryNode* binarysearchtree<Comparable>::findMax(BinaryNode* t)const {
    if (t == NULL) return NULL;
    if (t->right == NULL) return t;
    return findMin(t->right);
}

template <typename Comparable> void binarysearchtree<Comparable>::insert(const Comparable& x, BinaryNode*& t) const {
    if (t == NULL) t = new BinaryNode(x, NULL, NULL);
    else if (t->element > x) insert(x, t->left);
    else if (t->element < x) insert(x, t->right);
    else;
}

template <typename Comparable> void binarysearchtree<Comparable>::remove(const Comparable& x, BinaryNode*& t) const {
    if (t == NULL) return;
    if (t->element > x) remove(x, t->left);
    else if (t->element < x) remove(x, t->right);
    else if (t->right != NULL && t->left != NULL) {
        t->element = findMin(t->right)->element;
        remove(t->element, t->right);
    }
    else {
        BinaryNode* oldNode = t;
        t = (t->left != NULL) ? t->left : t->right;
        delete oldNode;
    }
}

template<typename Comparable> void binarysearchtree<Comparable>::makeEmpty(BinaryNode*& t) {
    if (t != nullptr) {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
        t = nullptr;
    }
}

template<typename Comparable> typename binarysearchtree<Comparable>::BinaryNode* binarysearchtree<Comparable>::clone(BinaryNode* t) const {
    if (t == nullptr) return t;
    return new BinaryNode(t->element, clone(t->left), clone(t->right));
}

template<typename Comparable> void binarysearchtree<Comparable>::printTree(BinaryNode* t, const int& i)const {
    if (t != nullptr) {
        printTree(t->left, i + 1);
        std::cout << string(i, '\t') << t->element << std::endl;
        
        printTree(t->right, i + 1);
    }
}

 

标签:Comparable,const,实现,binarysearchtree,C++,二叉树,return,NULL,BinaryNode
来源: https://www.cnblogs.com/airfy/p/12813962.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有