Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试将基于列表的树实现转换为基于数组的实现,父在第 i 个索引处,左子在第 2 个索引处,右子在第 2i+1 个索引处。由于某种原因,转换会导致节点数较多的树的数据丢失。我想知道在实现这一点时我需要检查的所有边界条件。谢谢!
假设您的语言使用从零开始的索引,则 node 的子级i进入2i + 1and 2i + 2not 2iand 2i + 1。后者适用于基于 1 的索引。
i
2i + 1
2i + 2
2i
你把头放在0还是1?选择“0”肯定会出问题,除非你调整你的公式。