C语言实现 AVL树

97 阅读4分钟

 

最下面有整体实现代码(放一起的)

AVL树

1.定义

typedef int ElementType;
typedef struct AVLNode* Position;
typedef Position AVLTree; /* AVL树类型 */
struct AVLNode {
    ElementType Data; /* 结点数据 */
    AVLTree Left;     /* 指向左子树 */
    AVLTree Right;    /* 指向右子树 */
    int Height;       /* 树高 */
};

2.获得高度

int Max(int a, int b)
{
    return a > b ? a : b;
}
int GetHeight(AVLNode* node)
{
    if (node == NULL)
        return -1;
​
    int leftchildheight = GetHeight(node->Left);
    int rightchildheight = GetHeight(node->Right);
    return (leftchildheight > rightchildheight ? leftchildheight : rightchildheight) + 1;
}

3.左单旋

AVLTree SingleLeftRotation(AVLTree A)
{ /* 注意:A必须有一个左子结点B */
  /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */
​
    AVLTree B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
    B->Height = Max(GetHeight(B->Left), A->Height) + 1;
​
    return B;
}

4.右单旋

AVLTree SingleRightRotation(AVLTree A) {
    /* 注意:A必须有一个右子结点B */
  /* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */
​
    AVLTree B = A->Right;
    A->Right = B->Left;
    B->Left = A;
    A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
    B->Height = Max(GetHeight(B->Right), A->Height) + 1;
    
    return B;
}

5.左—右双旋

AVLTree DoubleLeftRightRotation(AVLTree A)
{ /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
  /* 将A、B与C做两次单旋,返回新的根结点C *//* 将B与C做右单旋,C被返回 */
    A->Left = SingleRightRotation(A->Left);
    /* 将A与C做左单旋,C被返回 */
    return SingleLeftRotation(A);
}

6.右—左双旋

AVLTree DoubleRightLeftRotation(AVLTree A)
{/* 注意:A必须有一个右子结点B,且B必须有一个左子结点C */
  /* 将A、B与C做两次单旋,返回新的根结点C */
    /*将B与C做左单旋,C被返回*/
    A->Right = SingleLeftRotation(A->Right);
    /*将A与C做右单旋,C被返回*/
    return SingleRightRotation(A);
}

7.插入并调整

AVLTree Insert(AVLTree T, ElementType X)
{ /* 将X插入AVL树T中,并且返回调整后的AVL树 */
    if (!T) { /* 若插入空树,则新建包含一个结点的树 */
        T = (AVLTree)malloc(sizeof(struct AVLNode));
        T->Data = X;
        T->Height = 0;
        T->Left = T->Right = NULL;
    } /* if (插入空树) 结束 */
​
    else if (X < T->Data) {
        /* 插入T的左子树 */
        T->Left = Insert(T->Left, X);
        /* 如果需要左旋 */
        if (GetHeight(T->Left) - GetHeight(T->Right) == 2)
            if (X < T->Left->Data)
                T = SingleLeftRotation(T);      /* 左单旋 */
            else
                T = DoubleLeftRightRotation(T); /* 左-右双旋 */
    } /* else if (插入左子树) 结束 */
​
    else if (X > T->Data) {
        /* 插入T的右子树 */
        T->Right = Insert(T->Right, X);
        /* 如果需要右旋 */
        if (GetHeight(T->Left) - GetHeight(T->Right) == -2)
            if (X > T->Right->Data)
                T = SingleRightRotation(T);     /* 右单旋 */
            else
                T = DoubleRightLeftRotation(T); /* 右-左双旋 */
    } /* else if (插入右子树) 结束 *//* else X == T->Data,无须插入 *//* 别忘了更新树高 */
    T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;
​
    return T;
}
​

调整:

Node* maintain(Node* cur) {
        if (cur == nullptr) {
            return nullptr;
        }
        int leftHeight = cur->_left != nullptr ? cur->_left->h : 0;//计算出cur左树的高度
        int rightHeight = cur->_right != nullptr ? cur->_right->h : 0;//计算出cur右树的高度
        if (abs(leftHeight - rightHeight) > 1) {//出现不平衡
            if (leftHeight > rightHeight) {//如果是左树高
                //把左树的左右子树的高度来出来比较看到底是左边高还是右边高
                int leftLeftHeight = cur->_left != nullptr && cur->_left->_left != nullptr ? cur->_left->_left->h : 0;
                int leftRightHeight = cur->_left != nullptr && cur->_left->_right != nullptr ? cur->_left->_right->h : 0;
                if (leftLeftHeight >= leftRightHeight) {//注意想等时只能右旋
                    cur = rightRotate(cur);
                }
                else {//左右双旋
                    cur->_left = leftRotate(cur->_left);
                    cur = rightRotate(cur);
                }
            }
            else {
                int rightLeftHeight = cur->_right != nullptr && cur->_right->_left != nullptr ? cur->_right->_left->h: 0;
                int rightRightHeight = cur->_right != nullptr && cur->_right->_right != nullptr ? cur->_right->_right->h : 0;
                if (rightRightHeight >= rightLeftHeight) {
                    cur = leftRotate(cur);
                }
                else {//右左双旋
                    cur->_right = rightRotate(cur->_right);
                    cur = leftRotate(cur);
                }
            }
        }
        return cur;//返回调整好的新头
    }

无删除的整体代码:

// AVL树测试.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
using namespace std;
typedef int ElementType;
typedef struct AVLNode* Position;
typedef Position AVLTree; /* AVL树类型 */
struct AVLNode {
    ElementType Data; /* 结点数据 */
    AVLTree Left;     /* 指向左子树 */
    AVLTree Right;    /* 指向右子树 */
    int Height;       /* 树高 */
};
int Max(int a, int b)
{
    return a > b ? a : b;
}
int GetHeight(AVLNode* node)
{
    if (node == NULL)
        return -1;
​
    int leftchildheight = GetHeight(node->Left);
    int rightchildheight = GetHeight(node->Right);
    return (leftchildheight > rightchildheight ? leftchildheight : rightchildheight) + 1;
}
AVLTree SingleLeftRotation(AVLTree A)
{ /* 注意:A必须有一个左子结点B */
  /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */
​
    AVLTree B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
    B->Height = Max(GetHeight(B->Left), A->Height) + 1;
​
    return B;
}
AVLTree SingleRightRotation(AVLTree A) {
    /* 注意:A必须有一个右子结点B */
  /* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */
​
    AVLTree B = A->Right;
    A->Right = B->Left;
    B->Left = A;
    A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
    B->Height = Max(GetHeight(B->Right), A->Height) + 1;
​
    return B;
}
AVLTree DoubleLeftRightRotation(AVLTree A)
{ /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
  /* 将A、B与C做两次单旋,返回新的根结点C *//* 将B与C做右单旋,C被返回 */
    A->Left = SingleRightRotation(A->Left);
    /* 将A与C做左单旋,C被返回 */
    return SingleLeftRotation(A);
}
AVLTree DoubleRightLeftRotation(AVLTree A)
{/* 注意:A必须有一个右子结点B,且B必须有一个左子结点C */
  /* 将A、B与C做两次单旋,返回新的根结点C */
    /*将B与C做左单旋,C被返回*/
    A->Right = SingleLeftRotation(A->Right);
    /*将A与C做右单旋,C被返回*/
    return SingleRightRotation(A);
}
AVLTree Insert(AVLTree T, ElementType X)
{ /* 将X插入AVL树T中,并且返回调整后的AVL树 */
    if (!T) { /* 若插入空树,则新建包含一个结点的树 */
        T = (AVLTree)malloc(sizeof(struct AVLNode));
        T->Data = X;
        T->Height = 0;
        T->Left = T->Right = NULL;
    } /* if (插入空树) 结束 */
​
    else if (X < T->Data) {
        /* 插入T的左子树 */
        T->Left = Insert(T->Left, X);
        /* 如果需要左旋 */
        if (GetHeight(T->Left) - GetHeight(T->Right) == 2)
            if (X < T->Left->Data)
                T = SingleLeftRotation(T);      /* 左单旋 */
            else
                T = DoubleLeftRightRotation(T); /* 左-右双旋 */
    } /* else if (插入左子树) 结束 */
​
    else if (X > T->Data) {
        /* 插入T的右子树 */
        T->Right = Insert(T->Right, X);
        /* 如果需要右旋 */
        if (GetHeight(T->Left) - GetHeight(T->Right) == -2)
            if (X > T->Right->Data)
                T = SingleRightRotation(T);     /* 右单旋 */
            else
                T = DoubleRightLeftRotation(T); /* 右-左双旋 */
    } /* else if (插入右子树) 结束 *//* else X == T->Data,无须插入 *//* 别忘了更新树高 */
    T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;
​
    return T;
}
int main()
{
    int N;
    cin >> N;
    AVLTree ret=NULL;
    while (N--)
    {
        int x;
        cin >> x;
        ret = Insert(ret, x);
    }
    printf("%d", ret->Data);
}