堆的左右children等式准确吗?

Are the equations of right and left children of heap accurate?

在堆数据结构中,做两个等式

left = 2i+1
right = 2i+2

应用于任何给定的堆?如果不是,它们在什么条件下才有效?

根据D.S。马利克 "A heap is a list in which each element contains a key, such that the key in the element at position k in the list is at least as large as the key in the element at position 2k + 1 (if it exists) and 2k + 2 (if it exists)."

您还应注意元素在列表中的位置:"in general, for the node k,which is the k-1th element of the list, its left child is the 2kth (if it exists) element of the list, which is at position 2k-1 in the list, and the right child is the 2k + 1st (if it exists) element of the list, which is at position 2k in the list."

在学习时,我发现实际输入一些索引并确保您得到您希望的元素很有帮助。祝你好运。

这取决于根在哪里。如果根在数组中的索引 0 处,则您的方程式是正确的。左节点位于索引 2i + 1,右节点位于索引 2i + 2.

许多示例在数组中的索引 1 处都有根。在这种情况下,左子节点位于索引 2i,右节点位于索引 2i + 1.