在JDK1.8中的PriorityQueue底层使用了堆的数据结构,而堆实际就是在完全二叉树的基础之上进行一些元素的调整
如果有一个关键码的集合K={k0,k1,k2,k3,…,kn-1},把他的所有元素按完全二叉树的顺序存储方式存储,在一个一维数组中,并满足:Ki <= K2i + 1 且 Ki <= K2i + 2(Ki >= K2i + 1且 Ki >= K2I + 2)i = 0,1,2…,则称之为小堆(或大堆)。将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆
堆的性质:
- 堆中某个节点的值总是不大于或不小于其父节点的值;
- 堆总是一棵完全二叉树
堆的存储方式:
从堆的概念可知,堆是一棵完全二叉树,因此可以层序的规则采用顺序的方式来高效存储
将元素存储到数组中后,可以根据二叉树章节的性质5对树进行还原。假设i为节点在数组中的下标,则有:
- 如果i为0,则i表示的节点为根节点,否则i节点的双亲节点为 (i - 1)/2
- 如果2 * i + 1 小于节点个数,则节点i的左孩子下标为2 * i + 1,否则没有左孩子
- 如果2 * i + 2 小于节点个数,则节点i的右孩子下标为2 * i + 2,否则没有右孩子
堆的实现:
需要向下调整:
-
让parent标记需要调整的节点,child标记parent的左孩子(注意:parent如果有孩子一定先是有左孩子)
-
如果parent的左孩子存在,即:child < size, 进行以下操作,直到parent的左孩子不存在
parent右孩子是否存在,存在找到左右孩子中最小的孩子,让child进行标 将parent与较小的孩子child比较,如果: parent小于较小的孩子child,调整结束 否则:交换parent与较小的孩子child,交换完成之后,parent中大的元素向下移动, 可能导致子树不满足对的性质,因此需要继续向下调整, 即parent = child;child = parent*2+1; 然后继续2
堆的创建,从最后一个节点的父亲开始,依次往前调整每一个节点,进行向下调整
堆的插入,插入到队尾进行向上调整
堆的删除,把堆头的元素和堆尾交换,然后堆尾的元素被丢弃,从堆头进行向下调整
实现代码:
(大根堆)
public class TestHeap {
public int[] elem;
public int usedSize;
public TestHeap() {
this.elem = new int[10];
}
/**
* 时间复杂度:O(logn)
* @param root 每棵子树的开始位置
* @param len 结束位置
*/
public void adjustDown(int root,int len) {
int parent = root;
int child = 2*parent+1;
while (child < len) {
//0、判断是否有左右孩子 有的话 找到最大值 C下标表示最大值下标
if(child+1 < len && this.elem[child] < this.elem[child+1]) {
child++;
}
//代码指向到这里,c表示最大值下标
if(this.elem[child] > this.elem[parent]) {
//交换
int tmp = this.elem[child];
this.elem[child] = this.elem[parent];
this.elem[parent] = tmp;
parent = child;
child = 2*parent+1;
}else {
break;
}
}
}
//O(n)
public void createHeap(int[] array) {
for (int i = 0; i < array.length; i++) {
this.elem[i] = array[i];
this.usedSize++;
}
//i:每棵子树的根节点下标
for (int i = (this.usedSize-1-1)/2; i >= 0 ; i--) {
adjustDown(i,this.usedSize);
}
}
/**
* 插入数据:
* @param val
*/
public void push(int val) {
//0、堆是否是满的--》扩容
if(isFull()) {
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
//1、放到数组的最后一个位置
this.elem[this.usedSize] = val;
this.usedSize++;//11
//2、进行调整
adjustUp(this.usedSize-1);
}
public void adjustUp(int child) {
int parent = (child-1)/2;
while (child > 0) {
if(this.elem[child] > this.elem[parent]) {
int tmp = this.elem[child];
this.elem[child] = this.elem[parent];
this.elem[parent] = tmp;
child = parent;
parent = (child-1)/2;
}else {
break;
}
}
}
public boolean isFull() {
return this.usedSize == this.elem.length;
}
public void pop() {
//0、是否是空的
if(isEmpty()) {
return;
}
//1、最后一个元素和堆顶元素交换
int tmp = this.elem[0];
this.elem[0] = this.elem[this.usedSize-1];
this.elem[this.usedSize-1] = tmp;
this.usedSize--;//10 9
//2、调整0号下标这棵树
adjustDown(0,this.usedSize);
}
public boolean isEmpty() {
return this.usedSize == 0;
}
public int peek() {
if(isEmpty()) {
return -1;
}
return this.elem[0];
}
/**
* 时间复杂度:O(n*logn)
* 空间复杂度:O(1)
*/
public void heapSort() {
int end = this.usedSize-1;
while (end > 0) {
int tmp = this.elem[0];
this.elem[0] = this.elem[end];
this.elem[end] = tmp;
adjustDown(0,end);
end--;
}//O(n*logn)
}
public void show() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
}