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是由节点组成的数据结构。 它具有以下保证:
- Each tree has a root node (at the top). 每棵树都有一个根节点(在顶部)。
- The root node has zero, one or two child nodes. 根节点具有零个,一个或两个子节点。
- Each child node has zero, one or two child nodes, and so on. 每个子节点都有零个,一个或两个子节点,依此类推。
- Each node has up to two children. 每个节点最多有两个孩子。
- 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树有一个额外的保证:
- 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树平衡因子