• 【算法导论】二叉搜索树


    二叉搜索树的基本操作与树的高度成正比,而我们并不能保证随机地构造二叉树,所以又有了较稳定的二叉树的变体,比如红黑树、B树、B+树、平衡树等。二叉搜索树则是理解其他变体的基础,所以想对它基本操作进行一次系统的学习了。

    二叉搜索树的概念:其左子结点小于或等于该节点,其右结点大于或等于该节点。

    祖先的概念:

    二叉搜索树树的遍历:

    中序遍历:先输出左子树关键字,然后输出该结点关键字,最后输出右结点关键字。

    先序遍历:先输出该结点关键字,然后输出左子树关键字,最后输出右结点关键字。

    后续遍历:先输出左结点关键字,然后输出右节点关键字,最后输出该结点关键字。

    中序遍历实现代码

    template<typename T>
    class binarySearchNode
    {
    public:
        binarySearchNode(T value,binarySearchNode<T> * leftBinarySearchNode=null,binarySearchNode<T>* rightBinarySearchNode=null, binarySearchNode<T>* father=null){
            this.value=value;
            this.leftBinarySearchNode=leftBinarySearchNode;
            this.rightBinarySearchNode=rightBinarySearchNode;
            this.father=father;
        }
        ~binarySearchNode();
    
        /* data */
    private:
        T value;
        binarySearchNode<T>* leftBinarySearchNode;
        binarySearchNode<T>* rightBinarySearchNode;
        binarySearchNode<T>* father;    
    };
    
    template<typename T>
    class binarySearchTree
    {
    public:
        binarySearchTree(binarySearchNode<T>* head){
            this.head=head;
        };
        ~binarySearchTree();
    
        /* data */
    
    public:
        std::vector<T> inorderTreeWalk(binarySearchNode* x);
        binarySearchNode* treeSearch(binarySearchNode* k);
        binarySearchNode* treeMinmum();
        binarySearchNode* treeMaxmum();
        binarySearchNode* treeSuccessor(binarySearchNode* x);
        binarySearchNode* treePredecessor(binarySearchNode* x);
        void treeInsert(binarySearchNode* x);
        void treeDelete(binarySearchNode* x);
        friend void transplant(binarySearchNode* u, binarySearchNode* v);
    private:
        binarySearchNode<T>* head;
    
    };
    
    T* binarySearchTree::inorderTreeWalk(){
    
    }
    
    binarySearchNode* binarySearchTree::treeMinmum(){
        binarySearchNode* cur=head;
        if(cur->leftBinarySearchNode!=null){
            cur=cur->leftBinarySearchNode;
        }
        return cur;
    }
    binarySearchNode* binarySearchTree::treeMaxmum(){
        binarySearchNode* cur=head;
        if(cur->rightBinarySearchNode!=null){
            cur=cur->rightBinarySearchNode;
        }
        return cur;
    }
    binarySearchNode* binarySearchTree::treeSuccessor(binarySearchNode* x){
    
        if(x->rightBinarySearchNode!=null){
            return treeMinmum(x->rightBinarySearchNode);
        }
        binarySearchNode* y=x->father;
    
        while(y!=null&&y==x->rightBinarySearchNode){
            x=y;
            y=x->father;
        }
        return y;
    }
    binarySearchNode* binarySearchTree::treePredecessor(){
        if(x->leftBinarySearchNode!=null){
            return treeMaxmum(x->leftBinarySearchNode);
        }
        binarySearchNode* y=x->father;
        while(y!=null&&x==y.leftBinarySearchNode){
            x=y;
            y=x->father;
        }
        return y;
    }
    std::vector<T> binarySearchTree::inorderTreeWalk(binarySearchNode* x){
        std::vector<T> result;
    
        if(x!=null){
            inorderTreeWalk(x->leftBinarySearchNode);
            result.push_back(x->value);
            inorderTreeWalk(x->rightBinarySearchNode);
        }
    }
    binarySearchNode* binarySearchTree::treeSearch(binarySearchNode* cur,T k){
        if(k==cur->data){
            return cur;
        }else if(k<cur->data{
            return treeSearch(cur->leftBinarySearchNode,T k);
        }else{
            return treeSearch(cur->rightBinarySearchNode,T k);
        }
    }
    
    void binarySearchTree::treeInsert(binarySearchNode* k){
        binarySearchNode* cur=head;
    
        while(cur!=null){
            if(k->data<cur->data){
                cur=cur->leftBinarySearchNode;
            }else{
                cur=cur->rightBinarySearchNode;
            }
        }
    
        k->father=cur;
    
        if(cur==null){
            this->head=cur;
        }else if(k->data>cur->value){
            cur->rightBinarySearchNode=k;
        }else{
            cur->leftBinarySearchNode=k;
        }
    }
    
    void binarySearchTree::treeDelete(binarySearchNode* x){
        if(x->leftBinarySearchNode==null)
            transplant(x,x->rightBinarySearchNode);
        elseif(x->rightBinarySearchNode==null){
            transplant(x,x->leftBinarySearchNode);
        }else{
            binarySearchNode* rChild=x->rightBinarySearchNode;
            if(rChild->leftBinarySearchNode==null){
                transplant(x,rChild);
                rChild->leftBinarySearchNode=x->leftBinarySearchNode;
                x->leftBinarySearchNode->father=rChild;
            }else{
                binarySearchNode* cur=treeMinmum(x->leftBinarySearchNode);
                if(cur->leftBinarySearchNode!=null)
                    cur->leftBinarySearchNode->father=cur->father;
                transplant(x,cur);
                cur->leftBinarySearchNode=x->leftBinarySearchNode;
                x->leftBinarySearchNode->father=cur;
            }
        }
    }
    friend void transplant(binarySearchNode* u, binarySearchNode* v){
        if(u->father==null)
            v->father=head;
        else if(u=u->father->leftBinarySearchNode)
            u->father->leftBinarySearchNode=v;
        else
            u->father->rightBinarySearchNode=v;
    
        if(v!=null)
            v->father=u->father;
    }

    12.2-6 如果二叉搜索树T的一个结点x的右子树为空,且x有一个后继y,那么y一定是x最底层的祖先,并且其左孩子也是x的祖先。

  • 相关阅读:
    20190912
    20190825
    20190818
    20190811
    20190804
    数据结构与算法之顺序表
    数据结构与算法之顺序表
    数据结构与算法之单链表
    数据结构与算法之循环双链表
    数据结构与算法之循环单链表
  • 原文地址:https://www.cnblogs.com/sansan/p/3595788.html
Copyright © 2020-2023  润新知