如何创建堆?

发布于 2024-11-16 23:26:09 字数 567 浏览 10 评论 0原文

假设我有一个如下所示的堆:

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55

现在,我想将另一个项目 55 插入到这个堆中。

如何做到这一点?

选项 1.

         77
        /  \
       /    \
      55    60
     / \    / \
    50 30  44 55
   /
  22

选项 2.

     77
    /  \
   /    \
  55    60
 / \    / \
22 50  44 55
     \
     30

选项 3.

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  55 55
      /
     44

哪一个是正确的步骤?为什么?请解释一下。

Suppose I have a Heap Like the following:

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55

Now, I want to insert another item 55 into this Heap.

How to do this?

Option 1.

         77
        /  \
       /    \
      55    60
     / \    / \
    50 30  44 55
   /
  22

Option 2.

     77
    /  \
   /    \
  55    60
 / \    / \
22 50  44 55
     \
     30

Option 3.

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  55 55
      /
     44

Which is the correct step? And Why? Please explain.

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

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

发布评论

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

评论(3

淑女气质 2024-11-23 23:26:09

选项 1 是正确的..
因为我们从最左边的节点开始添加子节点,如果父节点低于新添加的子节点,那么我们将替换它们。如此下去,直到孩子得到比它更有价值的父母。

您的初始树

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55

现在根据最左侧的规则添加 55。

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55
/
55

但你看到 22 低于 55,所以将其替换。

       77
      /  \
     /    \
    50    60
   / \    / \
  55 30  44 55
 /
22 

现在 55 已经成为 50 的孩子,仍然低于 55,所以也替换它们。

       77
      /  \
     /    \
    55    60
   / \    / \
  50 30  44 55
 /
22

现在不能再排序了,因为 77 大于 55 ...
所以你的选项1是正确的。

在这里您可以详细查看堆排序示例。
此链接指出
堆是一种特殊的基于树的数据结构,它满足堆属性:如果 B 是 A 的子节点,则 key(A) ≥ key(B)。这意味着具有最大键的元素始终位于根节点中,因此这样的堆有时称为最大堆。 (或者,如果比较相反,则最小元素始终位于根节点中,这会产生最小堆。)对于堆中每个节点有多少个子节点没有限制,尽管实际上每个节点都有最多两个。

祝你好运

Option 1 is right..
Because we start adding child from the most left node and if the parent is lower than the newly added child than we replace them. And like so will go on until the child got the parent having value greater than it.

Your initial tree is

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55

Now adding 55 according to the rule on most left side.

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55
/
55

But you see 22 is lower than 55 so replaced it.

       77
      /  \
     /    \
    50    60
   / \    / \
  55 30  44 55
 /
22 

Now 55 has become the child of 50 which is still lower than 55 so replace them too.

       77
      /  \
     /    \
    55    60
   / \    / \
  50 30  44 55
 /
22

Now it cant be sorted more because 77 is greater than 55 ...
SO your option 1 is right.

here you can see heap sort example in detail..
This link states
a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.) There is no restriction as to how many children each node has in a heap, although in practice each node has at most two.

Good Luck

妄断弥空 2024-11-23 23:26:09

选项1,根据定义二叉堆的形状是完全二叉树。另外两个不是完全二叉树。
请参阅http://en.wikipedia.org/wiki/Binary_heap

Option 1, by definition binary heap's shape is a complete binary tree. The other 2 are not complete binary trees.
See http://en.wikipedia.org/wiki/Binary_heap

笑饮青盏花 2024-11-23 23:26:09

正确选项是1.

为什么?

请记住,堆的一个属性是完整的二叉树,即它们的所有级别(可能除了最后一个级别)都被完全填充,并且所有节点都尽可能远离......维基百科?

在选项2和3中,元素没有插入到尽可能靠左的位置,因此完全二叉树的性质被破坏。

关于元素的最终位置,是将插入的元素(儿子)与其直接祖先(父亲)交换,而儿子小于父亲。

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55


       77
      /  \
     /    \
    50    60
   / \    / \
  22 30  44 55
 /
55


       77
      /  \
     /    \
    50    60
   / \    / \
  55 30  44 55
 /
22


       77
      /  \
     /    \
    55    60
   / \    / \
  50 30  44 55
 /
22

The correct option is 1.

Why?

Remember than one property of a heap is to be a complete binary tree, i.e. all of their levels, except possibly the last, is completely filled, and all nodes are as far left as possible...wikipedia?

In options 2 and 3 the element is not inserted as far left as possible, and, therefore the property of complete binary tree is broken.

With respect to the final position of the element is by exchanging the inserted element(son) with their direct ancestor(father) while the son is less than the father.

     77
    /  \
   /    \
  50    60
 / \    / \
22 30  44 55


       77
      /  \
     /    \
    50    60
   / \    / \
  22 30  44 55
 /
55


       77
      /  \
     /    \
    50    60
   / \    / \
  55 30  44 55
 /
22


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