维基百科上的不平衡 AVL 树示例如何真正不平衡?

发布于 2024-07-07 09:43:05 字数 532 浏览 12 评论 0原文

alt text

上面的图片来自 < a href="http://en.wikipedia.org/wiki/AVL_tree" rel="nofollow noreferrer">“维基百科关于 AVL 树的条目” 维基百科指出这是不平衡的。 这棵树怎么还没有平衡呢? 这是文章中的引用:

节点的平衡因子是其右子树的高度减去其左子树的高度,平衡因子为1、0或-1的节点被认为是平衡的。 具有任何其他平衡因子的节点都被视为不平衡,需要重新平衡树。 平衡因子要么直接存储在每个节点上,要么根据子树的高度计算。

左子树和右子树的高度均为 4。左子树的右子树的高度为 3,仍然只比 4 少 1。有人能解释一下我错过了什么吗?

alt text

The image above is from "Wikipedia's entry on AVL trees" which Wikipedia indicates is unbalanced.
How is this tree not balanced already?
Here's a quote from the article:

The balance factor of a node is the height of its right subtree minus the height of its left subtree and a node with balance factor 1, 0, or -1 is considered balanced. A node with any other balance factor is considered unbalanced and requires rebalancing the tree. The balance factor is either stored directly at each node or computed from the heights of the subtrees.

Both the left and right subtrees have a height of 4. The right subtree of the left tree has a height of 3 which is still only 1 less than 4. Can someone explain what I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅浅 2024-07-14 09:43:06

例如,节点 76 是不平衡的,因为它的右子树的高度为 0,左子树的高度为 3。

Node 76 is unbalanced, for example, because its right subtree is of height 0 and the left is of height 3.

一梦等七年七年为一梦 2024-07-14 09:43:06

直观上来说,这是因为它不是越小越好。 例如,12 应该是 9 和 14 的父节点。事实上,9 没有左子树,因此它不平衡。 树是一种分层数据结构,因此“平衡”之类的规则通常适用于每个节点,而不仅仅是根节点。

您是对的,根节点是平衡的,但并非树的所有节点都是平衡的。

Intuitively, it's because it's not as small as possible. e.g., 12 should be the parent of 9 and 14. As it is, 9 has no left sub-tree so it's out of balance. A tree is a hierarchical data structure so a rule like "balanced" often apply to every node and not just the root node.

You're correct the root node is balanced, but not all the nodes of the tree are.

感情旳空白 2024-07-14 09:43:06

另一种看待这个问题的方法是,任何节点的高度 h 均由以下公式给出:

h = 1 + max( left.height, right.height ) ,

并且节点为每当以下情况时不平衡:

abs( left.height - right.height ) > 1

查看上面的树:

- Node 12 is a leaf node so its height = 1+max(0,0) = 1
- Node 14 has one child (12, on the left), so its height is = 1+max(1,0) = 2
- Node 9 has one child (14, on the right), so its height is = 1+max(0,2) = 3

为了确定节点 9 是否平衡,我们查看其子节点的高度:

- 9's left child is NULL, so 9.left.height = 0
- 9's right child (14) has height 2, so 9.right.height = 2

现在求解以显示节点 9 不平衡:

9.unbalanced = abs( 9.left.height - 9.right.height ) > 1
9.unbalanced = abs( 0 - 2 ) > 1
9.unbalanced = abs( -2 ) > 1
9.unbalanced = 2 > 1
9.unbalanced = true

可以对每个其他节点进行类似的计算。

Another way to look at this is that the height h of any node is given by:

h = 1 + max( left.height, right.height )

and a node is unbalanced whenever:

abs( left.height - right.height ) > 1

Looking at the tree above:

- Node 12 is a leaf node so its height = 1+max(0,0) = 1
- Node 14 has one child (12, on the left), so its height is = 1+max(1,0) = 2
- Node 9 has one child (14, on the right), so its height is = 1+max(0,2) = 3

To determine if node 9 is balanced or not we look at the height of its children:

- 9's left child is NULL, so 9.left.height = 0
- 9's right child (14) has height 2, so 9.right.height = 2

Now solve to show that node 9 is unbalanced:

9.unbalanced = abs( 9.left.height - 9.right.height ) > 1
9.unbalanced = abs( 0 - 2 ) > 1
9.unbalanced = abs( -2 ) > 1
9.unbalanced = 2 > 1
9.unbalanced = true

Similar calculations can be made for every other node.

无需解释 2024-07-14 09:43:06

高度平衡的树在很多方面都是最好的! 对于每个子树,它的子树的高度相差 1 或 0,其中没有子树的高度为零,因此叶子的高度为 1。它是最简单的平衡树,平衡开销最低:最多 2n在高度为 n 的树区域中插入或删除的旋转次数,通常要少得多。 一次循环就是写入 3 个指针,因此非常便宜。 高度平衡树的最坏情况,尽管最大高度高出约 42%,但与 2^n-1 值的完美平衡完整二叉树相比,效率大约低一比较。 完美平衡的完整二叉树的实现成本要高得多,平均而言,对于找到的情况往往需要 n-1 次比较,而对于未找到的情况则总是需要 n-1 次比较。 对于树最坏情况插入顺序,有序数据,当插入2^n-1个项目时,得到的高度平衡树是完美平衡的满二叉树!

(旋转是一种很好的平衡方法,但有一个问题:如果重的孙子位于重的子项的内侧,则一次旋转只会将其移动到另一侧的内侧,没有任何改善。所以,如果是 1 个单位高,即使名义上是平衡的,您首先旋转该子项以减轻它的重量,因此对于 n 层插入或删除最多 2n 次旋转,最坏的情况并且不太可能。)

A height balanced tree is many ways best! For every subtree, it has children of heights that differ by 1 or 0, where no children is a height of zero, so a leaf has height 1. It is the simplest balanced tree, with the lowest overhead to balance: a maximum of 2n rotations for an insert or delete in a tree area of height n, and often much less. A rotation is writing 3 pointers, and so is very cheap. The worst case of the height balanced tree, even though of about 42% greater maximum height, is about one comparison less efficient than a perfectly balanced full binary tree of 2^n-1 values. A perfectly balanced full binary tree is far more expensive to achieve, tends to need, on average, n-1 comparisons for a find and exactly n comparisons always for a not-found. For the tree worst case insertion order, ordered data, when 2^n-1 items are inserted, the height balanced tree that results is a perfectly balanced full binary tree!

(Rotation is a great way to balance, but comes with a catch: if the heavy grandchild is on the inside of the heavy child, a single rotate just moves it to the inside of the opposite side, with no improvement. So, if it is 1 unit higher, even though nominally balanced, you rotate that child to lighten it first. Hence a max of 2n rotations for an n level insert or delete, worst case and unlikely.)

懒的傷心 2024-07-14 09:43:05

为了平衡,树中的每个节点必须要么

  • 没有子节点,要么(作为“叶”节点)
  • 有两个子节点。
  • 或者,如果它只有一个子节点,则该子节点必须是一片叶子。

    在您发布的图表中,9, 54 & 76 违反了最后一条规则。

适当平衡后,树看起来像:

Root: 23
(23) -> 14 & 67
(14) -> 12 & 17
(12) -> 9
(17) -> 19
(67) -> 50 & 72
(50) -> 54
(72) -> 76

更新:(近 9 年后,我为该图创建了一个很酷的在线图表 draw.io)在此处输入图像描述

To be balanced, every node in the tree must, either,

  • have no children, (be a "leaf" node)
  • Have two children.
  • Or, if it has only one child, that child must be a leaf.

    In the chart you posted, 9, 54 & 76 violate the last rule.

Properly balanced, the tree would look like:

Root: 23
(23) -> 14 & 67
(14) -> 12 & 17
(12) -> 9
(17) -> 19
(67) -> 50 & 72
(50) -> 54
(72) -> 76

UPDATE: (after almost 9 years, I created a cool online chart for the graph at draw.io)enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文