JavaScript数据结构与算法(三) 优先级队列的实现

时间:2022-12-28 16:15:52

TypeScript方式实现源码

 1 // Queue类和PriorityQueue类实现上的区别是,要向PriorityQueue添加元素,需要创建一个特殊的元素。这个元素包含了要添加到队列的元素(它可以是任意类型)及在队列中的优先级
 2 class QueueElement {
 3     element;
 4     priority;
 5     constructor(element, priority) {
 6         this.element = element;
 7         this.priority = priority;
 8     }
 9 }
10 class PriorityQueue {
11     items = [];
12     public enqueue(element, priority) {
13         let queueElement = new QueueElement(element, priority);
14 
15         if (this.isEmpty()) {
16             this.items.push(queueElement);
17         } else {
18             let added = false;
19             for (let i = 0; i < this.items.length; i++) {
20                 if (queueElement.priority < this.items[i].priority) {
21                     this.items.splice(i, 0, queueElement);
22                     added = true;
23                     break;
24                 }
25             }
26             if (!added) {
27                 this.items.push(queueElement);
28             }
29         }
30     }
31     public dequeue() {
32         return this.items.shift();
33     }
34     public front() {
35         return this.items[0];
36     }
37     public isEmpty() {
38         return this.items.length == 0;
39     }
40     public clear() {
41         this.items = [];
42     }
43     public size() {
44         return this.items.length;
45     }
46     public print() {
47         console.log(this.items.toString());
48     }
49 }
50 // 使用方法
51 let priorityQueue = new PriorityQueue();
52 priorityQueue.enqueue('John', 2);
53 priorityQueue.enqueue('Jack', 1);
54 priorityQueue.enqueue('Camila', 1);
55 priorityQueue.print();
56 
57 // 下图中可以看到每条命令的结果
JavaScript数据结构与算法(三) 优先级队列的实现

 

JavaScript方式实现源码

 1 var QueueElement = (function () {
 2     function QueueElement(element, priority) {
 3         this.element = element;
 4         this.priority = priority;
 5     }
 6     return QueueElement;
 7 }());
 8 var PriorityQueue = (function () {
 9     function PriorityQueue() {
10         this.items = [];
11     }
12     PriorityQueue.prototype.enqueue = function (element, priority) {
13         var queueElement = new QueueElement(element, priority);
14         if (this.isEmpty()) {
15             this.items.push(queueElement);
16         }
17         else {
18             var added = false;
19             for (var i_1 = 0; i_1 < this.items.length; i_1++) {
20                 if (queueElement.priority < this.items[i_1].priority) {
21                     this.items.splice(i_1, 0, queueElement);
22                     added = true;
23                     break;
24                 }
25             }
26             if (!added) {
27                 this.items.push(queueElement);
28             }
29         }
30     };
31     PriorityQueue.prototype.dequeue = function () {
32         return this.items.shift();
33     };
34     PriorityQueue.prototype.front = function () {
35         return this.items[0];
36     };
37     PriorityQueue.prototype.isEmpty = function () {
38         return this.items.length == 0;
39     };
40     PriorityQueue.prototype.clear = function () {
41         this.items = [];
42     };
43     PriorityQueue.prototype.size = function () {
44         return this.items.length;
45     };
46     PriorityQueue.prototype.print = function () {
47         console.log(this.items.toString());
48     };
49     return PriorityQueue;
50 }());

 

代码角度解读:
    PriorityQueue对比Queue.PriorityQueue为每个队列元素包裹了一个对象,该对象用于优先级的标记,添加时会对队列中所有数据进行便利计算.其他使用接口一致
抽象:
    优先队列的应用场景非常多比如排队买票、地铁安检、飞机登机、任务计划、项目排期.等等都是队列,优先队列是对队列的扩展,比如排队买票只有一个窗口,出于人道主义,
老人与小朋友可以优先.这时优先队列便可以将他们安排到队列前面 总结: 当Queue能解决应用场景问题时不推荐使用PriorityQueue,以此来优化内存开支