数据结构 AVL树

AVL 树是一种自平衡的二叉搜索树,它在每次插入或删除节点时通过旋转操作来保持树的平衡。

介绍

在 AVL 树中,每个节点都有一个平衡因子(Balance Factor),它表示该节点的左子树高度和右子树高度之差。平衡因子可以是 -1、0 或 1。当插入或删除节点导致某个节点的平衡因子超出范围时,就需要进行旋转操作来恢复树的平衡

AVL 树支持常见的二叉搜索树操作,例如插入、删除和搜索。在执行这些操作时,AVL 树会根据节点的平衡因子进行自平衡调整。

特性

对于任意节点,其左子树和右子树的高度差不超过 1。
AVL 树中每个节点的左子树和右子树都是 AVL 树。
在 AVL 树中,查找、插入和删除操作的时间复杂度都是 O(log n),其中 n 是树中节点的数量。

旋转操作

AVL 树的平衡通过四种旋转操作来实现。
左旋转(Left Rotation):当某个节点的右子树高度较高时,通过左旋转来降低右子树的高度。
右旋转(Right Rotation):当某个节点的左子树高度较高时,通过右旋转来降低左子树的高度。
左右旋转(Left-Right Rotation):当某个节点的左子树的右子树高度较高时,通过先对左子树进行左旋转,再对根节点进行右旋转来调整树的平衡。
右左旋转(Right-Left Rotation):当某个节点的右子树的左子树高度较高时,通过先对右子树进行右旋转,再对根节点进行左旋转来调整树的平衡。
通过这些旋转操作,AVL 树可以保持平衡,避免出现不平衡的情况,确保树的高度始终保持在较小的范围内。

使用场景

AVL 树是一种高效的数据结构,适用于需要频繁插入、删除和搜索操作的场景,并且对于这些操作的时间复杂度有严格要求的情况。

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

// AVL 树节点结构
struct Node {
    int key;
    struct Node* left;
    struct Node* right;
    int height;
};

// 获取节点的高度
int getHeight(struct Node* node) {
    if (node == NULL)
        return 0;
    return node->height;
}

// 获取两个数中的较大值
int max(int a, int b) {
    return (a > b) ? a : b;
}

// 创建一个新节点
struct Node* newNode(int key) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->key = key;
    node->left = NULL;
    node->right = NULL;
    node->height = 1; // 新节点的高度为 1
    return node;
}

// 执行右旋转操作
struct Node* rightRotate(struct Node* y) {
    struct Node* x = y->left;
    struct Node* T2 = x->right;

    // 执行旋转
    x->right = y;
    y->left = T2;

    // 更新节点的高度
    y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
    x->height = max(getHeight(x->left), getHeight(x->right)) + 1;

    return x;
}

// 执行左旋转操作
struct Node* leftRotate(struct Node* x) {
    struct Node* y = x->right;
    struct Node* T2 = y->left;

    // 执行旋转
    y->left = x;
    x->right = T2;

    // 更新节点的高度
    x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
    y->height = max(getHeight(y->left), getHeight(y->right)) + 1;

    return y;
}

// 获取节点的平衡因子
int getBalanceFactor(struct Node* node) {
    if (node == NULL)
        return 0;
    return getHeight(node->left) - getHeight(node->right);
}

// 插入节点到 AVL 树
struct Node* insertNode(struct Node* node, int key) {
    // 执行标准的 BST 插入操作
    if (node == NULL)
        return newNode(key);

    if (key < node->key)
        node->left = insertNode(node->left, key);
    else if (key > node->key)
        node->right = insertNode(node->right, key);
    else // 不允许插入重复的节点
        return node;

    // 更新节点的高度
    node->height = max(getHeight(node->left), getHeight(node->right)) + 1;

    // 获取节点的平衡因子
    int balanceFactor = getBalanceFactor(node);

    // 如果节点不平衡,根据平衡因子进行旋转操作
    // 左左情况
    if (balanceFactor > 1 && key < node->left->key)
        return rightRotate(node);
    // 右右情况
    if (balanceFactor < -1 && key > node->right->key)
        return leftRotate(node);
    // 左右情况
    if (balanceFactor > 1 && key > node->left->key) {
        node->left = leftRotate(node->left);
        return rightRotate(node);
    }
    // 右左情况
    if (balanceFactor < -1 && key < node->right->key) {
        node->right = rightRotate(node->right);
        return leftRotate(node);
    }

    return node;
}

// 中序遍历 AVL 树
void inorderTraversal(struct Node* root) {
    if (root != NULL) {
        inorderTraversal(root->left);
        printf("%d ", root->key);
        inorderTraversal(root->right);
    }
}

int main() {
    struct Node* root = NULL;

    // 插入节点
    root = insertNode(root, 10);
    root = insertNode(root, 20);
    root = insertNode(root, 30);
    root = insertNode(root, 40);
    root = insertNode(root, 50);
    root = insertNode(root, 25);

    // 中序遍历 AVL 树
    printf("AVL 树中序遍历结果:");
    inorderTraversal(root);

    return 0;
}

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值