BST的C++实现

BST首先是二叉树,对应的二叉树定义和常用操作封装在以下头文件"bintree.h"

// 二叉树的头文件
#ifndef bintree_h
#define bintree_h


#include 
using namespace std;

template
struct BinNode{
    Elem data;
    BinNode* left;
    BinNode* right;
    BinNode(Elem x){
        data=x;
        left=right=NULL;
    }
};

template
class BinTree{
protected:
    BinNode* root;
    void rpreprint(BinNode* r);
    void rinprint(BinNode* r);
    void rpostprint(BinNode* r);
    int cntLeaves(BinNode* r);
    BinNode* rfindX(Elem x,BinNode* r);
    void rprint(BinNode* r,int depth);
public:
    BinTree()   {root=NULL;}
    BinTree(Elem r){
        root=new BinNode(r);
    }
    ~BinTree(){ };
    void preprint(){
        rpreprint(root);
        cout<* findX(Elem x){
        return rfindX(x,root);
    }
    bool insert(Elem p,int LorR,Elem x);
    int cnt(){
        return cntLeaves(root);
    }
};

template
void BinTree::rpreprint(BinNode* r){   // 对于传指针,一定不要掉以轻心,要首先看一下指针是不是空指针
        if(r==NULL) return;
        cout<data<<" ";
        rpreprint(r->left);
        rpreprint(r->right);
    }

template
void BinTree::rinprint(BinNode* r){
        if(r==NULL) return;
        rinprint(r->left);
        cout<data<<" ";
        rinprint(r->right);
    }

template
void BinTree::rpostprint(BinNode* r){
        if(r==NULL) return;
        rinprint(r->left);
        rinprint(r->right);
        cout<data<<" ";
    }

template
int BinTree::cntLeaves(BinNode* r){
        if(r==NULL) return 0;
        if(r->left==NULL && r->right==NULL) return 1;
        return cntLeaves(r->left)+cntLeaves(r->right);
    }

template
BinNode* BinTree::rfindX(Elem x,BinNode* r){
        if(!r)   return NULL;
        if(r->data==x)   return r;
        BinNode* found;
        found=rfindX(x,r->left);
        return found?found:rfindX(x,r->right);
    }

template
void BinTree::rprint(BinNode* r,int depth){
        for(int i=0;idata<left,depth+1);
        rprint(r->right,depth+1);
        }
    }

template
bool BinTree::insert(Elem p,int LorR,Elem x){
        BinNode* found;
        found=findX(p);
        if(!found)  return false;
        if(LorR==0){
            if(found->left) return false;
            found->left=new BinNode(x);
        }
        else{
            if(found->right)    return false;
            found->right=new BinNode(x);
        }
        return true;
    }
#endif // bintree_h

二叉搜索树的实现

// 二叉搜索树
#include 
#include "bintree.h"
using namespace std;

template
class BSTree : public BinTree{
protected:
    BinNode* rfindMax(BinNode* r){
        if(r->right==NULL)  return r;
        return rfindMax(r->right);  // 尾递归
    }
    BinNode* rinsert(Elem x,BinNode* r){
        if(r==NULL){
            r=new BinNode(x);
            if(!r)    throw -1;
        }
        else if(xdata)  r->left=rinsert(x,r->left);
        else if(x>r->data)  r->right=rinsert(x,r->right);
        else throw -2;
        return r;
    }
    BinNode* remove(Elem x,BinNode* r){
        BinNode* tmp;
        if(!r)  throw -1;
        else if(xdata){
            r->left=remove(x,r->left);
        }
        else if(x>r->data){
            r->right=remove(x,r->right);
        }
        else{
            if(r->left && r->right){
                tmp=rfindMax(r->left);
                r->data=tmp->data;
                r->left=remove(tmp->data,r->left);
            }
            else{
                tmp=r;
                r=r->left?r->left:r->right;
                delete tmp;
            }
        }
        return r;
    }
public:
    BSTree(){
        this->root=NULL;
    }
    BinNode* findMax(){
//        return rfindMax(this->root);
        BinNode* tmp=this->root;
        while(tmp && tmp->right){
            tmp=tmp->right;
        }
        return tmp;
    }
    BinNode* findMin(){
        BinNode* tmp=this->root;
        while(tmp && tmp->left){
            tmp=tmp->left;
        }
        return tmp;
    }
    BinNode* findX(Elem x){
        BinNode* tmp=this->root;
        while(tmp && x!=tmp->data){
            if(xdata) tmp=tmp->left;
            else    tmp=tmp->right;
        }
        return tmp;
    }
    bool insert(Elem x){
        try{
            this->root=rinsert(x,this->root);
        }
        catch(int e){
            return false;
        }
        return true;
    }
    bool remove(Elem x){
        try{
            this->root=remove(x,this->root);
        }
        catch(int e){
            return false;
        }
        return true;
    }
};
int main(){
    BSTree bt;
    bt.insert(10);
    bt.insert(5);
    bt.insert(20);
    bt.insert(8);
    bt.insert(15);
    bt.insert(2);
    bt.insert(6);
    bt.print();
    cout<<"---------"<

你可能感兴趣的:(【数据结构】)