• 二叉查找树


    性质:对于树中的每个节点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;
        }
        
    生活不止眼前的苟且 还有诗和远方的田野
  • 相关阅读:
    AjaxHelper使用范例
    IE浏览器的条件编译指令和微软的ajax实现
    基于AjaxHelper0.41的相册范例程序演示及一个关于博客园的功能建议
    AjaxHelper 0.41
    .Net环境下基于Ajax的MVC方案
    Msys/MinGW与Cygwin/gcc[转]
    常见应用程序的架构
    perl正则表达式[转]
    [CruiseControl] 概念
    MinGW
  • 原文地址:https://www.cnblogs.com/jc-nogame/p/5900340.html
Copyright © 2020-2023  润新知