一、引
面试被问到的题,懵逼~~~
二、介绍
一开始问的是缓存队列,那就直接拿个数组做,末尾插,头上取,很简单…然后就问了优先级队列,我先是一脸懵,然后才开始思考,可以用堆做。每个任务是有个权重信息,然后根据权重的高低进行排序(一般是最先取出最大权重的任务),所以可以做一个大顶堆,然后对堆进行调整、加入和取顶节点操作。
2.1 堆
很简单的就是可以拿个数组存,堆的层次遍历就是数组的顺序。同时堆的节点之间的关系就可以表示为:父节点(i),左子节点(i2),右子节点(i2+1)。
类似上面这个堆,就是 [10,14,25,33,81,82,99]
即可表示。
2.2 取
堆的作业中比较大的一环就是取值,大小顶堆的取值就是取上面的最值,具体的操作方式是:
- 把顶端的节点取走,顶端空缺
- 把尾节点,最下层的最右节点拿到顶端
- 从上往下调整堆
2.3 插
既然做优先级的缓存队列,那么任务是源源不断会来的,所以插入是必须的,具体的操作方式是:
- 把新节点放在末尾
- 递归判断该节点和其都节点的大小,根据该堆的性质
2.4 调整
调整是堆的最重要的一环了,大小顶锥的优点都是因为他的快速取得最大/小节点,那么堆的数据有变化的时候就要对堆进行操作,具体的操作方式是:比较最后面一棵子树双子节点和父节点的关系,然后根据堆的性质做出交换,直到不需要交换为止。
3. 实现
function heap(heap) {
let self = this
self.heap = heap;
//返回堆大小
self.size = function () {
return self.heap.length;
}
//调整为小丁堆
self.MinTopHeapSort = function () {
if (self.heap.length == 1) {
return;
} else {
for (let i = parseInt(self.heap.length / 2) - 1; i >= 0; i--) {
// 左子节点存在 且 父节点大于左子节点
if (2 * i + 1 < self.heap.length && self.heap[i] > self.heap[2 * i + 1]) {
[self.heap[i], self.heap[2 * i + 1]] = [self.heap[2 * i + 1], self.heap[i]]
if (2 * (2 * i + 1) + 1 < self.heap.length && self.heap[2 * i + 1] > self.heap[2 * (2 * i + 1) + 1]) {
self.MinTopHeapSort()
}
if (2 * (2 * i + 1) + 2 < self.heap.length && self.heap[2 * i + 2] > self.heap[2 * (2 * i + 1) + 2]) {
self.MinTopHeapSort()
}
}
// 右子节点存在 且 父节点大于右子节点
if (2 * i + 2 < self.heap.length && self.heap[i] > self.heap[2 * i + 2]) {
[self.heap[i], self.heap[2 * i + 2]] = [self.heap[2 * i + 2], self.heap[i]]
if (2 * (2 * i + 2) + 1 < self.heap.length && self.heap[2 * i + 1] > self.heap[2 * (2 * i + 2) + 1]) {
self.MinTopHeapSort()
}
if (2 * (2 * i + 2) + 2 < self.heap.length && self.heap[2 * i + 2] > self.heap[2 * (2 * i + 2) + 2]) {
self.MinTopHeapSort()
}
}
}
return;
}
}
//调整为大顶堆
self.MaxTopHeapSort = function () {
if (self.heap.length == 1) {
return;
} else {
for (let i = parseInt(self.heap.length / 2) - 1; i >= 0; i--) {
// 左子节点存在 且 父节点小于左子节点
if (2 * i + 1 < self.heap.length && self.heap[i] < self.heap[2 * i + 1]) {
[self.heap[i], self.heap[2 * i + 1]] = [self.heap[2 * i + 1], self.heap[i]]
if (2 * (2 * i + 1) + 1 < self.heap.length && self.heap[2 * i + 1] < self.heap[2 * (2 * i + 1) + 1]) {
self.MaxTopHeapSort()
}
if (2 * (2 * i + 1) + 2 < self.heap.length && self.heap[2 * i + 2] < self.heap[2 * (2 * i + 1) + 2]) {
self.MaxTopHeapSort()
}
}
// 右子节点存在 且 父节点小于右子节点
if (2 * i + 2 < self.heap.length && self.heap[i] < self.heap[2 * i + 2]) {
[self.heap[i], self.heap[2 * i + 2]] = [self.heap[2 * i + 2], self.heap[i]]
if (2 * (2 * i + 2) + 1 < self.heap.length && self.heap[2 * i + 1] < self.heap[2 * (2 * i + 2) + 1]) {
self.MaxTopHeapSort()
}
if (2 * (2 * i + 2) + 2 < self.heap.length && self.heap[2 * i + 2] < self.heap[2 * (2 * i + 2) + 2]) {
self.MaxTopHeapSort()
}
}
}
}
return;
}
//获取最大值
self.getMax = function () {
self.MaxTopHeapSort();
return {
value: self.heap.shift(1),
next: self.heap.length == 0 ? false : true
}
}
//获取最小值
self.getMin = function () {
self.MinTopHeapSort();
return {
value: self.heap.shift(1),
next: self.heap.length == 0 ? false : true
}
}
//堆中插入
self.push = function(key, kind){
self.heap.push(key)
if(kind ==1){
self.MaxTopHeapSort()
return;
} else{
self.MinTopHeapSort()
return;
}
}
//返回当前堆
self.data = function () {
return self.heap;
}
}
let queue = new heap([7,3,8,5,1,2]);
console.log(queue.data());
// [ 7, 3, 8, 5, 1, 2 ]
queue.MaxTopHeapSort();
console.log(queue.data());
// [ 7, 3, 8, 5, 1, 2 ]
queue.MinTopHeapSort();
console.log(queue.data());
// [ 1, 3, 2, 8, 5, 7 ]
queue.push(9,1)
console.log(queue.data());
// [ 9, 5, 8, 1, 3, 2, 7 ]
queue.push(10,2)
console.log(queue.data());
// [ 1, 3, 2, 9, 5, 8, 7, 10 ]
let next = true;
while(next){
let res = queue.getMin();
next = res.next;
console.log(res)
}
// { value: 1, next: true }
// { value: 2, next: true }
// { value: 3, next: true }
// { value: 5, next: true }
// { value: 7, next: true }
// { value: 8, next: true }
// { value: 9, next: true }
// { value: 10, next: false }