堆(Heap)和二叉堆(Binary heap)

堆(Heap

The operations commonly performed with a heap are:

  • create-heap: create an empty heap
  • heapify: create a heap out of given array of elements
  • find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap (aka, peek)
  • delete-max or delete-min: removing the root node of a max- or min-heap, respectively
  • increase-key or decrease-key: updating a key within a max- or min-heap, respectively
  • insert: adding a new key to the heap
  • merge: joining two heaps to form a valid new heap containing all the elements of both.
  • meld(h1,h2): Return the heap formed by taking the union of the item-disjoint heaps h1 and h2. Melding destroys h1 and h2.
  • size: return the number of items in the heap.
  • isEmpty(): returns true if the heap is empty, false otherwise.
  • ExtractMin() or ExtractMax(): Returns the node of minimum value from a min heap [or maximum value from a max heap] after removing it from the heap
  • Union(): Creates a new heap by joining two heaps given as input.
  • Shift-up: Move a node up in the tree, as long as needed (depending on the heap condition: min-heap or max-heap)
  • Shift-down: Move a node down in the tree, similar to Shift-up

 二叉堆(Binary heap

binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:

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
All nodes are  either greater than or equal to or less than or equal to each of its children, according to a comparison predicate defined for the heap.

Heaps with a mathematical "greater than or equal to" (≥) comparison predicate are called max-heaps; those with a mathematical "less than or equal to" (≤) comparison predicate are called min-heaps. Min-heaps are often used to implement priority queues.

堆的两个特性:

  • 结构性:用数组表示的完全二叉树;
  • 有序性:任一结点的关键字是其子树所有结点最大值(或最小值);最大堆(MaxHeap)也称为“大顶堆”;最小堆(MinHeap)也称为“小顶堆”;

最大堆和最小堆:

堆(Heap)和二叉堆(Binary heap)_第1张图片

不是堆:

堆(Heap)和二叉堆(Binary heap)_第2张图片

注意:从根结点到任意结点路径上结点序列的有序性!

 


 【堆的抽象数据类型描述】

类型名称:最大堆(MaxHeap)

数据对象集:完全二叉树,每个结点的元素值不小于其子结点的元素值

操作集:最大堆H ∈ MaxHeap,元素item ∈ ElementType,主要操作有:

  • MaxHeap Create(int MaxSize):创建一个空的最大堆;
  • Boolean IsFull(MaxHeap H):判断最大堆H是否已满;
  • Insert(MaxHeap H,ElementType item):将元素item插入最大堆H;
  • Boolean IsEmpty(MaxHeap H):判断最大堆H是否为空;
  • ElementType DeleteMax(MaxHeap H):返回H中最大元素(高优先级);

【最大堆的创建】

 

【最大堆的插入】

 

【最大堆的删除】

 

【最大堆的建立】

 

你可能感兴趣的:(binary)