AVL Tree

本文介绍了一种实现AVL树的C语言代码,详细展示了如何进行左旋、右旋以及平衡调整等操作,确保树的高度平衡。适用于对数据结构和算法感兴趣的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 [code]
bstree.c
#include<stdlib.h>
#include<stdio.h>
#include "bstree.h"

void R_Rotate(BSTNode **a)
{
    BSTNode *b;
    b=(*a)->left;
    (*a)->left=b->right;
    b->right=*a;
    *a=b;
}

void L_Rotate(BSTNode **a)
{
    BSTNode *b;
    b=(*a)->right;
    (*a)->right=b->left;
    b->left=*a;
    *a=b;
}

void LeftBalance(BSTNode **a)
{
    //add your code
    BSTNode *lc=(BSTNode *)malloc(sizeof(BSTNode));
    BSTNode *rd=(BSTNode *)malloc(sizeof(BSTNode));
    lc=(*a)->left;
    switch(lc->bf)
    {
        case 1:
            (*a)->bf=lc->bf=0;
            R_Rotate(a);
            break;
        case -1:
            rd=lc->right;
            switch(rd->bf)
            {
                case 1 :
                    (*a)->bf=-1;
                    lc->bf=0;
                    break;
                case 0 :
                    (*a)->bf=lc->bf=0;
                    break;
                case -1 :
                    (*a)->bf=0;
                    lc->bf=1;
                    break;
            }
            rd->bf=0;
            L_Rotate(&((*a)->left));
            R_Rotate(a);
            break;
    }
}

void RightBalance(BSTNode **a)
{
    BSTNode *rc=(BSTNode *)malloc(sizeof(BSTNode));
    BSTNode *ld=(BSTNode *)malloc(sizeof(BSTNode));
    rc=(*a)->right;
    switch(rc->bf)
    {
        case -1:
            (*a)->bf=rc->bf=0;
            L_Rotate(a);break;
        case 1 :
            ld=rc->left;
            switch(ld->bf)
            {
                case 1:
                    (*a)->bf=0;
                    rc->bf=-1;
                    break;
                case 0 :
                    (*a)->bf=rc->bf=0;
                    break;
                case -1 :
                    (*a)->bf=1;
                    rc->bf=0;
                    break;
            }
            ld->bf=0;
            R_Rotate(&((*a)->right));
            L_Rotate(a);
    }
}

int InsertAVL(BSTNode **base , DataType e,int taller)
{
    //add your code
    if(*base==NULL)
    {
        *base=(BSTNode *)malloc(sizeof(BSTNode));
        (*base)->left=(*base)->right=NULL;
        (*base)->data=e;
        (*base)->bf=0;
        taller=1;
    }
    else
    {
        if(e==(*base)->data)
        {
            taller=0;
            return 0;
        }
        else if(e<(*base)->data)
        {
            if(!InsertAVL(&((*base)->left),e,taller))
                return 0;
            if(taller)
            {
                switch((*base)->bf)
                {
                    case 1:
                        LeftBalance(base);
                        taller=0;
                        break;
                    case 0 :
                        (*base)->bf=1;
                        taller=1;
                        break;
                    case -1:
                        (*base)->bf=0;
                        taller=0;
                        break;
                }
            }
        }
        else
        {
            if(!InsertAVL(&((*base)->right),e,taller))
                return 0;
            if(taller)
            {
                switch((*base)->bf)
                {
                    case 1:
                        (*base)->bf=0;
                        taller=0;
                        break;
                    case 0:
                        (*base)->bf=-1;
                        taller=1;
                        break;
                    case -1:
                        RightBalance(base);
                        taller=0;
                        break;
                }
            }
        }
    }
    return 1;
}

int Delete(BSTNode **a , DataType key)
{
    //add your code
    if(*a==NULL)
        return 0;
    /*when find the key ,begin to delete it.*/
    if((*a)->data==key)
    {
        //to add your code
    }
}

bstree.h
typedef int DataType;
struct bstnode
{
    DataType data;
    int bf ;
    int visited;
    struct bstnode *left,*right;
};

typedef struct bstnode BSTNode;
void R_Rotate(BSTNode **a);
void L_Rotate(BSTNode **a);
void LeftBalance(BSTNode **a);
void RightBalance(BSTNode **a);
int InsertAVL(BSTNode **base,DataType key,int taller);
int Delete(BSTNode **a , DataType key);
[/code]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值