纯C语言实现的AVL树(插入&删除&前序遍历输出)

突发奇想的敲了一遍,主要是想加深一下理解,毕竟AVL还是很重要的。
敲完了就想着发上来方便以后考前看看……..
顺便推荐一下这位大神的文章http://www.cnblogs.com/skywang12345/p/3576969.html

#include <stdio.h>
#include <stdlib.h>

#define MAX(a,b) ((a)>(b)?(a):(b)) //比较大小 
#define Height(p) ((p==NULL)?0:(((Tree)(p))->height)) // 求高度 

typedef struct Node* Tree;

struct Node
{
    int Key;
    int height;
    Tree Left;
    Tree Right;
};

Tree nodeCreate(int Key) //创建节点 
{
    Tree p = (Tree)malloc(sizeof(struct Node));
    if(p == NULL)return p;
    p->height = 0;
    p->Key = Key;
    p->Left = NULL;
    p->Right = NULL;
    return p;
}

Tree lL(Tree k2) 
{
    Tree k1 = k2->Left;
    k2->Left = k1->Right;
    k1->Right = k2;
    k2->height = MAX(Height(k2->Left) , Height(k2->Right))+1;
    k1->height = MAX(Height(k1->Left) , Height(k1->Right))+1;

    return k1;
}

Tree rR(Tree k1)
{
    Tree k2 = k1->Right;
    k1->Right = k2->Left;
    k2->Left = k1;
    k1->height = MAX(Height(k1->Left) , Height(k1->Right))+1;
    k2->height = MAX(Height(k2->Left) , Height(k2->Right))+1;

    return k2;
}

Tree lR(Tree k3)
{
    k3->Left = rR(k3->Left);
    return lL(k3);
}

Tree rL(Tree k3)
{
    k3->Right = lL(k3->Right);
    return rR(k3);
}

Tree nodeInsert(Tree root,int data) //插入 
{
    if(root == NULL)
    {
        root = nodeCreate(data);
        if(root == NULL)
        {
            printf("ERROR of the memory!!!\n");
            return NULL;
        }
    }
    else if(root->Key == data)
    {
        printf("ERROR of the data!!!\n");
    }
    else if(root->Key > data)
    {
        root->Left = nodeInsert(root->Left,data);
        if(Height(root->Left) - Height(root->Right) == 2)
        {
            if(data < root->Left->Key)
            {
                root = lL(root);
            }
            else if(data >= root->Left->Key)
            {
                root = lR(root);
            }
        }
    }
    else if(root->Key < data)
    {
        root->Right = nodeInsert(root->Right,data);
        if(Height(root->Right) - Height(root->Left) == 2)
        {
            if(data > root->Right->Key)
            {
                root = rR(root);
            }
            else if(data <= root->Right->Key)
            {
                root = rL(root);
            }
        }
    }

    root->height = MAX( Height(root->Left) , Height(root->Right) )+1;

    return root;
}

Tree nodeDelete(Tree root,int data) //删除 
{
    if(root == NULL)
    {
        printf("Don`t have this data!!!\n");
        return NULL;
    }

    if(data < root->Key)
    {
        root->Left = nodeDelete(root->Left,data);
        if(Height(root->Right) - Height(root->Left) == 2)
        {
            if(Height(root->Right->Left) > Height(root->Right->Right))
            {
                root = rL(root);
            }
            else root = rR(root);
        }
    }
    else if(data > root->Key)
    {
        root->Right = nodeDelete(root->Right,data);
        if(Height(root->Left) - Height(root->Right) == 2)
        {
            if(Height(root->Left->Left) > Height(root->Left->Right))
            {
                root = lL(root);
            }
            else root = lR(root);
        }
    }
    else if(data == root->Key) 
    {
        if(root->Left && root->Right)
        {
            if(Height(root->Left) > Height(root->Right))
            {
                Tree max = root->Left;
                Tree tr = root;
                int flag = 1;
                while(max->Right)
                {
                    if(flag)
                    {
                        flag = 0;
                        tr = tr->Left;
                    }
                    else tr = tr->Right;
                    max = max->Right;
                }
                root->Key = max->Key;
                if(flag)tr->Left = NULL;
                else tr->Right = NULL;
                free(max);
            }
            else
            {
                Tree max = root->Right;
                Tree tr = root;
                int flag = 1;
                while(max->Left)
                {
                    if(flag)
                    {
                        flag = 0;
                        tr = tr->Right;
                    }
                    else tr = tr->Left;
                    max = max->Left;
                }
                root->Key = max->Key;
                if(flag)tr->Right = NULL;
                else tr->Left = NULL;
                free(max);
            }
        }
        else
        {
            Tree t = root;
            root = (root->Left ? root->Left : root->Right);
            free(t);
        }
    }

    return root;
}

void preorderPrintf(Tree tree) // 前序遍历 
{
    if(tree != NULL)
    {
        printf("%d ", tree->Key);
        preorderPrintf(tree->Left);
        preorderPrintf(tree->Right);
    }
}

int main()
{
    Tree root = NULL;

    int N;
    scanf("%d",&N);
    while(N--)
    {
        int mid;
        scanf("%d",&mid);
        root = nodeInsert(root,mid);
    }

    int M;
    scanf("%d",&M);
    while(M--)
    {
        int mid;
        scanf("%d",&mid);
        root = nodeDelete(root,mid);
    }

    preorderPrintf(root);

    return 0;
}
posted @ 2017-11-06 22:08  Assassin_poi君  阅读(366)  评论(0编辑  收藏  举报