关于堆(最大堆和最小堆)
我有一个问题,在堆数据结构中,左孩子可以比其所在级别的右孩子多吗?我的意思是,考虑这三个数字 9、5、8,我想创建一个最大堆数据结构,因此根将为 9,并且 8 是其左子节点,5 是其右子节点,这是真的吗? 请帮助我,谢谢
I have this question that in the heap data structure , a left child can be more than a right child in its own level ? I mean that consider these three numbers 9,5,8 and I want to make a max-heap data structure so the root will be 9 and is it true that 8 be its left child and 5 be its right child?
please help me thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最大堆属性:
最小堆属性:
因此,堆不适合搜索,但它用于对元素数组进行排序,因为搜索需要线性 O(n) 时间。
对于搜索,我们总是可以使用二叉搜索树(BST),它在 O(h) 时间内完成同样的事情。在最好的情况下,如果 BS 树是平衡的,它将在 O(logn) 中进行搜索。
Max-Heap properties:
Min-Heap properties:
Hence, Heap is not suitable for searching but it is used for sorting an array of elements, because searching takes linear O(n) time.
For searching, we could always go for Binary Search trees(BSTs) which does the same thing in O(h) time. And in best case, it would do the searching in O(logn) if the BS tree is balanced.
那没关系。最大堆中的节点必须具有较低的子节点,而最小堆中的节点必须具有较大的子节点。这是唯一的要求。
That doesn't matter. A node in a max-heap must have children that are lower, and a node in a min-heap must have children that are greater. Those are the only requirements.