zoukankan      html  css  js  c++  java
  • 二叉查找树

    性质:对于树中的每个节点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;
        }
        
    生活不止眼前的苟且 还有诗和远方的田野
  • 相关阅读:
    结对-结对编项目贪吃蛇-最终程序
    团队-团队编程项目中国象棋-模块测试过程
    团队-团队编程项目中国象棋-模块开发过程
    团队-团队编程项目中国象棋-项目进度
    结对-结对编项目贪吃蛇-测试过程
    结对-贪吃蛇-开发过程
    课后作业-阅读任务-阅读提问-2
    20171005-构建之法:现代软件工程-阅读笔记
    结队-结队编程项目贪吃蛇-项目进度
    课后作业-阅读任务-任务阅读-2
  • 原文地址:https://www.cnblogs.com/jc-nogame/p/5900340.html
Copyright © 2011-2022 走看看