7x-Heap Sort

叶子结点 就是出度为0的结点 就是没有子结点的结点 近二叉树:不能满的二叉树 一、binary tree 1,binary tree image1

2,Complete Binary Tree 定义:完备二叉树是指除了最后一层之外的每一层都是满的,并且所有节点都在尽可能远的左边的二叉树

堆:完全二叉树+父节点>子节点(父节点\<子节点) | max heap | 父>子 | |----------|--------| | Min heap | 父\<子 | . The heap can be represented by a binary tree or array.

3,Heap Property 3.1 a nearly complete binary tree with “heap property”:父节点>两个子节点 注意序号 3.2 image2 从一开始

4,Tree height:从下往上; depth:从上往下 image3

5,binary tree facts

![image4](../../assets/bb60ae89dbbb42f994d494ab58b05164.png)

![image5](../../assets/95f90653531845dcbd51b3ef64a7da67.png)

![image6](../../assets/50b34c644f0f4eadaff13e89cc5520cd.png)

![image7](../../assets/773b83b36ab04b1c93d76cbf3d59dda0.png)

![image8](../../assets/db9eda9f236941dc97f366f45f6b0633.png)

![image9](../../assets/27e3e2da4ece4fdc8702d51abe5e95ef.png)

![image10](../../assets/727c5d8a7759430895bb7dc6497d2a78.png)

![image11](../../assets/4efaf9c6daa24b8a8d6f65274992a3a3.png)

数学公式 image12

image13

二、Heap Sort
1, image14

2,HEAPIFY[调整为heap tree]

![image15](../../assets/1feec142dfb545fa9fa5682cdaff067a.png)

![image16](../../assets/b8aec7b4adf748e8af9bc756f937af06.png)

![image17](../../assets/6ebe15fa1b2e452e83d9271a7f6dc484.png)

步骤 A is a heap 1,把A中最大的放到B 2,把最小的放到A[1] 3,调整剩余的node,【除了当前最大的】,直到满足heap 性质 4,重复1-3步直到A空 5,B就是从大到小排列好的数组

3,两个缺点 1,need a lot of extra space=O(1)

2,移动元素后的tree很可能不满足堆性质

image18 4,heapify例子

![image19](../../assets/b343f96efff2460aa7c927dc94eaa4b4.png)

![image20](../../assets/41e4866d1fb24671af17bf0c7278c222.png)

![image21](../../assets/09271f9013f844a284d9246d9dd4cc99.png)

5,时间复杂度

![image22](../../assets/c988234c57404be2b62a2b273757bbdf.png)

![image23](../../assets/fd5aa43dd75c45b78543bb9b4095c958.png)

6,construct a heap tree image24 方法1:top-down O(nlgn) Extend the heap tree by add nodes one by one 从上至下,从左到右,一边加点一边调整结构 image25 Then we add A[X] to this tree and do "floating up"

image26

方法2:bottom up O(n) 从下往上,从右到左,开始构建 Use bottom up to construct heap ➔ 𝑂(𝑛) image27

image28

时间复杂度:

image29

image30

image31

7,堆排序时间复杂度 image32

三、Priority queue image33

image34