Skip to content

Commit

Permalink
采用大顶堆实现一个优先队列
Browse files Browse the repository at this point in the history
  • Loading branch information
Xikl committed Jan 5, 2019
1 parent bc97b20 commit 9fdf48f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

import java.time.Duration;
import java.time.Instant;
import java.util.PriorityQueue;
import java.util.Random;

import static org.junit.Assert.*;

/**
* @author Ximo
* @date 2018/10/23 22:19
Expand Down Expand Up @@ -65,4 +64,10 @@ private static long timeConsuming(Queue<Integer> queue, int count) {
Duration timeConsuming = Duration.between(start, end);
return timeConsuming.toMillis() / 1000;
}


@Test
public void testPriorityQueue() {
java.util.Queue<Integer> integerQueue = new PriorityQueue<>();
}
}

0 comments on commit 9fdf48f

Please sign in to comment.