avl树平衡因子_解释了AVL树的插入,旋转和平衡因子

avl树平衡因子

什么是AVL树? (What is an AVL Tree?)

An AVL tree is a subtype of binary search tree. Named after it's inventors Adelson, Velskii and Landis, AVL trees have the property of dynamic self-balancing in addition to all the properties exhibited by binary search trees.

AVL树是二进制搜索树的子类型。 AVL树以其发明人Adelson,Velskii和Landis的名字命名,除了二叉搜索树显示的所有属性外,还具有动态自平衡的属性。

A BST is a data structure composed of nodes. It has the following guarantees:

BST是由节点组成的数据结构。 它具有以下保证:

  1. Each tree has a root node (at the top).

    每棵树都有一个根节点(在顶部)。
  2. The root node has zero, one or two child nodes.

    根节点具有零个,一个或两个子节点。
  3. Each child node has zero, one or two child nodes, and so on.

    每个子节点都有零个,一个或两个子节点,依此类推。
  4. Each node has up to two children.

    每个节点最多有两个孩子。
  5. For each node, its left descendants are less than the current node, which is less than the right descendants.

    对于每个节点,其左后代小于当前节点,而当前节点小于右后代。

AVL trees have an additional guarantee:

AVL树有一个额外的保证:

  1. The difference between the depth of right and left subtrees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee.

    左右子树的深度之差不能超过一。 为了维持此保证,AVL的实现将包括一种算法,当添加其他元素会破坏此保证时,该算法将重新平衡树。

AVL trees have a worst case lookup, insert and delete time of O(log n).

AVL树的最坏情况下查找,插入和删除时间为O(log n)。

右旋 (Right Rotation)

左旋 (Left Rotation)

AVL插入过程 (AVL Insertion Process)

You will do an insertion similar to a normal Binary Search Tree insertion. After inserting, you fix the AVL property using left or right rotations.

您将执行与普通“二进制搜索树”插入类似的插入。 插入后,您可以使用向左或向右旋转来固定AVL属性。

  • If there is an imbalance in left child of right subtree, then you perform a left-right rotation.

    如果右子树的左子节点不平衡,则执行左右旋转。
  • If there is an imbalance in left child of left subtree, then you perform a right rotation.

    如果左子树的左子节点不平衡,则执行右旋转。
  • If there is an imbalance in right child of right subtree, then you perform a left rotation.

    如果右子树的右子节点不平衡,则执行左旋转。
  • If there is an imbalance in right child of left subtree, then you perform a right-left rotation.

    如果左子树的右子节点不平衡,则执行左右旋转。

An AVL tree is a self-balancing binary search tree. An AVL tree is a binary search tree which has the following properties: ->The sub-trees of every node differ in height by at most one. ->Every sub-tree is an AVL tree.

AVL树是一种自平衡二进制搜索树。 AVL树是具有以下属性的二进制搜索树:->每个节点的子树在高度上最多相差一个。 ->每个子树都是AVL树。

AVL tree checks the height of the left and the right sub-trees and assures that the difference is not more than 1. This difference is called the Balance Factor. The height of an AVL tree is always O(Logn) where n is the number of nodes in the tree.

AVL树检查左子树和右子树的高度,并确保差异不超过1。该差异称为“平衡因子”。 AVL树的高度始终为O(Logn),其中n是树中的节点数。

AVL树轮换 (AVL Tree Rotations)

In AVL tree, after performing every operation like insertion and deletion we need to check the balance factor of every node in the tree. If every node satisfies the balance factor condition then we conclude the operation otherwise we must make it balanced. We use rotation operations to make the tree balanced whenever the tree is becoming imbalanced due to any operation.

在AVL树中,执行插入和删除之类的所有操作后,我们需要检查树中每个节点的平衡因子。 如果每个节点都满足平衡因子条件,则可以得出操作的结论,否则必须使其平衡。 每当树由于任何操作而变得不平衡时,我们都会使用旋转操作使树保持平衡。

Rotation operations are used to make a tree balanced.There are four rotations and they are classified into two types:

旋转操作用于使树平衡,共有四次旋转,它们分为两种类型:

单向左旋转(LL旋转) (Single Left Rotation (LL Rotation) )

In LL Rotation every node moves one position to left from the current position.

在“ LL旋转”中,每个节点从当前位置向左移动一个位置。

单向右旋转(RR旋转) (Single Right Rotation (RR Rotation) )

In RR Rotation every node moves one position to right from the current position.

在“ RR旋转”中,每个节点从当前位置向右移动一个位置。

左右旋转(左旋转) (Left Right Rotation (LR Rotation))

The LR Rotation is combination of single left rotation followed by single right rotation. In LR Rotation, first every node moves one position to left then one position to right from the current position.

LR旋转是左单旋转和右单旋转的组合。 在“ LR旋转”中,首先每个节点将一个位置从当前位置向左移动,然后再向右移动一个位置。

左右旋转(RL旋转) (Right Left Rotation (RL Rotation))

The RL Rotation is combination of single right rotation followed by single left rotation. In RL Rotation, first every node moves one position to right then one position to left from the current position.

RL旋转是单向右旋转后跟单向左旋转的组合。 在“ RL旋转”中,首先,每个节点从当前位置向右移动一个位置,然后向左移动一个位置。

AVL树的时间分析 (Time Analysis Of AVL Trees)

AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node can’t be more than 1.

AVL树是二叉搜索树,具有其他属性,即任何节点的左子树和右子树的高度之差不能超过1。

Algorithm Average Worst case: Space: O(n), Time: O(n)

算法平均最坏情况:空间: O(n) ,时间: O(n)

AVL树的应用 (Application of AVL Trees)

AVL trees are beneficial in the cases where you are designing some database where insertions and deletions are not that frequent but you have to frequently look-up for the items present in there.

在设计某些数据库时,如果不经常进行插入和删除操作,但您必须经常查找其中存在的项目,则AVL树是很有用的。

翻译自: https://www.freecodecamp.org/news/avl-tree-insertion-rotation-and-balance-factor/

avl树平衡因子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: AVL树是一种自平衡二叉搜索树。它的插入和删除过程都有一定的复杂度。 在 AVL树插入新节点时,需要先找到新节点应该插入的位置,然后将新节点插入到该位置。如果在插入新节点后AVL树的平衡性被破坏,则需要对AVL树进行调整,以使其重新平衡。这个过程需要使用旋转操作来调整AVL树的结构。 在AVL树中删除节点时,需要先找到要删除的节点,然后在AVL树中删除该节点。如果在删除节点后AVL树的平衡性被破坏,则需要对AVL树进行调整,以使其重新平衡。这个过程也需要使用旋转操作来调整AVL树的结构。 总的来说,AVL树插入和删除都比较复杂,但是它们可以保证AVL树在进行插入和删除操作后仍然是一棵平衡树。 ### 回答2: AVL树是一种自平衡二叉搜索树,插入和删除操作会涉及到树的平衡调整,因此相对来说复杂一些。 在AVL树插入操作,首先会按照二叉搜索树的规则找到插入位置。在插入新节点后,需要从插入位置开始向上逐层检查是否平衡,并适时进行旋转操作来恢复平衡。旋转操作有四种,分别为左旋、右旋、左右旋和右左旋。当需要旋转时,会通过旋转调整节点的左右子树来维持平衡。 在AVL树中删除操作,也需要按照二叉搜索树的规则找到要删除的节点。删除节点后,同样需要从删除节点的父节点开始向上逐层检查是否平衡,并进行旋转操作来恢复平衡。与插入操作类似,删除操作也可能需要进行左旋、右旋、左右旋或右左旋。 因为插入和删除操作都会引起树的平衡性发生改变,所以需要对整棵树进行频繁的旋转操作。这就增加了算法的复杂性,导致插入和删除操作相对困难。 但正是由于AVL树的自平衡特性,保证了树的平衡性,使得在搜索、插入、删除等操作的时间复杂度都能够保持在O(logn)的级别,这样在大规模数据的处理中仍然是非常高效的。因此,虽然插入和删除操作相对较难,但AVL树的特性使其成为一种非常重要和有价值的数据结构。 ### 回答3: AVL树是一种自平衡二叉搜索树,相比于普通的二叉搜索树,它可以在插入或删除节点时保持树的平衡状态,以提供更高效的查找、插入和删除操作。 AVL树插入操作相对复杂一些,因为插入新节点可能会导致树失去平衡。插入一个节点的过程涉及对根节点到插入位置之间的路径上的每个节点进行旋转操作以调整树的平衡。这涉及到计算每个节点的平衡因子,即左子树的高度减去右子树的高度。如果插入节点后某个节点的平衡因子大于1或小于-1,需要进行旋转操作来恢复平衡。 AVL树的删除操作比插入稍微复杂一些。删除一个节点后,也可能导致树失去平衡。与插入不同,删除操作可能导致更多的旋转操作,以恢复树的平衡。在删除节点之后,需要根据被删除节点的后继(或前驱)节点来替代被删除节点的位置,并更新路径上的每个节点的平衡因子。与插入一样,如果某个节点的平衡因子大于1或小于-1,需要进行旋转操作来恢复平衡。 总结起来,AVL树插入和删除操作相对难以理解和实现,因为它涉及到计算平衡因子和多次旋转操作。对于插入和删除而言,我们需要确保在执行这些操作后,树的高度仍然保持较小,并且保持节点的平衡。因此,在实际应用中,为了简化操作和提高性能,我们可能会使用其他平衡树的数据结构,如红黑树。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值