最后
由于细节内容实在太多了,为了不影响文章的观赏性,只截出了一部分知识点大致的介绍一下,每个小节点里面都有更细化的内容!
小编准备了一份Java进阶学习路线图(Xmind)以及来年金三银四必备的一份《Java面试必备指南》
}
如果使用向上调整,我们在建堆的时候遍历数组要从后到前遍历。
int[] array1 = new int[] {9,5,2,7,3,6,8};
for (int i = array1.length - 1; i >= 0; i --) {
shiftUp(array1, array1.length ,i);
}
小堆的操作和实现:
//堆的向下调整
public static void shiftDown(int[] array, int size, int index) {
int patent = index;
int child = 2 * patent + 1;
while (child < size) {
if(child + 1 < size && array[child + 1] < array[child]) {//取俩个结点的最小值
child += 1;
}
if(array[child] < array[patent]) {//孩子的值小于parent的值
int temp = array[child];
array[child] = array[patent];
array[patent] = temp;
}else {
break;
}
patent = child;
child = 2 * patent + 1;
}
}
//向上调整
public static void shiftUp(int[] array, int size, int index) {
int child = index;
int parent = (child - 1)/2;
while (child > 0) {
if(array[child] < array[parent]) {//孩子的值小于parent的值
int temp = array[child];
array[child] = array[parent];
array[parent] = temp;
}else {
break;
}
child = parent;
parent = (child - 1)/2;
}
}
建大堆是每次循环找大的值,而建小堆就是每次循环找小的值。
同理建小堆的时候用向下调整还是会出现失败反例(最小值出现在后几个数字),而使用向上调整是不会出现这种情况的。
**建小堆的时候用向下调整还是从前到后遍历数组。(因为index是根结点)
使用向上调整建立小堆的时候还是从后到前遍历数组。(因为index是最后一个叶子结点)**
优先级队列的实现:
优先级队列本质还是队列只是我们在实现offer和poll操作的时候分别进行了向上调整和向下调整。(当然我们使用建大堆的向上和向下调整的话是值最高的优先级最高,最先出队列)
public class MyPriorQueue {
private int[] array = new int[100];
private int size = 0;
public void offer(int data) {
this.array[size] = data;
size ++;
shiftUp(array, size, size - 1);
}
private void shiftUp(int[] array, int size, int index) {
int child = index;
int parent = (child - 1)/2;
while (child > 0) {
if(array[child] > array[parent]) {
int temp = array[child];
array[child] = array[parent];
array[parent] = temp;
}else {
break;
}
child = parent;
parent = (child - 1)/2;
}
}
public Integer poll() {
if(size <= 0) {
return null;
}
int ret = this.array[0];
this.array[0] = this.array[size - 1];
size --;
shiftDown(array, size, 0);
return ret;
}
private void shiftDown(int[] array, int size, int index) {
int parent = index;
int child = 2 * parent + 1;
while (child < size) {
if(child + 1 < size && array[child] < array[child + 1]) {
child += 1;
}
if(array[child] > array[parent]) {
int temp = array[child];
array[child] = array[parent];
array[parent] = temp;
}else {
break;
}
parent = child;
child = 2 * parent + 1;
}
}
public Integer peek() {
if(size <= 0) {
return null;
}
return array[0];
}
public boolean empty() {
return size == 0;
}
/* public void display() {
for (int i:this.array
) {
System.out.print(i + " ");
}
System.out.println();
}*/
public int length() {
return size;
}
}
**时间复杂度分析:
入队列-》向上调整O(logN)
出队列-》向下调整O(logN)
取队首元素O(1)
建堆操作的时间复杂度是O(N)**
**PriorityQueue 的使用:
(也就是库优先级队列)**
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<>();
int[] nums = new int[] {3, 7, 4, 8, 3, 2, 9};
for (int i:nums
) {
pq.offer(i);
}
while (!pq.isEmpty()) {
System.out.print(pq.poll() + " ");
}
}
**我们要注意库函数里的优先级队列使用的是建小堆的向上调整和向下调整。
库里的原始优先级队列遵循值越小,优先级越高思想。
当然我们也可以自己定义一个比较器对象,借助比较器我们自己定义谁优先谁不优先。**
static class comp implements Comparator {
@Override
public int compare(Integer o1, Integer o2) {
// return o1 - o2;
return o2 - o1;
}
}
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<>(new comp());
int[] nums = new int[] {3, 7, 4, 8, 3, 2, 9};
for (int i:nums
) {
pq.offer(i);
}
while (!pq.isEmpty()) {
System.out.print(pq.poll() + " ");
}
}
TopK 问题、堆排序:
**只要掌握了优先级队列,这两问题是很简单的。
我们知道如果我们使用建大堆的调整思想,我们每次出队列的一定是当前队列里优先级最高的,也就是值最大的。**
所以TopK我们只要出K次队列即可。
堆排序就是挨个出队列,就是这么简单。
总结
上述知识点,囊括了目前互联网企业的主流应用技术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。
很多人担心学了容易忘,这里教你一个方法,那就是重复学习。
打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。
从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。
人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。
或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。
从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。
[外链图片转存中…(img-gHtYXjdj-1715291130924)]
人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。