我是正常蛇

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

What is it?

If a binary tree who's node's value in left child tree is all smaller than root and the node's value in right child tree is all greater than root , and it also holds for any subtree in this tree, then this is a binary search tree.

If we perform an inorder tree walk of T , which is like leftchild -> root -> rightchild. We can get a sorted array.

 

Operations:

Search   

search(t,x)
{
    if(t.value = x) return t;
    if(t.value > x)
        return search(t.left,x);
    else
        return search(t.right,x);
}    

Minimum & Maximun 

Since in binary search tree , the minimum element is always in the most left slot. So we can do visit t.left recurrently untill it is null.

Predecessor & Successor 

Findding successor is kind of trikky. Separate it into 2 cases:

case if T.right is not null : the successor of T is Minimum(T.right).

case if T.right is null : recurrently visit the parent node of T, if the current T is the left child of P(T) , then P(T) is what we looking for.

Successor(T)
{
    if(T.right != null)
        return Minimum(T.right);
    p = T.parent;
    while(p != null && p.right = T)
    {
        T = p;
        p = T.parent;
    }
    return p;
}

Insert

It's like build the tree, easy to do.Bounch of comparisions.

Delete

This is the most tricky part. Assume the node we want to delete is z.Then we separate the problem into 3 cases.

case z got no child : just cut.

case z got one child : assume the p is z's child , then let p.parent = z.parent .

case z got two children: assume the p is z's successor , x is the right child of p (p got no left child). Then we delete y as what we do in the case z got one child, and replace z.value with y.value.

 

All the operation's running time is O(lgn) 

 

Ramdom Search Tree:

Select the root node for each subtree ramdomize then we got a random search tree.

Performance: T(n) is the expect height of a ramdom search tree. T(n) = O(lgn) 

 

 

Downhere are codes to handle some of these operations by C, these codes suck cause I didn't think enough.

#include <stdio.h>
#include <malloc.h>

typedef struct BST{
    struct BST* parent;
    struct BST* lchild;
    struct BST* rchild;
    int value;
}*PBST,BST;

int BSTInsert(PBST T,int value)
{
    if(T->value == -1)
    {
        T->value = value;
        return 1;
    }
    PBST x = (PBST)malloc(sizeof(BST));
    *x = *T;
    PBST ptx = (PBST)malloc(sizeof(BST));
    ptx = T;
    PBST slot = (PBST)malloc(sizeof(BST));
    while(x)
    {
        if(x->value > value)
        {
            if(x->lchild != NULL)
            {
                ptx = x->lchild;
                *x = *x->lchild;
            }
            else
            {
                ptx->lchild = (PBST)malloc(sizeof(BST));
                ptx->lchild->value = value;
                ptx->lchild->parent = ptx;
                break;
            }
        }
        else
        {
            if(x->rchild != NULL)
            {
                ptx = x->rchild;
                *x = *x->rchild;
            }
            else
            {
                ptx->rchild = (PBST)malloc(sizeof(BST));
                ptx->rchild->parent = ptx;
                ptx->rchild->value = value;
                break;
            }
        }
    }
    return 1;
}

int travel(PBST T)
{
    if(T != NULL)
    {
        travel(T->lchild);
        printf("%d\t",T->value);
        travel(T->rchild);
    }
}

PBST BSTSearch(PBST T,int value)
{
    if(T == NULL) return NULL;
    if(T->value == value) return T;
    if(T->value > value)  
        BSTSearch(T->lchild,value);
    else 
        BSTSearch(T->rchild,value);
}

int BSTDelete(PBST B,int value)
{
    PBST T = BSTSearch(B,value);
    if(T->lchild == NULL && T->rchild == NULL)
    {
        if(T->parent->lchild ==T)
            T->parent->lchild = NULL;
        else
            T->parent->rchild = NULL;
    }
    else if(T->lchild == NULL || T->rchild == NULL)
    {
        if(T->parent == NULL)
        {
            if(T->lchild != NULL)
            {
                T->value = T->lchild->value;
                if(T->lchild->rchild != NULL)
                  T->rchild = T->lchild->rchild;
                else
                  T->rchild = NULL;
                if(T->lchild->lchild != NULL)
                  T->lchild = T->lchild->lchild;
                else
                  T->lchild = NULL;
            }
            if(T->rchild != NULL)
            {
                T->value = T->rchild->value;
                if(T->rchild->lchild != NULL)
                  T->lchild = T->rchild->lchild;
                else
                  T->lchild = NULL;
                if(T->rchild->rchild != NULL)
                  T->rchild = T->rchild->rchild;
                else
                  T->rchild = NULL;
                
            }
        }
        else
        {
            PBST child = (PBST)malloc(sizeof(BST));
            if(T->lchild != NULL)
                child = T->lchild;
            else
                child = T->rchild;
            if(T->parent->lchild = T)
            {
                T->parent->lchild = child;
                child->parent = T->parent;
            }
            else
            {
                T->parent->rchild = child;
                child->parent = T->parent;
            }
        }
    }
    else
    {
        PBST successor = (PBST)malloc(sizeof(PBST));
        PBST mini = (PBST)malloc(sizeof(PBST));
        *mini = *T->rchild;
        while(mini->lchild != NULL)
        {
            mini = mini->lchild;
        }
        successor = mini;
        if(successor->parent = T)
        {
            if(successor->rchild != NULL)
            {
                successor->rchild->parent = successor->parent;
                successor->parent->rchild = successor->rchild;
            }
            else
            {
                successor->parent->rchild = NULL;
            }
        }
        else if(successor->rchild != NULL)
        {
            successor->rchild->parent = successor->parent;
            successor->parent->lchild = successor->rchild;
        }
        else
        {
            successor->parent->lchild = NULL;
        }
        T->value = successor->value;
    }
}

int test(PBST T)
{
    T->lchild = NULL;
}

int showMain()
{
    system("clear");
    printf("===========================the Binary Search Tree Test==========================\n");
    printf("1: build a BST \n");
    printf("2: insert a value to the BST you have built before\n");
    printf("3: travel the tree in medium order\n");
    printf("4: delete a tree by specifyin the value\n");
    printf("5: check if a specified value already exists\n");
    printf("0: exit.\n");
    printf("input:");
    return 1;
}

void main()
{
    PBST T = (PBST)malloc(sizeof(BST));
    int commend = 99;
    showMain();
    while(commend != 0)
    {
        scanf("%d",&commend);
        if(commend == 1)
        {
            T->value = -1;
            T->lchild = NULL;
            T->rchild = NULL;
            system("clear");
            printf("A new Tree has built, input some numbers to init the tree. 0 to exit \n");
            int node = 1;
            while(1)
            {
                scanf("%d",&node);
                if(node == 0)
                  break;
                BSTInsert(T,node);
            }
            showMain();
        }
        else if(commend == 2)
        {
            int node = 1;
            system("clear");
            printf("Input a number and system will autoly goto the main board,0 to exit \n");
            scanf("%d",&node);
            if(node != 0)
            {
                BSTInsert(T,node);
                showMain();
            }
            else
            {
                showMain();
            }
        }
        else if(commend == 3)
        {
            system("clear");
            printf("The following the now the tree traveled by left -> root -> right order : \n");
            travel(T);
            int node = 1;
            while(node != 0)
            {
                scanf("%d",&node);
            }
            showMain();
        }
        else if(commend == 4)
        {
            system("clear");
            printf("input the value you wanna delete\n");
            int node = 1;
            scanf("%d",&node);
            if(node != 0)
            {
                BSTDelete(T,node);
                printf("delete successfully!!!press any key to continue!!\n");
                scanf("%d",&node);
                system("clear");
                showMain();
            }
            else
            {
                showMain();
            }
        }
        else if(commend == 5)
        {
            system("clear");
            printf("Input the value to check it out \n");
            int node = 1;
            scanf("%d",&node);
            if(node != 0)
            {
                if(BSTSearch(T,node) == NULL)
                {
                    printf("the value is not existed!!");
                    scanf("%d",&node);
                    system("clear");
                    showMain();
                }
                else
                {
                    printf("the value is existed!!");
                    scanf("%d",&node);
                    system("clear");
                    showMain();
                }
            }
            else
            {
                system("clear");
                showMain();
            }
        }
        else if(commend == 7)
        {
            test(T);
            travel(T);
        }
        else if(commend == 0)
        {
            printf("exit!");
        }
        else
        {
            printf("invalid input");
        }
    }

}

 

 

posted on 2012-09-06 16:45  我是正常蛇  阅读(335)  评论(0编辑  收藏  举报