美文网首页
2020-07-14(堆)

2020-07-14(堆)

作者: swagsmile | 来源:发表于2020-07-15 09:51 被阅读0次

定义:Heap is a binary tree with two special properties: it must have all its nodes in specific order and its shape must be complete.

注意:
Keep in mind

  • We can have duplicate values in a heap — there’s no restriction against that.
  • A heap doesn’t follow the rules of a binary search tree; unlike binary search trees, the left node does not have to be smaller than the right node! The ordering of the child nodes isn’t important for a heap; the only ordering that matters is the heap-order property, or the ordering of parent nodes compared to their children.

大根堆 小根堆

Heap can be broadly classified in two types :

1. Min heap :

A min heap is a heap where every single parent node, including the root, is less than or equal to the value of its children nodes.
The most important property of a min heap is that the node with the smallest, or minimum value, will always be the root node.


Min Heap.png

2. Max heap:

A max heap is effectively the converse of a min heap; in this format, every parent node, including the root, is greater than or equal to the value of its children nodes.
The important property of a max heap is that the node with the largest, or maximum value will always be at the root node.


Max heap.png

Implementation

  • Use array to store the data.
  • Start storing from index 1, not 0.
  • For any given node at position i:
    Its Left Child is at [2i] if available.
    Its right child is at [2
    i+1] if available.
    Its Parent Node is at [i/2] if available.
  • Heap Majorly has 3 operations –
    Insert Operation(Time complexity O(log n))
    Delete Operation (Time complexity O(log n))
    Extract-Min (OR Extract-Max) (Time complexity O(n))

Bubble-up Operation(往上冒泡)

Steps:
If inserted element is smaller than its parent node in case of Min-Heap OR greater than its parent node in case of Max-Heap, swap the element with its parent.
Keep repeating the above step, if node reaches its correct position, STOP.


Insert-Bubble-Up-Min-Heap.gif

Sink-Down Operation(往下沉落)

Steps:
If replaced element is greater than any of its child node in case of Min-Heap OR smaller than any if its child node in case of Max-Heap, swap the element with its smallest child(Min-Heap) or with its greatest child(Max-Heap).
Keep repeating the above step, if node reaches its correct position, STOP.


Delete-OR-Extract-Min-from-Heap.gif

参考链接:https://iq.opengenus.org/max-heap-min-heap/

相关文章

  • 2020-07-14(堆)

    定义:Heap is a binary tree with two special properties: it ...

  • uni-app获取今天之前的7天时间

    今天[2020-07-16],过去的7天时间,时间格式["2020-07-15", "2020-07-14", "...

  • 逻辑

    2020-07-14 洪水逻辑:水利,防汛编织袋,农业 2020-07-15 跨省旅游开放:旅游股异动 2020-...

  • office2019 for Mac v16.30 Catali

    本文章更新于“2020-07-14” 1.Mac OS 系统更新后特地测试了一下。目前 Catalina 10.1...

  • 衣物包养

    【穿搭记】衣物保养二 私人首席形象设计师_小夏 关注 字数 750 · 阅读 46 2020-07-14 23:3...

  • 【D201】往后余生,与临在作伴——写作营共读打卡第168天《当

    2020-07-14,周二,晴 今天阅读《当下的力量》第六章。 Day168《往后余生,与临在作伴》 ——写作营第...

  • 关于做好2020年东阳市教师招聘体检工作的公告

    关于做好2020年东阳市教师招聘体检工作的公告 发布日期:2020-07-14 来源:东阳市教育局 字号:[ 大 ...

  • 心有不甘而无能为力

    2020-07-14 人生最无奈的事,就是心有不甘而无能为力。 人生总结,以后工作上的事情,不要跟家人抱怨,真的,...

  • 0715

    2020-07-14陆小白晨间日记 今天是什么日子:2020年7月14日 日出: 5点00日落:6:00 起床: ...

  • 周记

    2020-07-14 这周过的出奇的快。 消化之前的事情。 施工图依然是个难啃的骨头。软装接近尾声,也到了要检验的...

网友评论

      本文标题:2020-07-14(堆)

      本文链接:https://www.haomeiwen.com/subject/volqhktx.html