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;
        }
        
    生活不止眼前的苟且 还有诗和远方的田野
  • 相关阅读:
    React 项目 ant design 的 CheckboxGroup 验证
    React 项目中修改 Ant Design 的默认样式(Input Checkbox 等等
    create-react-app 构建的项目使用释放配置文件 webpack 等等 运行 npm run eject 报错
    使用 nodejs 和 axios 以及 cherrio 爬取天气预报
    ant design Radio.Group defaultValue 默认选中没生效
    macOS 更新 git 命令提示 xcrun,.gitignore 配置不生效问题。
    mac 绑定阿里企业邮箱
    create-react-app 构建的项目使用 mobx (说到底就是为了使用装饰器语法对 babel 做些配置
    React 项目使用 React-router-dom 4.0 以上版本时使用 HashRouter 怎么控制 history
    js 操作css
  • 原文地址:https://www.cnblogs.com/jc-nogame/p/5900340.html
Copyright © 2011-2022 走看看