-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/ximo/datastructuresinaction/queue/PriorityQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ximo.datastructuresinaction.queue; | ||
|
||
import com.ximo.datastructuresinaction.heap.MaxHeap; | ||
|
||
/** | ||
* @author xikl | ||
* @date 2019/1/5 | ||
*/ | ||
public class PriorityQueue<E extends Comparable<E>> implements Queue<E> { | ||
|
||
private MaxHeap<E> maxHeap; | ||
|
||
public PriorityQueue() { | ||
maxHeap = new MaxHeap<>(); | ||
} | ||
|
||
|
||
@Override | ||
public int getSize() { | ||
return maxHeap.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return maxHeap.isEmpty(); | ||
} | ||
|
||
@Override | ||
public void enqueue(E element) { | ||
maxHeap.add(element); | ||
} | ||
|
||
@Override | ||
public E dequeue() { | ||
return maxHeap.extractMax(); | ||
} | ||
|
||
@Override | ||
public E getFront() { | ||
return maxHeap.findMax(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters