二叉查找树的实现(c++)

来源:互联网 发布:淘宝直播回放怎么下载 编辑:程序博客网 时间:2024/05/25 19:59
#ifndef _HJ_STL_BST_H_#define _HJ_STL_BST_H_/** Author:hujian* Time:2016/5/8* discription:this file is about binary search tree<BST>.** NOTICE:you should not use hjstl_vector in anywhere.* because the hjstl_vector has too much bugs.* it not work till now.* NOTICE:if you find bugs,please connect me with E-mail or WeChat*E-mail:1425124481@qq.com & hujianshiwo&nankai.edu.cn*WeChat:HUJIAN000000**/#define _HJSTL_BST_PUBLIC  public#define _HJSTL_BST_PRINATE  private#define _HJSTL_BST_PROTECTED protected///this is the node of bst///template<class Type>struct _HJSTL_BST_Node_{    _HJSTL_BST_Node_() :_L_child(NULL), _R_child(NULL){}    Type value;//node's value    _HJSTL_BST_Node_* _L_child, *_R_child;//left child and right child};//end of bst node///this is the class about BSTtemplate<class Type>class _HJSTL_BST{_HJSTL_BST_PRINATE:    typedef _HJSTL_BST_Node_<Type>  node_type;    typedef node_type*   node_pointer;    typedef const node_type* const_node_pointer;    typedef node_type&  reference;    typedef const node_type& const_reference;    typedef size_t size_type;    //you can not access the root and size._HJSTL_BST_PRINATE:    //this is the root of this BST    node_pointer root;    //the node's num<size>    size_t  size_;_HJSTL_BST_PUBLIC:    _HJSTL_BST() :root(NULL),size_(0){}    _HJSTL_BST(_HJSTL_BST<Type>&x){ root = x.root, size_ = x.size_; }    //empty?    bool empty(){ return size_ == 0; }    //size of this bst    size_type size(){ return size_; }    //get the root    node_pointer ROOT(){ return root; }    //insert     void insert(const Type& x){ insert_aux(root, x); }    //find    bool find(const Type&x) { return find_aux(root, x); }    //delete node    void remove(const Type&x){        //if you want to delete root        if (root->value == x){            if (root->_L_child == NULL&&root->_R_child == NULL){                root = NULL;                size_--;            }            else if (root->_L_child != NULL){                root = root->_L_child;                size_--;            }            else if (root->_R_child != NULL){                root = root->_R_child;                size_--;            }            return;        }        if (remove_aux(root, x)) size_--;     }_HJSTL_BST_PRINATE:    //insert node    node_pointer insert_aux(node_pointer& cur,const Type&x);    //find value    bool find_aux(node_pointer cur,const Type&x);    //delete node    node_pointer remove_aux(node_pointer cur,const Type&x);};//end of bst//implement the code.template<class Type>_HJSTL_BST_Node_<Type>* _HJSTL_BST<Type>::insert_aux(_HJSTL_BST_Node_<Type>*& cur, const Type& x){    if (cur == NULL){//this node is empty,so we find the position to insert        cur = new _HJSTL_BST_Node_<Type>();        cur->value = x;        cur->_L_child = cur->_R_child = NULL;        size_++;        return cur;    }    else{//find the position and insert the node         if (x < cur->value)  insert_aux(cur->_L_child, x);        else insert_aux(cur->_R_child, x);    }}//findtemplate<class Type>bool _HJSTL_BST<Type>::find_aux(_HJSTL_BST_Node_<Type>* cur, const Type& x){    if (cur == NULL) return false;    else if (x == cur->value) return true;    else if (x < cur->value) return find_aux(cur->_L_child, x);    else find_aux(cur->_R_child, x);}template<class Type>_HJSTL_BST_Node_<Type>* _HJSTL_BST<Type>::remove_aux(_HJSTL_BST_Node_<Type>* cur, const Type& x){    //no find node's value=x    if (cur == NULL) return NULL;    else if (x < cur->value) cur->_L_child = remove_aux(cur->_L_child, x);    else if (x > cur->value) cur->_R_child = remove_aux(cur->_R_child, x);    //else,we can start to delete the cur node.    //@1 left child is null    else if (cur->_L_child == NULL){        //left child is null,so easy to do this        _HJSTL_BST_Node_<Type>* rep = cur->_R_child;        delete cur;        return rep;    }    //@2 left's right child is null    else if (cur->_L_child->_R_child == NULL){        //so easy to do this        _HJSTL_BST_Node_<Type>* rep = cur->_L_child;        rep->_R_child = cur->_R_child;        delete cur;        return rep;    }    //@3 this case is complex.please paint the case in paper and understand it    else{        //i get the largest node from cur->left,and let the node        //as the new node.        //you can also get the minimum node from cur->right,and let        //the node as the new node        //        _HJSTL_BST_Node_<Type>* rep = cur->_L_child;        //get the maximum from left child-tree        for (; rep->_R_child->_R_child!=NULL; rep = rep->_R_child);        _HJSTL_BST_Node_<Type>* res = rep->_R_child;        rep->_R_child = res->_L_child;        res->_L_child = cur->_L_child;        res->_R_child = cur->_R_child;        delete cur;        return res;    }    return cur;}#endif  //end of _HJ_STL_BST_H_///<2016/5/10 nankai hujian>
0 0
原创粉丝点击