简体   繁体   English

2-3-4树高不平衡

[英]2-3-4 tree height imbalanced

I observed that the height of a 2-3-4 tree can be different depending of the order of insertion of nodes. 我观察到2-3-4树的高度可以根据节点插入的顺序而有所不同。

eg 1,2,3,4,5,6,7,8,9,10 will yield a tree of height 2 例如1,2,3,4,5,6,7,8,9,10将产生高度为2的树

While inserting in this order: 在按此顺序插入时:

eg 1, 5, 10, 2, 3, 8, 9, 4, 7, 8 will yield a tree of height 1 例如1、5、10、2、3、8、9、4、7、8将产生高度为1的树

Is this a normal property of a 2-3-4 tree? 这是2-3-4树的正常属性吗? Inserting nodes in sequence will yield a very imbalanced tree in that case. 在这种情况下,顺序插入节点将产生非常不平衡的树。 I thought 2-3-4 trees should be balanced trees? 我以为2-3-4棵树应该是平衡树?

Thanks. 谢谢。

2-3-4 trees are indeed "balanced" trees in the sense that the height of the tree never exceeds some fixed bound relative to the number of nodes (which, if each node has exactly two values in it, is O(log n)). 2-3-4树实际上是“平衡”树,从某种意义上说,树的高度永远不会超过相对于节点数的固定范围(如果每个节点中恰好有两个值,则为O(log n ))。 The term "balanced" here should be contrasted with "imbalanced," which would be a tree in which the height is "large" relative to the number of nodes. 术语“平衡”应与“不平衡”进行对比,“不平衡”将是一棵树,其中的高度相对于节点数为“大”。 For example, this tree is highly imbalanced: 例如,此树高度不平衡:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

I think that you are assuming that the term "balanced" means "as compact as possible," which is not the case. 我认为您假设“平衡”一词的意思是“尽可能紧凑”,事实并非如此。 It is absolutely possible to have multiple different insertion orders into a 2-3-4 tree produce trees of different height, some of which will have lower heights than others. 绝对有可能在2-3-4棵树中产生多个不同的插入顺序,以产生不同高度的树,其中一些树的高度会比其他树低。 However, the maximum possible height achievable is not too great compared to the total number of nodes in the tree, and therefore 2-3-4 trees are indeed considered balanced trees. 但是,与树中节点的总数相比,可达到的最大高度并不是太大,因此,2-3-4棵树实际上被认为是平衡树。

Hope this helps! 希望这可以帮助!

a balanced tree usually means it's height is O(logn). 一棵平衡的树通常意味着它的高度是O(logn)。

a vaild B-Trees (including 2-3-4 Tree) have the following limits: 合格的B树(包括2-3-4树)具有以下限制:

  1. all non-root node have at least [m/2] elements. 所有非根节点至少具有[m / 2]个元素。

  2. all leaves are in the same height. 所有叶子都在同一高度。

with this two limits, a valid B-Tree is proved to have O(logn) height. 在这两个限制下,有效的B树被证明具有O(logn)高度。

I observed that the height of a 2-3-4 tree can be different depending of the order of insertion of nodes. 我观察到2-3-4树的高度可以根据节点插入的顺序而有所不同。

The insertion algorithm for 2-3-4 trees splits 4-nodes "on the way" to the leaf node, since they cannot take another item. 2-3-4树的插入算法将“四个”节点“在途中”拆分到叶节点,因为它们无法获取其他项。 This allows insertion to be done in one pass and the tree remains balanced. 这样一来,插入就可以完成,并且树可以保持平衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM