Binary sort tree, red-black trees and balanced binary tree

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/jiaomubai/article/details/102769394

Binary sort tree

Binary sort tree is also known as a binary search tree. It is either an empty tree, or a binary tree with the following properties:

(1) if its left subtree is not empty, then the value of the left sub-tree, all the nodes are less than the value of its root.

(2) if its right subtree is not empty, then the value of the right sub-tree, all the nodes are greater than the value of its root.

(3) its left and right subtrees are also binary sort tree.

Find a binary sort tree: the usual method using recursion to find, which is to find the root node, if the value of the root keywords are equal, then find, if the key is smaller than the value of the root node, then left subtree continue to look, if the key is larger than the value of the root node, look in the right subtree.

typedef struct BiTNode
{
    int data;                            //结点值
    struct BiTNode* lchild,*rchild;      //左右孩子指针
}BiTNode,*BiTree;
//查找:查找二叉排序树T中是否存在key
//指针f指向T的双亲,其初始值为NULL,若查找成功,则指针p指向该数据元素的结点,并返回TRUE,否则指针p指向查找路线上访问的最后一个结点并返回FALSE
bool SearchBST(BiTree T,int key,BiTree f,BiTree *p)
{
    //查找不成功
    if(!T)
    {
        *p = f;
        return false;
    }
    //查找成功
    else if(key == T->data)
    {
        *p = T;
        return true;
    }
    else if(key < T->data)
    {
        //如果key值小于当前根结点的值,则继续在左子树中查找
        return SearchBST(T->lchild,key,T,p);
    }
    else
    {
        //如果key值大于当前根结点的值,则继续在右子树中查找
        return SearchBST(T->rchild,key,T,p);
    }
}

 Insert binary sort tree: the so-called binary tree insertion, in fact, put the key in the tree appropriate position only.

//当二叉排序树T中不存在关键字等于key的数据时,插入key并返回TRUE,否则返回FALSE
bool InsertBST(BiTree* T,int key)
{
    BiTree p,s;
    //若查找不成功
    if(!SearchBST(*T,key,NULL,&p))
    {
        s = (BiTree)malloc(sizeof(BiTNode));
        s->data = key;
        s->lchild = NULL;
        s->rchild = NULL;
        if(!p)
        {
            *T = s;        //插入s为新的根结点
        }
        else if(key < p->data)
        {
            p->lchild = s;    //插入s为左孩子
        }
        else
        {
            p->rchild = s;    //插入s为右孩子
        }
        return true;
    }
    //若书中已有与关键字相同的结点,则插入失败
    else
    {
        return false;
    }
}

Delete binary sort tree: to delete the binary tree, the more issues to consider, as deleting the issues involved are complex and require three cases considered, namely to remove a leaf node, the node you want to delete only left subtree right subtree or only around the node you want to remove sub-tree has.

For the node to delete only the case where only the left or the right subtree of the subtrees, delete the nodes to its left or right subtree of the entire sub-tree is moved to the position of the deleted node can be understood as "inherited his father."

For the node you want to remove the left and right sub-tree situation there, the solution is too much trouble. A better approach is to first locate the node you want to remove the direct precursor p s, to replace the junction with p s, then delete this node s.

//删除
//若二叉排序树T中存在关键字等于key的数据元素,则删除该结点,并返回TRUE,否则返回FALSE
bool DeleteBST(BiTree* T,int key)
{
    //如果不存在关键字等于key的结点
    if(!*T)
    {
        return false;
    }
    else
    {
        if(key == (*T)->data)
        {
            return Delete(T);
        }
        //若关键字小于根结点的值
        else if(key < (*T)->data)
        {
            return DeleteBST(&(*T)->lchild,key);
        }
        else
        {
            return DeleteBST(&(*T)->rchild,key);
        }
    }
}
//从二叉排序树中删除结点p,并重新连接它的左右子树
bool Delete(BiTree* p)
{
    BiTree q,s;
    //如果右子树为空,则只需要重新连接它的左子树
    if((*p)->rchild == NULL)
    {
        q = *p;
        *p = (*p)->lchild;
        free(q);
    }
    //如果左子树为空,则只需要重新连接它的右子树
    if((*p)->lchild == NULL)
    {
        q = *p;
        *p = (*p)->rchild;
        free(q);
    }
    //左右子树均不空
    else
    {
        q = *p;
        s = (*p)->lchild;
        //转左,然后向右到尽头,找待删除结点的前驱
        while(s->rchild)
        {
            q = s;
            s = s->rchild;
        }
        (*p)->data = s->data;    //s指向被删除结点的直接前驱
        if(q != *p)
        {
            q->rchild = s->lchild;    //重新连接q的右子树
        }
        else
        {
            q->lchild = s->lchild;    //重新连接q的左子树
        }
        free(s);
    }
    return true;
}

Summary: binary sort tree is linked fashion store, maintaining the advantages of the chain storage structure without moving elements in the implementation of insert or delete operation, then as long as the proper insertion and deletion position is found, you can only modify the link pointer. Insertion and deletion time performance is better, but look for the binary sort tree, go is the path from the root to the node you want to find, which is equal to the number of comparisons of key nodes in the layer binary sort tree number, at least once, most no more than the depth of the binary tree. In other words, look for performance binary sort tree depends on binary sort tree shape. If the binary sort tree is relatively balanced, that is the same as its depth and complete binary tree, are \left \lfloor \log_{2}n \right \rfloor + 1, then find the time complexity is also O (logn), similar to binary search.

平衡二叉树(Self-Balancing Binary Search Tree 或 Height-Balanced Binary Search Tree)

Balanced binary tree is a binary sort tree, also known as an AVL tree, wherein the height difference between the left and the right subtree of the subtrees of each node of at most 1. As can be seen from the English name of the balanced binary tree, balanced binary tree is a highly balanced binary sort tree. Means balanced binary tree is either an empty tree, or its left subtree and right subtree are balanced binary tree, and the absolute value of the difference between the height of the left subtree and right subtree of not more than one. We left child node of the tree depth binary value minus the depth of the right subtree called balance factor, then all nodes on a balanced binary tree can only balance factor is -1, 0. That is, as long as a node on the balance factor is greater than the absolute value of a binary tree, the binary tree is unbalanced. The insertion distance of the nearest node, the balance factor is greater than the absolute value of the node is a root node of a subtree, called the minimum unbalanced tree.

The principle of balanced binary tree: the basic idea is to build a balanced binary tree in the process of building a binary sort tree, every time when you insert a node, first check by the insertion and destroy the balance of the tree, and if so, to find out the smallest imbalance tree. While maintaining the characteristics of binary sort tree, the relationship between the adjustment link node minimum unbalanced tree corresponding rotation, making new equilibrium subtree.

Balanced binary tree algorithm: on balanced binary tree, to note that right-handed and left-handed operation. The so-called L, the balance factor is that when a node is -2, the whole tree will be rotated counterclockwise. When the balance factor refers to a right-hand node is 2, the clockwise rotation of the entire tree.

//树的结点结构
typedef struct BiTNode
{
    int data;                            //结点值
    int bf;                              //平衡因子
    struct NiTNode *lchild,*rchild;      //左右孩子指针
}BiTNode,*BiTree;
//右旋
void Right_Rotate(BiTree* p)
{
    BiTree L;
    L = (*p)->lchild;        //L指向p的左子树根结点
    (*p)->lchild = L->rchild;    //L的右子树挂接为p的左子树
    L->rchild = (*p);
    *p = L;                //p指向新的根结点
}
//左旋
void Right_Rotate(BiTree* p)
{
    BiTree R;
    R = (*p)->rchild;        //R指向p的右子树根结点
    (*p)->rchild = R->lchild;    //R的左子树挂接为p的右子树
    R->lchild = (*p);
    *p = R;                //p指向新的根结点
}

Dextrose Code Interpretation: a when an incoming binary sort tree by P, and its left child node is defined as L,

 

 

 

Guess you like

Origin blog.csdn.net/jiaomubai/article/details/102769394