AVL树(C语言实现)

截取了插入和删除部分

原文地址https://www.cnblogs.com/skywang12345/p/3576969.html

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


typedef struct tNode
{
    int data;
    struct tNode* lchild;
    struct tNode* rchild;
    int height;
} tNode, *AVLTree;


int MAX(int a, int b)
{
    if(a>b) return a;
    return b;
}

int HEIGHT(tNode *t)
{
    if(t == NULL) return -1;
    else return t->height;

}
tNode *avltree_maximum(tNode *t)
{

    if(t == NULL) return NULL;
    while(t->rchild!=NULL)
    {
        t = t->rchild;
    }
    return t;
}

tNode *avltree_minimum(tNode *t)
{
    if(t == NULL) return NULL;
    while(t->lchild!=NULL)
    {
        t = t->lchild;
    }
    return t;
}


tNode* left_left_rotation(tNode *t)
{
    tNode *p = t->lchild;
    t->lchild = p->rchild;
    p->rchild = t;
    t->height = MAX(HEIGHT(t->lchild),HEIGHT(t->rchild))+1;
    p->height = MAX(HEIGHT(p->lchild),t->height)+1;
    return p;
}

tNode* right_right_rotation(tNode *t)
{

    tNode *p = t->rchild;
    t->rchild = p->lchild;
    p->lchild = t;
    t->height = MAX(HEIGHT(t->lchild),HEIGHT(t->rchild))+1;
    p->height = MAX(HEIGHT(t),HEIGHT(p->rchild))+1;
    return p;
}

tNode* left_right_rotation(tNode *t)
{

    t->lchild = right_right_rotation(t->lchild);
    return left_left_rotation(t);
}

tNode* right_left_rotation(tNode *t)
{
    t->rchild = left_left_rotation(t->rchild);
    return right_right_rotation(t);
}


tNode *create_node(int data, tNode *left, tNode *right)
{
    tNode *p;
    p = (tNode *)malloc(sizeof(tNode));
    if(p == NULL)
    {
        printf("内存分配失败!\n");
        return NULL;
    }
    p->data = data;
    p->lchild = left;
    p->rchild = right;
    p->height = 0;
    return p;

}
tNode *avltree_insert(tNode *t, int data)
{

    if(t == NULL)
    {

        t = create_node(data, NULL, NULL);
        if(t == NULL)
        {
            printf("create avltree failed!\n");
            return NULL;
        }
        return t;
    }

    if(t->data > data)
    {
        t->lchild = avltree_insert(t->lchild, data);
        if(HEIGHT(t->lchild)-HEIGHT(t->rchild)==2)
        {
            if(data < t->lchild->data){
 //printf("%d 左左旋转\n",t->data);
                  t = left_left_rotation(t);
            }

            else {
     //               printf("%d 左右旋转\n",t->data);
                 t = left_right_rotation(t);
            }

        }
    }
    else if(t->data < data)
    {
        t->rchild = avltree_insert(t->rchild, data);
        if(HEIGHT(t->rchild)-HEIGHT(t->lchild)==2)
        {
            if(data > t->rchild->data)
            {
       //         printf("%d 右右旋转\n",t->data);
                t = right_right_rotation(t);
            }
            else
            {  //printf("%d 右左旋转\n",t->data);
                t = right_left_rotation(t);
            }
        }
    }
    else
    {
        printf("添加节点失败,该节点已经存在!\n");
        return NULL;
    }
    t->height = MAX(HEIGHT(t->lchild),HEIGHT(t->rchild))+1;
    return t;
}


tNode* delete_node(tNode *t,int data)
{

    if(t == NULL)
    {
        printf("找不到要被删除的元素!\n");
        return NULL;
    }
    if(data < t->data)
    {
        t->lchild = delete_node(t->lchild, data);
        if(HEIGHT(t->rchild)-HEIGHT(t->lchild)==2)
        {
            tNode *r = t->rchild;
            if(HEIGHT(r->lchild)>HEIGHT(r->rchild))
                t = right_left_rotation(t);
            else t = right_right_rotation(t);
        }
    }
    else if(data > t->data)
    {
        t->rchild = delete_node(t->rchild, data);
        if(HEIGHT(t->lchild)-HEIGHT(t->rchild)==2)
        {
            tNode *l = t->lchild;
            if(HEIGHT(l->rchild)>HEIGHT(l->lchild))
                t = left_right_rotation(t);
            else t = left_left_rotation(t);

        }
    }
    else
    {
        if(t->lchild&&t->rchild)
        {
            if(HEIGHT(t->lchild) > HEIGHT(t->rchild))
            {
                tNode *m = avltree_maximum(t->lchild);
                t->data = m->data;
                t->lchild = delete_node(t->lchild,m->data);
            }
            else
            {
                tNode *m = avltree_minimum(t->rchild);
                t->data = m->data;
                t->rchild = delete_node(t->rchild,m->data);
            }
        }
        else
        {
            tNode *tmp = t;
            t = t->lchild ? t->lchild : t->rchild;
            free(tmp);
        }
    }
    return t;
}

tNode* createAVL(int *data, int numNode)
{
    tNode *t = NULL;
    for(int i=0; i<numNode; i++)
    {
        t = avltree_insert(t, data[i]);
        //   printf("插入节点%d\n",data[i]);
    }

    return t;
}
/*
void print_tree(tNode *t, int h) {
    if(t) {

        print_tree(t->rchild, h+1);
        for(int i=0;idata);
        print_tree(t->lchild,h+1);
    }
}
*/
void print_avltree(tNode *tree,int data, int direction)
{
    if(tree != NULL)
    {
        if(direction==0)    // tree是根节点
            printf("%2d is root\n", tree->data);
        else                // tree是分支节点
            printf("%2d is %2d's %6s child\n", tree->data, data, direction==1?"right" : "left");

        print_avltree(tree->lchild, tree->data, -1);
        print_avltree(tree->rchild,tree->data,  1);
    }
}
int main()
{

    int arr[] = {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9};
    tNode *t;
    t = createAVL(arr, 16);
    //    print_tree(t,0);
    print_avltree(t,t->data,0);
    t = delete_node(t,11);
    print_avltree(t,t->data,0);
    return 0;
}


你可能感兴趣的:(数据结构)