十乂
日行一事

性质:对于树中的每个节点X,它的左子树中的所有节点均小于X,它的右子树中的所有节点均大于X

    
class BST    
{
    public:  
        BST();
        ~BST();

        int findMin() const;
        int findMax() const;
        bool contains(int x) const;
        bool isEmpty() const;
        void printBST() const;
        void insert(int x);
        void remove(int x);

    private:
        struct BinaryNode
        {
            int value;
            BinaryNode *left;
            BinaryNode *right;
            BinaryNode( int x=0, BinaryNode *l=NULL, BinaryNode *r=NULL ):value(x),left(l),right(r) {}    
        };          

        BinaryNode* findMin(BinaryNode *bn) const;
        BinaryNode* findMax(BinaryNode *bn) const;
        bool contains(BinaryNode *bn, int x) const;
        void insert(BinaryNode *&bn, int x) const;
        void remove(BinaryNode *&bn, int x);    
        void printBST(BinaryNode *bn) const;
        
        BinaryNode *root;
};      

BST::BST()
{
    root = NULL;
}

BST::~BST()
{
}

bool BST::isEmpty() const
{
    if( root==NULL )
        return true;
    
    return false;
}

void BST::insert(int x) 
{
    if( root==NULL )
    {
        root = new BinaryNode();
        root->value = x;
    }
    else
    {
        insert(root, x);
    }
}

void BST::insert(BinaryNode * &bn, int x) const
{
    if( bn==NULL )
    {
        bn = new BinaryNode();
        bn->value = x;
    }
    else if( bn->value>x )
    {
        insert(bn->left, x);
    }
    else
    {
        insert(bn->right, x);
    }
}

void BST::printBST() const
{
    if( root!=NULL )
    {
        printBST(root);
    }
    
    cout << endl;
}

void BST::printBST(BinaryNode *bn) const
{
    cout << bn->value << " ";
    if( bn->left!=NULL )
        printBST(bn->left);
    if( bn->right!=NULL )
        printBST(bn->right);
}

int BST::findMin() const
{
    if( root!=NULL )
        return findMin(root)->value;
    
    return -1;
}

BST::BinaryNode* BST::findMin(BinaryNode *bn) const
{
    if( bn->left!=NULL )
        return findMin(bn->left);

    return bn;
}

int BST::findMax() const
{
    if( root!=NULL )
        return findMax(root)->value;
    
    return -1;
}

BST::BinaryNode* BST::findMax(BinaryNode *bn) const
{
    if( bn->right!=NULL )
        return findMax(bn->right);

    return bn;
}

void BST::remove(int x)
{
    if( root!=NULL )
        remove(root, x);
}

void BST::remove(BinaryNode* &bn, int x)
{
    if( bn==NULL )
    {
        cout << "not exist.." << endl;
    }
    if( bn->value<x )
    {    
        remove(bn->right, x);
    }
    else if( bn->value>x )
    {
        remove(bn->left, x);
    }
    else if( bn->left!=NULL && bn->right!=NULL )
    {
        int rMin = findMin( bn->right )->value;
        bn->value = rMin;
        remove( bn->right, rMin );
    }
    else
    {
        BinaryNode *old = bn;
        bn = bn->left!=NULL ? bn->left : bn->right;
        delete old;
    }
}

int main()
{
    BST bst;
    
    int vec[] = {3,1,7,4,0,2,6};
    
    for( int i=0;i<7;i++ )
    {
        bst.insert(vec[i]);
    }
    
    bst.printBST();
    
    cout << "min = "<< bst.findMin() << endl;
    cout << "max = "<< bst.findMax() << endl;
    
    bst.remove(1);
    bst.printBST();
    
    if( bst.isEmpty() )
    {
        cout << "bst is null..." << endl;
        return 0;
    }
    

 

posted on 2016-09-23 15:51  暂定  阅读(78)  评论(0编辑  收藏  举报