二叉查找树的简单实现

^_^ 好些天木有写博客了,惭愧啊,不过不代表我没有继续学习哦~

还是要向大神学习,每天坚持学习,每天写博客~加油!!!


#ifndef BINARY_SEARCHTREE_J
#define BINARY_SEARCHTREE_J
#include <iostream>
using std::cout;
#ifndef NULL
#define NULL 0
#endif
template <typename _Tp>
class BinarySearchTree {
public:
    struct TreeNode {
        _Tp element;
        TreeNode *left_child;
        TreeNode *right_child;
        TreeNode(const _Tp &param = 0) : element(param), left_child(0),right_child(0) {}
    };
public:
    BinarySearchTree() : root(NULL) {}
    BinarySearchTree(const BinarySearchTree<_Tp> &rhs);
    BinarySearchTree &operator=(const BinarySearchTree<_Tp> &rhs);
    ~BinarySearchTree() {
        makeEmpty();
    }
//========================================== 
    TreeNode *findMax() const {
        return findMax(root);
    }
    TreeNode *findMin() const {
        return findMin(root);
    }
    bool contains(const _Tp &rhs) const {
        return contains(rhs, root);
    }
    bool empty() const {
        return root == NULL;
    }
    void printTree() const {
        printTree(root);
    }
    void makeEmpty() {
        makeEmpty(root);
    }
    void insert(const _Tp &rhs) {
        insert(rhs, root);
    }
    void remove(const _Tp &rhs) {
        remove(rhs, root);
    }
    
//========================================== 
private:
    TreeNode *root;
    TreeNode *findMax(TreeNode *itr) const {
        if (itr == NULL) return NULL;
        if (itr->right_child == NULL) return itr;
        return findMax(itr->right_child);
    }
    TreeNode *findMin(TreeNode *itr) const {
        if (itr == NULL) return NULL;
        if (itr->left_child == NULL) return itr;
        return findMin(itr->left_child);
    }
    void insert(const _Tp &rhs, TreeNode *&itr) {
        if (itr == NULL) {
            itr = new TreeNode(rhs);
        } else if ( rhs < itr->element ) {
            insert(rhs, itr->left_child);
        } else if ( rhs > itr->element ) {
            insert(rhs, itr->right_child);
        }
    }
    bool contains(const _Tp &rhs, TreeNode *itr) const {
        if (itr == NULL) return false;
        if (itr->element == rhs) return true;
        else if (rhs < itr->element ) return contains(rhs, itr->left_child);
        return contains(rhs, itr->right_child);
    }
    void remove(const _Tp &rhs, TreeNode *&itr) {
        if (itr == NULL) return;
        if (rhs < itr->element) remove(rhs, itr->left_child);
        else if (rhs > itr->element) remove(rhs, itr->right_child);
        else if (itr->left_child != NULL && itr->right_child != NULL) {
            itr->element = findMin(itr->right_child)->element;
            remove(itr->element, itr->right_child);
        } else {
            TreeNode *temp = itr;
            itr = (itr->left_child != NULL) ? itr->left_child : itr->right_child;
            delete temp;
        }
    }
    void makeEmpty(TreeNode *&itr) {
        if (itr == NULL) return;
        makeEmpty(itr->left_child);
        makeEmpty(itr->right_child);
        delete itr;
        itr = NULL;
    }
    void printTree(TreeNode *itr) const {
        if (itr == NULL) return;
        cout <<" " << itr->element << " ";
        printTree(itr->left_child);
        printTree(itr->right_child);
    }
    void clone(TreeNode *&lhs,const TreeNode *rhs) {
        if (rhs == NULL) return;
        lhs = new TreeNode(rhs->element);
        clone(lhs->left_child, rhs->left_child);
        clone(lhs->right_child, rhs->right_child);
    }
};
template <typename _Tp>
BinarySearchTree<_Tp>::BinarySearchTree(const BinarySearchTree<_Tp> &rhs) {
    clone(root, rhs.root); 
}
template <typename _Tp>
BinarySearchTree<_Tp> &BinarySearchTree<_Tp>::operator=(const BinarySearchTree<_Tp> &rhs) {
    if (this != &rhs) {
        makeEmpty();
        clone(root, rhs.root);
    }
    return *this;
}
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值