3. 图解-什么是堆

1. 完全二叉树 Complete binary tree

  • Complete binary tree = a binary tree where all levels, except possibly the last level are completely filled with nodes

    Furthermore: the last level has all its nodes to the left side

  • 完全二叉树= 除最后一层外,每一层上的所有结点都有两个子结点的二叉树,

  •                       而且最后一层所有节点都在左边

   完全二叉树例子:

 

 

2.什么是堆

堆是具有以下性质的完全二叉树:每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆;

或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆。如下图:

最小堆

    因为:

     1  节点1 的值 是节点1所在子树节点中最小的 

     

  

     2  节点5的值 是节点5所在子树中最小的

   

 

  3    节点6的值 是节点6所在子树中最小的

       

3.优先队列 priority queue

     用堆表示优先队列,堆经常被叫做优先队列。

       优先队列并不是队列,而是树。Java中,PriorityQueue就是堆(默认是小堆)

       在优先队列中,元素被赋予优先级。具有最高级的元素先出 (first in, highest priority out)

  • The job with the highest priority will always be stored in the root node of a heap
  • 最高优先级的项目总是存储在堆的根节
  • The heap data structure is often use to store jobs that need to be ranked by their priorities
  • 堆    数据结构经常被用来存储需要按按优先级别排序的数据

4.参考

http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-intro.html

猜你喜欢

转载自blog.csdn.net/wengyupeng/article/details/84260353
3.