大小堆 排序

来源:互联网 发布:数据库技术发展 编辑:程序博客网 时间:2024/06/02 04:20


From:Binary Min — Max Heap  andexample of heapSort


A binary heap is a heap data struc­ture cre­ated using a binary tree.

binary tree has two rules -

  1. Binary Heap has to be com­plete binary tree at all lev­els except the last level. This is calledshape prop­erty.
  2. All nodes are either greater than equal to (Max-Heap) or less than equal to (Min-Heap) to each of its child nodes. This is calledheap prop­erty.

Imple­men­ta­tion:

  • Use array to store the data.
  • Start stor­ing from index 1, not 0.
  • For any given node at posi­tion i:
  • Its Left Child is at [2*i] if available.
  • Its Right Child is at [2*i+1] if available.
  • Its Par­ent Node is at [i/2]if avail­able.




0 0