堆排序获取父级

发布于 2024-12-12 14:11:38 字数 372 浏览 4 评论 0原文

要获取堆排序数组中节点的父节点,计算公式为 (index - 1) / 2。

但是,假设每个节点在数组中占用 4 个空格。要访问记录/节点,我必须访问该记录的四个记录的第一个位置。下面是一个示例:

如果父级从位置 4 开始,则左子级从位置 12 开始,右子级从位置 16 开始。获取左子级的方程式为 2*position + 4获得右子节点的方程为2*position + 8

获得父级的方程式是什么?我需要一个方程来获取左子节点或右子节点的父节点,与 index-1/2 的方式相同。这可能吗?如果我需要两个单独的方程,那是行不通的,因为我真的不知道记录是左孩子还是右孩子。

谢谢你,

To get a parent of a node in a heap sort array, the calculation would be (index - 1) / 2.

However, assume each node takes up 4 spaces in the array. To access a record/node, I must access the first position of the four of that record. So here's an example:

If a parent starts at position 4, the left child starts at position 12 and the right child starts at position 16. The equation to get the left child would be 2*position + 4 and the equation to get the right child would be 2*position + 8.

What would the equation be to get the parent though? I need one equation to get the parent of either the left child or the right child, the same way index-1/2 does. Would that be possible? If I need two separate equations, that wouldn't work since I don't really know if a record is a left or a right child.

Thank you,

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

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

发布评论

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

评论(2

一梦浮鱼 2024-12-19 14:11:39

查看以下 C# 最小堆

    public int GetParent(int i, out int index)
    {
        double element = i;
        index = (int)Math.Floor((element - 1)  / 2);

        if(index < 0)
        {
            return 0;
        }

        return _collection[index];
    }

这适用于常规堆实现。

您可以在此处查看 MinHeap 实现 的其余部分。

通常,堆结构由数组支持,有趣的是,堆希望从数组开始1 使计算父母和孩子变得容易。

如果您想修改子级、父级,正如您在问题中提到的那样,您应该考虑将 4 个节点包装在一个节点中,但保持堆的工作方式不变

Look at the following C# Min heap.

    public int GetParent(int i, out int index)
    {
        double element = i;
        index = (int)Math.Floor((element - 1)  / 2);

        if(index < 0)
        {
            return 0;
        }

        return _collection[index];
    }

This works for regular heap impl.

you can see the rest of the MinHeap implementation here.

Usually, heap structure is backed with an array and interestingly, heap wants to start from Array1 to make calculating parent and childs easy.

if you want to modify the child, parent as you mentioned in your problem, you should consider wrapping your 4 nodes in a node but keep how heap works the way it is

伊面 2024-12-19 14:11:39

您应该将第一个索引(或者在本例中为前四个索引)留空;这简化了计算。因此根应位于 4-7,根的子级应位于 8-11 和 12-15,依此类推。鉴于此,节点的父节点位于 index / 8 * 4。如果必须让根在0-3处,可以使用(index + 4) / 8 * 4 - 4

(但是,您应该考虑将四个相关元素包装在一个类中,以便更容易使用它们,并且一次可以处理一个数组元素。)

You should leave the first index (or, in this case, the first four indices) blank; this simplifies the calculations. So the root should be at 4-7, the root's children at 8-11 and 12-15, and so on. Given this, a node's parent is at index / 8 * 4. If you have to let the root be at 0-3, you can use (index + 4) / 8 * 4 - 4.

(However, you should consider wrapping the four related elements in a class so that it will be easier to work with them and you can handle a single array element at a time.)

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