平衡二叉树AVLTree平衡二叉树AVLTree是一种自平衡二叉搜索树由 Adelson-Velsky 和 Landis 于 1962 年提出。它通过维护每个节点的平衡因子定义为左子树高度减去右子树高度来确保树的高度平衡即任意节点的平衡因子绝对值不超过 1$|\text{balance}| \leq 1$。这保证了查找、插入和删除操作的时间复杂度均为 $O(\log n)$适用于需要高效动态数据管理的场景。核心概念平衡因子对于节点 $N$平衡因子 $\text{balance}(N) \text{height}(\text{left_subtree}) - \text{height}(\text{right_subtree})$。平衡条件是 $|\text{balance}(N)| \leq 1$。高度更新插入或删除节点后需递归更新节点高度高度定义为从该节点到最深叶子节点的路径长度。旋转操作当平衡因子绝对值大于 1 时执行旋转以恢复平衡右旋Right Rotation用于“左左”不平衡情况新节点插入在左子树的左子树。左旋Left Rotation用于“右右”不平衡情况新节点插入在右子树的右子树。左右旋Left-Right Rotation先左旋左子节点再右旋当前节点用于“左右”情况。右左旋Right-Left Rotation先右旋右子节点再左旋当前节点用于“右左”情况。C 实现以下是 AVLTree 的完整 C 实现包括节点结构、插入操作和旋转逻辑。代码使用递归实现插入并处理所有四种不平衡情况。#include iostream #include algorithm using namespace std; // 定义 AVL 树节点 class TreeNode { public: int key; // 节点键值 TreeNode* left; // 左子节点指针 TreeNode* right; // 右子节点指针 int height; // 节点高度从该节点到最深叶子节点的边数 TreeNode(int k) : key(k), left(nullptr), right(nullptr), height(1) {} }; // 定义 AVL 树类 class AVLTree { private: TreeNode* root; // 根节点指针 // 辅助函数获取节点高度空节点高度为 0 int height(TreeNode* node) { if (node nullptr) return 0; return node-height; } // 辅助函数计算平衡因子 int getBalance(TreeNode* node) { if (node nullptr) return 0; return height(node-left) - height(node-right); } // 右旋操作用于左左不平衡 TreeNode* rightRotate(TreeNode* y) { TreeNode* x y-left; TreeNode* T2 x-right; // 执行旋转 x-right y; y-left T2; // 更新高度先更新子节点 y再更新父节点 x y-height max(height(y-left), height(y-right)) 1; x-height max(height(x-left), height(x-right)) 1; return x; // 返回新根节点 } // 左旋操作用于右右不平衡 TreeNode* leftRotate(TreeNode* x) { TreeNode* y x-right; TreeNode* T2 y-left; // 执行旋转 y-left x; x-right T2; // 更新高度 x-height max(height(x-left), height(x-right)) 1; y-height max(height(y-left), height(y-right)) 1; return y; // 返回新根节点 } // 递归插入节点 TreeNode* insertNode(TreeNode* node, int key) { // 步骤 1: 正常二叉搜索树插入 if (node nullptr) { return new TreeNode(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; // 键值重复不插入 } // 步骤 2: 更新当前节点高度 node-height 1 max(height(node-left), height(node-right)); // 步骤 3: 计算平衡因子 int balance getBalance(node); // 步骤 4: 如果不平衡执行旋转 // 左左情况新节点在左子树的左子树 if (balance 1 key node-left-key) { return rightRotate(node); } // 右右情况新节点在右子树的右子树 if (balance -1 key node-right-key) { return leftRotate(node); } // 左右情况新节点在左子树的右子树 if (balance 1 key node-left-key) { node-left leftRotate(node-left); return rightRotate(node); } // 右左情况新节点在右子树的左子树 if (balance -1 key node-right-key) { node-right rightRotate(node-right); return leftRotate(node); } return node; // 返回未修改或调整后的节点 } // 中序遍历用于测试 void inOrder(TreeNode* node) { if (node nullptr) return; inOrder(node-left); cout node-key ; inOrder(node-right); } public: AVLTree() : root(nullptr) {} // 公共插入接口 void insert(int key) { root insertNode(root, key); } // 中序遍历接口 void printInOrder() { inOrder(root); cout endl; } }; int main() { AVLTree tree; // 测试插入操作 tree.insert(10); tree.insert(20); tree.insert(30); tree.insert(40); tree.insert(50); tree.insert(25); cout 中序遍历结果: ; tree.printInOrder(); // 输出应有序: 10 20 25 30 40 50 return 0; }https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKP0https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKPGhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKPYhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKQChttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKQOhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKRPhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKS3https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKSMhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKTGhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKTRhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKUAhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKUQhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKV7https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKW1https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKWHhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKWXhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKXJhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKY2https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKYJhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAKZ1https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKZPhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAL05https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL0Jhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAL0Vhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAL17https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL1Hhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAL1Xhttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2Ahttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2Ihttps://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2R代码解析节点结构TreeNode包含键值、左右子节点指针和高度。高度管理height()函数处理空节点情况确保高度计算正确。插入逻辑递归插入新节点。更新节点高度。检查平衡因子根据四种情况执行旋转。旋转操作rightRotate和leftRotate通过调整指针恢复平衡并更新高度。测试main函数演示插入序列中序遍历输出有序序列。性能与应用时间复杂度所有操作插入、删除、查找均为 $O(\log n)$。优势适用于数据库索引、实时系统等需要快速动态更新的场景。扩展删除操作类似插入但更复杂需先删除节点再递归平衡树。完整实现可添加删除方法。通过维护平衡AVLTree 在动态数据集中提供稳定性能是 C 中实现高效搜索的理想选择。