为什么node in heap数据结构在很多类型中只有两个children?
Why node in heap data structure in many types has only two children?
来自维基
The maximum number of children each node can have depends on the type of heap, but in many types it is at most two, which is known as a binary heap.
我不明白为什么很多类型堆中的节点最多只有两个children?为什么三个children或四个children等等不常见?谢谢~
大多数类型的堆每个节点最多有两个 children 是不正确的,但是二叉堆是真的 - 最多有每个节点两个 children -- 是最常实现的类型。它是最常实现的类型,因为它很简单,cache-friendly,memory-efficient。
用于二叉堆的数据结构可以与每个节点不同数量的children一起使用。如果我们考虑 x 保持不变。然而,为了决定最好的 x,我们必须让它有所不同,在这种情况下,常见操作需要 O(x * log N / log x)次.
为了确定每个节点最有效的 children 数量,我们可以选择 x 来最小化因子 x/log x.
如果你绘制你可以看到每个节点的最佳 children 数量实际上是 3(最小值在 x=e,但我们需要一个整数):
...但是 2 和 3 之间的差异并不显着,并且代码更简单,每个节点使用 2 children,所以这是常见的做法。
来自维基
The maximum number of children each node can have depends on the type of heap, but in many types it is at most two, which is known as a binary heap.
我不明白为什么很多类型堆中的节点最多只有两个children?为什么三个children或四个children等等不常见?谢谢~
大多数类型的堆每个节点最多有两个 children 是不正确的,但是二叉堆是真的 - 最多有每个节点两个 children -- 是最常实现的类型。它是最常实现的类型,因为它很简单,cache-friendly,memory-efficient。
用于二叉堆的数据结构可以与每个节点不同数量的children一起使用。如果我们考虑 x 保持不变。然而,为了决定最好的 x,我们必须让它有所不同,在这种情况下,常见操作需要 O(x * log N / log x)次.
为了确定每个节点最有效的 children 数量,我们可以选择 x 来最小化因子 x/log x.
如果你绘制你可以看到每个节点的最佳 children 数量实际上是 3(最小值在 x=e,但我们需要一个整数):
...但是 2 和 3 之间的差异并不显着,并且代码更简单,每个节点使用 2 children,所以这是常见的做法。