gpt4 book ai didi

c++ - 索引 : Implementing Tree data structures with Arrays/Vectors

转载 作者:太空宇宙 更新时间:2023-11-04 15:53:34 26 4
gpt4 key购买 nike

我一直在使用 vector 在 C++ 中实现堆。因为我必须轻松访问节点 (2n, 2n+1) 的子节点,所以我必须从索引 1 开始。这是正确的方法吗?根据我的实现,在第 0 个位置总是有一个虚拟元素。

最佳答案

您的方法行得通。或者,您可以在索引 0 处拥有 root 并在 2n+12n+2

处拥有 child

关于c++ - 索引 : Implementing Tree data structures with Arrays/Vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3750483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com