原文: https://www.programiz.com/dsa/avl-tree

在本教程中,您将学习什么是 avl 树。 此外,您还将找到在 C,C++ ,Java 和 Python 的 avl 树上执行的各种操作的工作示例。

AVL 树是一种自平衡二叉搜索树,其中每个节点都维护额外的信息,称为平衡因子,其值为 -1、0 或 +1。

AVL 树以其发明者 Georgy Adelson-Velsky 和 Landis 得名。


平衡系数

AVL 树中节点的平衡因子是该节点的左子树的高度和右子树的高度之间的差。

平衡因子=(左子树的高度-右子树的高度)或(右子树的高度-左子树的高度)

Avl 树的自平衡属性由平衡因子保持。 平衡因子的值应始终为 -1、0 或 +1。

平衡 avl 树的示例是:

AVL 树 - 图1

Avl tree


AVL 树上的操作

可以在 AVL 树上执行的各种操作是:

旋转 AVL 树中的子树

在旋转操作中,子树的节点位置互换。

轮播有两种类型:


向左旋转

在向左旋转时,右侧节点的排列将转换为左侧节点的排列。

算法

  1. 令初始树为:
    AVL 树 - 图2
    左旋转

  2. 如果y具有左子树,则将x分配为y的左子树的父树。
    AVL 树 - 图3
    x分配为y的左子树的父级

  3. 如果x的父级是NULL,则将y作为树的根。

  4. 否则,如果xp的左子代,则将y设为p的左子代。

  5. 否则,将y分配为p的右子元素。
    AVL 树 - 图4
    x的父级更改为y的父级

  6. y设为x的父代。
    AVL 树 - 图5
    y指定为x的父代。


向右旋转

在向左旋转时,左侧节点的排列将转换为右侧节点的排列。

  1. 令初始树为:
    AVL 树 - 图6
    初始树

  2. 如果x具有右子树,则将y分配为x的右子树的父树。
    AVL 树 - 图7
    y分配为x
    的右子树的父级

  3. 如果y的父级为NULL,则将x作为树的根。

  4. 否则,如果y是其父级p的右子,则将x作为p的右子。

  5. 否则,将x分配为p的左子元素。
    AVL 树 - 图8
    y的父级指定为x的父级。

  6. 使xy的父代。
    AVL 树 - 图9
    x分配为y的父项


左右旋转

在左右旋转时,首先将布置向左移动,然后向右移动。

  1. x-y上向左旋转。
    AVL 树 - 图10
    向左旋转x-y

  2. y-z上向右旋转。
    AVL 树 - 图11
    向右旋转z-y

在左右旋转时,这些布置首先向右移动,然后向左移动。

  1. x-y上向右旋转。
    AVL 树 - 图12
    右旋转x-y

  2. z-y上向左旋转。
    AVL 树 - 图13
    向左旋转z-y


插入新节点的算法

newNode始终作为平衡因子等于 0 的叶节点插入。

  1. 令初始树为:
    AVL 树 - 图14
    插入的初始树
    令要插入的节点为:
    AVL 树 - 图15
    新节点

  2. 使用以下递归步骤转到相应的叶节点以插入newNode。 比较newKey与当前树的rootKey

    1. 如果newKey < rootKey,则在当前节点的左子树上调用插入算法,直到到达叶节点为止。

    2. 否则,如果newKey > rootKey,则在当前节点的右子树上调用插入算法,直到到达叶节点为止。

    3. 否则,返回leafNode
      AVL 树 - 图16
      查找插入newNode的位置

  3. 将通过上述步骤获得的leafKeynewKey进行比较:

    1. 如果newKey < leafKey,则将newNode设为leafNodeleftChild

    2. 否则,将newNode设为leafNoderightChild
      AVL 树 - 图17
      插入新节点

  4. 更新节点的balanceFactor
    AVL 树 - 图18
    插入后更新平衡因子

  5. 如果节点不平衡,请重新平衡该节点。

    1. 如果balanceFactor > 1,则表示左侧子树的高度大于右侧子树的高度。 因此,请向右旋转或向左旋转

      1. 如果newNodeKey < leftChildKey进行右旋转。

      2. 否则,请左右旋转。
        AVL 树 - 图19
        旋转平衡树
        AVL 树 - 图20
        旋转平衡树

    2. 如果balanceFactor < -1,则意味着右子树的高度大于左子树的高度。 因此,请向右旋转或向左旋转

      1. 如果newNodeKey > rightChildKey进行左旋转。
      2. 否则,请左右旋转
  6. 最终的树是:
    AVL 树 - 图21
    最终的平衡树

删除节点的算法

节点始终被删除为叶节点。 删除节点后,节点的平衡因子将更改。 为了重新平衡平衡系数,执行适当的旋转。

  1. 找到nodeToBeDeleted(递归用于在下面使用的代码中找到nodeToBeDeleted)。
    AVL 树 - 图22
    定位要删除的节点

  2. 删除节点有以下三种情况:

    1. 如果nodeToBeDeleted是叶节点(即没有任何子节点),则删除nodeToBeDeleted

    2. 如果nodeToBeDeleted有一个子级,则用该子级的内容替换nodeToBeDeleted的内容。 移走子级。

    3. 如果nodeToBeDeleted有两个子级,则找到nodeToBeDeleted的有序继承人w(即,在右子树中具有键的最小值的节点)。
      AVL 树 - 图23
      寻找继任者

      1. nodeToBeDeleted的内容替换为w的内容。
        AVL 树 - 图24
        替换要删除的节点

      2. 删除叶节点w
        AVL 树 - 图25
        移除

  3. 更新节点的balanceFactor
    AVL 树 - 图26
    更新bf

  4. 如果任何节点的平衡因子不等于 -1、0 或 1,则重新平衡树。

    1. 如果currentNode > balanceFactor

      1. 如果leftChildbalanceFactor >= 0,请向右旋转。
        AVL 树 - 图27
        向右旋转以平衡树

      2. 否则做左右旋转。

    2. 如果currentNodebalanceFactor < -1

      1. 如果rightChildbalanceFactor = 0,请向左旋转。
      2. 否则做左右旋转。
  5. 最终的树是:
    AVL 树 - 图28
    Avl 最终树

Python,Java 和 C/C++ 示例

  1. # AVL tree implementation in Python
  2. import sys
  3. # Create a tree node
  4. class TreeNode(object):
  5. def __init__(self, key):
  6. self.key = key
  7. self.left = None
  8. self.right = None
  9. self.height = 1
  10. class AVLTree(object):
  11. # Function to insert a node
  12. def insert_node(self, root, key):
  13. # Find the correct location and insert the node
  14. if not root:
  15. return TreeNode(key)
  16. elif key < root.key:
  17. root.left = self.insert_node(root.left, key)
  18. else:
  19. root.right = self.insert_node(root.right, key)
  20. root.height = 1 + max(self.getHeight(root.left),
  21. self.getHeight(root.right))
  22. # Update the balance factor and balance the tree
  23. balanceFactor = self.getBalance(root)
  24. if balanceFactor > 1:
  25. if key < root.left.key:
  26. return self.rightRotate(root)
  27. else:
  28. root.left = self.leftRotate(root.left)
  29. return self.rightRotate(root)
  30. if balanceFactor < -1:
  31. if key > root.right.key:
  32. return self.leftRotate(root)
  33. else:
  34. root.right = self.rightRotate(root.right)
  35. return self.leftRotate(root)
  36. return root
  37. # Function to delete a node
  38. def delete_node(self, root, key):
  39. # Find the node to be deleted and remove it
  40. if not root:
  41. return root
  42. elif key < root.key:
  43. root.left = self.delete_node(root.left, key)
  44. elif key > root.key:
  45. root.right = self.delete_node(root.right, key)
  46. else:
  47. if root.left is None:
  48. temp = root.right
  49. root = None
  50. return temp
  51. elif root.right is None:
  52. temp = root.left
  53. root = None
  54. return temp
  55. temp = self.getMinValueNode(root.right)
  56. root.key = temp.key
  57. root.right = self.delete_node(root.right,
  58. temp.key)
  59. if root is None:
  60. return root
  61. # Update the balance factor of nodes
  62. root.height = 1 + max(self.getHeight(root.left),
  63. self.getHeight(root.right))
  64. balanceFactor = self.getBalance(root)
  65. # Balance the tree
  66. if balanceFactor > 1:
  67. if self.getBalance(root.left) >= 0:
  68. return self.rightRotate(root)
  69. else:
  70. root.left = self.leftRotate(root.left)
  71. return self.rightRotate(root)
  72. if balanceFactor < -1:
  73. if self.getBalance(root.right) <= 0:
  74. return self.leftRotate(root)
  75. else:
  76. root.right = self.rightRotate(root.right)
  77. return self.leftRotate(root)
  78. return root
  79. # Function to perform left rotation
  80. def leftRotate(self, z):
  81. y = z.right
  82. T2 = y.left
  83. y.left = z
  84. z.right = T2
  85. z.height = 1 + max(self.getHeight(z.left),
  86. self.getHeight(z.right))
  87. y.height = 1 + max(self.getHeight(y.left),
  88. self.getHeight(y.right))
  89. return y
  90. # Function to perform right rotation
  91. def rightRotate(self, z):
  92. y = z.left
  93. T3 = y.right
  94. y.right = z
  95. z.left = T3
  96. z.height = 1 + max(self.getHeight(z.left),
  97. self.getHeight(z.right))
  98. y.height = 1 + max(self.getHeight(y.left),
  99. self.getHeight(y.right))
  100. return y
  101. # Get the height of the node
  102. def getHeight(self, root):
  103. if not root:
  104. return 0
  105. return root.height
  106. # Get balance factore of the node
  107. def getBalance(self, root):
  108. if not root:
  109. return 0
  110. return self.getHeight(root.left) - self.getHeight(root.right)
  111. def getMinValueNode(self, root):
  112. if root is None or root.left is None:
  113. return root
  114. return self.getMinValueNode(root.left)
  115. def preOrder(self, root):
  116. if not root:
  117. return
  118. print("{0} ".format(root.key), end="")
  119. self.preOrder(root.left)
  120. self.preOrder(root.right)
  121. # Print the tree
  122. def printHelper(self, currPtr, indent, last):
  123. if currPtr != None:
  124. sys.stdout.write(indent)
  125. if last:
  126. sys.stdout.write("R----")
  127. indent += " "
  128. else:
  129. sys.stdout.write("L----")
  130. indent += "| "
  131. print(currPtr.key)
  132. self.printHelper(currPtr.left, indent, False)
  133. self.printHelper(currPtr.right, indent, True)
  134. myTree = AVLTree()
  135. root = None
  136. nums = [33, 13, 52, 9, 21, 61, 8, 11]
  137. for num in nums:
  138. root = myTree.insert_node(root, num)
  139. myTree.printHelper(root, "", True)
  140. key = 13
  141. root = myTree.delete_node(root, key)
  142. print("After Deletion: ")
  143. myTree.printHelper(root, "", True)
  1. // AVL tree implementation in Java
  2. // Create node
  3. class Node {
  4. int item, height;
  5. Node left, right;
  6. Node(int d) {
  7. item = d;
  8. height = 1;
  9. }
  10. }
  11. // Tree class
  12. class AVLTree {
  13. Node root;
  14. int height(Node N) {
  15. if (N == null)
  16. return 0;
  17. return N.height;
  18. }
  19. int max(int a, int b) {
  20. return (a > b) ? a : b;
  21. }
  22. Node rightRotate(Node y) {
  23. Node x = y.left;
  24. Node T2 = x.right;
  25. x.right = y;
  26. y.left = T2;
  27. y.height = max(height(y.left), height(y.right)) + 1;
  28. x.height = max(height(x.left), height(x.right)) + 1;
  29. return x;
  30. }
  31. Node leftRotate(Node x) {
  32. Node y = x.right;
  33. Node T2 = y.left;
  34. y.left = x;
  35. x.right = T2;
  36. x.height = max(height(x.left), height(x.right)) + 1;
  37. y.height = max(height(y.left), height(y.right)) + 1;
  38. return y;
  39. }
  40. // Get balance factor of a node
  41. int getBalanceFactor(Node N) {
  42. if (N == null)
  43. return 0;
  44. return height(N.left) - height(N.right);
  45. }
  46. // Insert a node
  47. Node insertNode(Node node, int item) {
  48. // Find the position and insert the node
  49. if (node == null)
  50. return (new Node(item));
  51. if (item < node.item)
  52. node.left = insertNode(node.left, item);
  53. else if (item > node.item)
  54. node.right = insertNode(node.right, item);
  55. else
  56. return node;
  57. // Update the balance factor of each node
  58. // And, balance the tree
  59. node.height = 1 + max(height(node.left), height(node.right));
  60. int balanceFactor = getBalanceFactor(node);
  61. if (balanceFactor > 1) {
  62. if (item < node.left.item) {
  63. return rightRotate(node);
  64. } else if (item > node.left.item) {
  65. node.left = leftRotate(node.left);
  66. return rightRotate(node);
  67. }
  68. }
  69. if (balanceFactor < -1) {
  70. if (item > node.right.item) {
  71. return leftRotate(node);
  72. } else if (item < node.right.item) {
  73. node.left = rightRotate(node.left);
  74. return leftRotate(node);
  75. }
  76. }
  77. return node;
  78. }
  79. Node nodeWithMimumValue(Node node) {
  80. Node current = node;
  81. while (current.left != null)
  82. current = current.left;
  83. return current;
  84. }
  85. // Delete a node
  86. Node deleteNode(Node root, int item) {
  87. // Find the node to be deleted and remove it
  88. if (root == null)
  89. return root;
  90. if (item < root.item)
  91. root.left = deleteNode(root.left, item);
  92. else if (item > root.item)
  93. root.right = deleteNode(root.right, item);
  94. else {
  95. if ((root.left == null) || (root.right == null)) {
  96. Node temp = null;
  97. if (temp == root.left)
  98. temp = root.right;
  99. else
  100. temp = root.left;
  101. if (temp == null) {
  102. temp = root;
  103. root = null;
  104. } else
  105. root = temp;
  106. } else {
  107. Node temp = nodeWithMimumValue(root.right);
  108. root.item = temp.item;
  109. root.right = deleteNode(root.right, temp.item);
  110. }
  111. }
  112. if (root == null)
  113. return root;
  114. // Update the balance factor of each node and balance the tree
  115. root.height = max(height(root.left), height(root.right)) + 1;
  116. int balanceFactor = getBalanceFactor(root);
  117. if (balanceFactor > 1) {
  118. if (getBalanceFactor(root.left) >= 0) {
  119. return rightRotate(root);
  120. } else {
  121. root.left = leftRotate(root.left);
  122. return rightRotate(root);
  123. }
  124. }
  125. if (balanceFactor < -1) {
  126. if (getBalanceFactor(root.right) <= 0) {
  127. return leftRotate(root);
  128. } else {
  129. root.right = rightRotate(root.right);
  130. return leftRotate(root);
  131. }
  132. }
  133. return root;
  134. }
  135. void preOrder(Node node) {
  136. if (node != null) {
  137. System.out.print(node.item + " ");
  138. preOrder(node.left);
  139. preOrder(node.right);
  140. }
  141. }
  142. // Print the tree
  143. private void printTree(Node currPtr, String indent, boolean last) {
  144. if (currPtr != null) {
  145. System.out.print(indent);
  146. if (last) {
  147. System.out.print("R----");
  148. indent += " ";
  149. } else {
  150. System.out.print("L----");
  151. indent += "| ";
  152. }
  153. System.out.println(currPtr.item);
  154. printTree(currPtr.left, indent, false);
  155. printTree(currPtr.right, indent, true);
  156. }
  157. }
  158. // Driver code
  159. public static void main(String[] args) {
  160. AVLTree tree = new AVLTree();
  161. tree.root = tree.insertNode(tree.root, 33);
  162. tree.root = tree.insertNode(tree.root, 13);
  163. tree.root = tree.insertNode(tree.root, 53);
  164. tree.root = tree.insertNode(tree.root, 9);
  165. tree.root = tree.insertNode(tree.root, 21);
  166. tree.root = tree.insertNode(tree.root, 61);
  167. tree.root = tree.insertNode(tree.root, 8);
  168. tree.root = tree.insertNode(tree.root, 11);
  169. tree.printTree(tree.root, "", true);
  170. tree.root = tree.deleteNode(tree.root, 13);
  171. System.out.println("After Deletion: ");
  172. tree.printTree(tree.root, "", true);
  173. }
  174. }
// AVL tree implementation in C

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

// Create Node
struct Node {
  int key;
  struct Node *left;
  struct Node *right;
  int height;
};

int max(int a, int b);

// Calculate height
int height(struct Node *N) {
  if (N == NULL)
    return 0;
  return N->height;
}

int max(int a, int b) {
  return (a > b) ? a : b;
}

// Create a node
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;
  return (node);
}

// Right rotate
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(height(y->left), height(y->right)) + 1;
  x->height = max(height(x->left), height(x->right)) + 1;
  return x;
}

// Left Rotate
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(height(x->left), height(x->right)) + 1;
  y->height = max(height(y->left), height(y->right)) + 1;
  return y;
}

// Get the balance factor
int getBalanceFactor(struct Node *N) {
  if (N == NULL)
    return 0;
  return height(N->left) - height(N->right);
}

// Insert node
struct Node *insertNode(struct Node *node, int key) {
  // Find the correct position to insert the node and insert it
  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;

  // Update the balance factor of each node and
  // Balance the tree
  node->height = 1 + max(height(node->left),
               height(node->right));
  int balanceFactor = getBalanceFactor(node);
  if (balanceFactor > 1) {
    if (key < node->left->key) {
      return rightRotate(node);
    } else if (key > node->left->key) {
      node->left = leftRotate(node->left);
      return rightRotate(node);
    }
  }
  if (balanceFactor < -1) {
    if (key > node->right->key) {
      return leftRotate(node);
    } else if (key < node->right->key) {
      node->left = rightRotate(node->left);
      return leftRotate(node);
    }
  }
  return node;
}

struct Node *minValueNode(struct Node *node) {
  struct Node *current = node;
  while (current->left != NULL)
    current = current->left;
  return current;
}

// Delete a node
struct Node *deleteNode(struct Node *root, int key) {
  // Find the node and delete it
  if (root == NULL)
    return root;
  if (key < root->key)
    root->left = deleteNode(root->left, key);
  else if (key > root->key)
    root->right = deleteNode(root->right, key);
  else {
    if ((root->left == NULL) || (root->right == NULL)) {
      struct Node *temp = root->left ? root->left : root->right;
      if (temp == NULL) {
        temp = root;
        root = NULL;
      } else
        *root = *temp;

      free(temp);
    } else {
      struct Node *temp = minValueNode(root->right);
      root->key = temp->key;
      root->right = deleteNode(root->right, temp->key);
    }
  }
  if (root == NULL)
    return root;

  // Update the balance factor of each node and
  // balance the tree
  root->height = 1 + max(height(root->left),
               height(root->right));
  int balanceFactor = getBalanceFactor(root);
  if (balanceFactor > 1) {
    if (getBalanceFactor(root->left) >= 0) {
      return rightRotate(root);
    } else {
      root->left = leftRotate(root->left);
      return rightRotate(root);
    }
  }
  if (balanceFactor < -1) {
    if (getBalanceFactor(root->right) <= 0) {
      return leftRotate(root);
    } else {
      root->right = rightRotate(root->right);
      return leftRotate(root);
    }
  }
  return root;
}

// Print the tree
void printPreOrder(struct Node *root) {
  if (root != NULL) {
    printf("%d ", root->key);
    printPreOrder(root->left);
    printPreOrder(root->right);
  }
}

int main() {
  struct Node *root = NULL;
  root = insertNode(root, 33);
  root = insertNode(root, 13);
  root = insertNode(root, 53);
  root = insertNode(root, 9);
  root = insertNode(root, 21);
  root = insertNode(root, 61);
  root = insertNode(root, 8);
  root = insertNode(root, 11);
  printPreOrder(root);
  root = deleteNode(root, 13);
  printf("\nAfter deletion: ");
  printPreOrder(root);
}
// AVL tree implementation in C++

#include <iostream>
using namespace std;

class Node {
   public:
  int key;
  Node *left;
  Node *right;
  int height;
};

int max(int a, int b);

// Calculate height
int height(Node *N) {
  if (N == NULL)
    return 0;
  return N->height;
}

int max(int a, int b) {
  return (a > b) ? a : b;
}

// New node creation
Node *newNode(int key) {
  Node *node = new Node();
  node->key = key;
  node->left = NULL;
  node->right = NULL;
  node->height = 1;
  return (node);
}

// Rotate right
Node *rightRotate(Node *y) {
  Node *x = y->left;
  Node *T2 = x->right;
  x->right = y;
  y->left = T2;
  y->height = max(height(y->left),
          height(y->right)) +
        1;
  x->height = max(height(x->left),
          height(x->right)) +
        1;
  return x;
}

// Rotate left
Node *leftRotate(Node *x) {
  Node *y = x->right;
  Node *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;
}

// Get the balance factor of each node
int getBalanceFactor(Node *N) {
  if (N == NULL)
    return 0;
  return height(N->left) -
       height(N->right);
}

// Insert a node
Node *insertNode(Node *node, int key) {
  // Find the correct postion and insert the node
  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;

  // Update the balance factor of each node and
  // balance the tree
  node->height = 1 + max(height(node->left),
               height(node->right));
  int balanceFactor = getBalanceFactor(node);
  if (balanceFactor > 1) {
    if (key < node->left->key) {
      return rightRotate(node);
    } else if (key > node->left->key) {
      node->left = leftRotate(node->left);
      return rightRotate(node);
    }
  }
  if (balanceFactor < -1) {
    if (key > node->right->key) {
      return leftRotate(node);
    } else if (key < node->right->key) {
      node->left = rightRotate(node->left);
      return leftRotate(node);
    }
  }
  return node;
}

// Node with minimum value
Node *nodeWithMimumValue(Node *node) {
  Node *current = node;
  while (current->left != NULL)
    current = current->left;
  return current;
}

// Delete a node
Node *deleteNode(Node *root, int key) {
  // Find the node and delete it
  if (root == NULL)
    return root;
  if (key < root->key)
    root->left = deleteNode(root->left, key);
  else if (key > root->key)
    root->right = deleteNode(root->right, key);
  else {
    if ((root->left == NULL) ||
      (root->right == NULL)) {
      Node *temp = root->left ? root->left : root->right;
      if (temp == NULL) {
        temp = root;
        root = NULL;
      } else
        *root = *temp;
      free(temp);
    } else {
      Node *temp = nodeWithMimumValue(root->right);
      root->key = temp->key;
      root->right = deleteNode(root->right,
                   temp->key);
    }
  }

  if (root == NULL)
    return root;

  // Update the balance factor of each node and
  // balance the tree
  root->height = 1 + max(height(root->left),
               height(root->right));
  int balanceFactor = getBalanceFactor(root);
  if (balanceFactor > 1) {
    if (getBalanceFactor(root->left) >= 0) {
      return rightRotate(root);
    } else {
      root->left = leftRotate(root->left);
      return rightRotate(root);
    }
  }
  if (balanceFactor < -1) {
    if (getBalanceFactor(root->right) <= 0) {
      return leftRotate(root);
    } else {
      root->right = rightRotate(root->right);
      return leftRotate(root);
    }
  }
  return root;
}

// Print the tree
void printTree(Node *root, string indent, bool last) {
  if (root != nullptr) {
    cout << indent;
    if (last) {
      cout << "R----";
      indent += "   ";
    } else {
      cout << "L----";
      indent += "|  ";
    }
    cout << root->key << endl;
    printTree(root->left, indent, false);
    printTree(root->right, indent, true);
  }
}

int main() {
  Node *root = NULL;
  root = insertNode(root, 33);
  root = insertNode(root, 13);
  root = insertNode(root, 53);
  root = insertNode(root, 9);
  root = insertNode(root, 21);
  root = insertNode(root, 61);
  root = insertNode(root, 8);
  root = insertNode(root, 11);
  printTree(root, "", true);
  root = deleteNode(root, 13);
  cout << "After deleting " << endl;
  printTree(root, "", true);
}

AVL 树上的不同操作的复杂度

插入 删除 搜索
O(log n) O(log n) O(log n)

AVL 树应用

  • 用于索引数据库中的大记录
  • 用于在大型数据库中搜索