File tree 4 files changed +94
-1
lines changed
4 files changed +94
-1
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
1
+ package com .ds_algo .m_priorityQueue ;
2
+
3
+ import java .util .Comparator ;
4
+
5
+ public class Person implements Comparable <Person > {
6
+ int age ;
7
+ String name ;
8
+ int brokenBone ;
9
+
10
+ public Person (int age , String name , int brokenBone ) {
11
+ this .age = age ;
12
+ this .name = name ;
13
+ this .brokenBone = brokenBone ;
14
+ }
15
+
16
+ @ Override
17
+ public int compareTo (Person o ) {
18
+ return this .brokenBone - o .brokenBone ;
19
+ }
20
+
21
+ @ Override
22
+ public String toString () {
23
+ return "Person{" +
24
+ "age=" + age +
25
+ ", name='" + name + '\'' +
26
+ ", brokenBone=" + brokenBone +
27
+ '}' ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change 1
1
package com .ds_algo .m_priorityQueue ;
2
2
3
- public class PriorityQueue {
3
+ import com .ds_algo .k_heap .BinaryHeap ;
4
+
5
+ import java .util .Comparator ;
6
+
7
+ /**
8
+ * 优先级队列
9
+ * 普通队列 :FIFO原则,先进先出
10
+ * 优先级队列 :按照优先级的高低进行 出队列, 比如将高优先级的元素最为队头
11
+ *
12
+ * 实现的数据结构 : 二叉堆
13
+ */
14
+
15
+ public class PriorityQueue <E > {
16
+ private BinaryHeap <E > heap ;
17
+
18
+ public PriorityQueue (Comparator <E > comparator ){
19
+ this .heap = new BinaryHeap <E >(comparator );
20
+ }
21
+ public PriorityQueue () {
22
+ this (null );
23
+ }
24
+
25
+ public int size (){
26
+ return heap .size ();
27
+ }
28
+
29
+ public boolean isEmpty (){
30
+ return heap .isEmpty ();
31
+ }
32
+ public void clear (){
33
+ heap .clear ();
34
+ }
35
+
36
+ public void enQueue (E element ){
37
+ heap .add (element );
38
+ }
39
+
40
+ public E deQueue (){
41
+ return heap .remove ();
42
+ }
43
+
44
+ public E front (){
45
+ return heap .get ();
46
+ }
47
+
4
48
}
Original file line number Diff line number Diff line change
1
+ package com .ds_algo .m_priorityQueue ;
2
+
3
+
4
+ public class TestPriorityQueueMain {
5
+
6
+ public static void main (String [] args ) {
7
+
8
+ PriorityQueue <Person > queue = new PriorityQueue <>();
9
+ queue .enQueue (new Person (10 ,"Jack" , 1 ));
10
+ queue .enQueue (new Person (10 ,"Luce" , 5 ));
11
+ queue .enQueue (new Person (10 ,"Rose" , 2 ));
12
+ queue .enQueue (new Person (10 ,"Kobe" , 10 ));
13
+ queue .enQueue (new Person (10 ,"Tomas" , 2 ));
14
+ queue .enQueue (new Person (10 ,"James" , 11 ));
15
+
16
+ while (!queue .isEmpty ()){
17
+ System .out .println (queue .deQueue ());
18
+ }
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments