堆可以是一棵完整的树吗?
Can a heap be a complete tree?
如果我在节点 6 下插入一个新节点,树仍然是一个最小堆?
如果要插入的值至少为 6,那么是的,它仍然是 min-heap。如果该值小于 6,则不会是 min-heap。
然而,大多数堆插入算法将总是最初插入值,即在树的最低级别的下一个可用位置,或第一个,left-most 新关卡的位置,如果该关卡已满。然后插入过程将继续交换值,直到值 bubbled up 到 min-heap 属性 再次有效的位置。
将每个新元素添加到堆后,需要将该元素提升到根或高于其当前级别的任何特定级别,直到它下面的所有节点都大于它。
阅读您的评论后:
默认情况下,堆是一个完整的二叉树,据我了解,您可能在谈论完整的二叉树。
binary heap 上的维基百科文章说:
A binary heap is defined as a binary tree with two additional
constraints:[3]
- Shape property: a binary heap is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are
fully filled, and, if the last level of the tree is not complete, the
nodes of that level are filled from left to right.
- Heap property: the key stored in each node is either greater than or equal to (≥) or less than or equal to (≤) the keys in the node's
children, according to some total order.
如果一个堆不能是完全二叉树,那么就不可能有一个只有一个元素的堆。因为只有一个节点的二叉树是完全二叉树。
二叉堆的最后一层不能完整的想法是错误的。如果存在该限制,数学将无法计算。
是一个最小堆 :
这棵树的所有根节点都小于它们的子节点。
它调用几乎完整的二叉树 :
在这棵树中(节点=6)没有两个子树,这就是为什么我们不能称之为完整的二叉树。
- 但是,(node=6)有一个left子树,所以我们可以称这个"ALMOST BINARY COMPLETE TREE"
堆可以是完全二叉树,也可以是几乎完全二叉树。
如果你加上一个数,并且这个数>6就变成了完全二叉树。
谢谢。
如果我在节点 6 下插入一个新节点,树仍然是一个最小堆?
如果要插入的值至少为 6,那么是的,它仍然是 min-heap。如果该值小于 6,则不会是 min-heap。
然而,大多数堆插入算法将总是最初插入值,即在树的最低级别的下一个可用位置,或第一个,left-most 新关卡的位置,如果该关卡已满。然后插入过程将继续交换值,直到值 bubbled up 到 min-heap 属性 再次有效的位置。
将每个新元素添加到堆后,需要将该元素提升到根或高于其当前级别的任何特定级别,直到它下面的所有节点都大于它。
阅读您的评论后: 默认情况下,堆是一个完整的二叉树,据我了解,您可能在谈论完整的二叉树。
binary heap 上的维基百科文章说:
A binary heap is defined as a binary tree with two additional constraints:[3]
- Shape property: a binary heap is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.
- Heap property: the key stored in each node is either greater than or equal to (≥) or less than or equal to (≤) the keys in the node's children, according to some total order.
如果一个堆不能是完全二叉树,那么就不可能有一个只有一个元素的堆。因为只有一个节点的二叉树是完全二叉树。
二叉堆的最后一层不能完整的想法是错误的。如果存在该限制,数学将无法计算。
是一个最小堆 :
这棵树的所有根节点都小于它们的子节点。
它调用几乎完整的二叉树 :
在这棵树中(节点=6)没有两个子树,这就是为什么我们不能称之为完整的二叉树。
- 但是,(node=6)有一个left子树,所以我们可以称这个"ALMOST BINARY COMPLETE TREE"
堆可以是完全二叉树,也可以是几乎完全二叉树。
如果你加上一个数,并且这个数>6就变成了完全二叉树。
谢谢。