无序数组实现
package com.zwz.priority;
/**
* 无序数组的实现
* @param <E>
*/
public class PriortiyServiceImpl<E extends Priortiy> implements PriortiyService<E> {
E[] array = (E[]) new Priortiy[5];
int size = 0;
@Override
public Boolean offer(E e) {
if (isFull()) {
return false;
}
array[size] = e;
size++;
return true;
}
private int getMax() {
int m = 0;
for (int i = 1; i < array.length; i++) {
if (array[m].priortiy() < array[i].priortiy()) {
m = i;
}
}
return m;
}
@Override
public E poll() {
if (isEmpty()) {
return null;
}
int max = getMax();
E e = array[max];
remove(max);
return e;
}
private void remove(int index) {
if (index < size - 1) {
System.arraycopy(array,index+1,array,index,size-1-index);
}
size--;
}
@Override
public E peek() {
if (isEmpty()) {
return null;
}
int max = getMax();
return array[max];
}
@Override
public Boolean isFull() {
return size == array.length;
}
@Override
public Boolean isEmpty() {
return size == 0;
}
}
有序数组实现
package com.zwz.priority;
/**
* 有序数组的实现
*
* @param <E>
*/
public class PriortiyServiceImpl2<E extends Priortiy> implements PriortiyService<E> {
E[] array = (E[]) new Priortiy[5];
int size = 0;
@Override
public Boolean offer(E e) {
if (isFull()) {
return false;
}
if(size == 0){
array[size] = e;
}else{
insert(e);
}
size++;
return true;
}
private void insert(E e) {
for (int i = size-1; i >= 0; i--) {
if (e.priortiy() > array[i].priortiy()) {
System.arraycopy(array, i, array, i + 1, size - i);
array[i + 1] = e;
break;
}
}
}
@Override
public E poll() {
if (isEmpty()) {
return null;
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i].priortiy());
}
E e = array[size - 1];
size--;
return e;
}
@Override
public E peek() {
if (isEmpty()) {
return null;
}
return array[size - 1];
}
@Override
public Boolean isFull() {
return size == array.length;
}
@Override
public Boolean isEmpty() {
return size == 0;
}
}
完全二叉树实现: 堆
完全二叉树:父节点如果有子节点。添加子节点从左到右添加
大顶堆:父节点比子节点大
小顶堆:父节点比子节点小
使用完全二叉树实现优先级队列
堆实现:
package com.zwz.priority;
/**
* 无序数组的实现
*
* @param <E>
*/
public class PriortiyServiceImpl3<E extends Priortiy> implements PriortiyService<E> {
E[] array = (E[]) new Priortiy[10];
int size = 0;
@Override
public Boolean offer(E e) {
if (isFull()) {
return false;
}
int child = size++;
int parent = (child - 1) / 2;
while (child > 0 && e.priortiy() > array[parent].priortiy()) {
array[child] = array[parent];
child = parent;
parent = (child - 1) / 2;
}
array[child] = e;
return true;
}
@Override
public E poll() {
if (isEmpty()) {
return null;
}
swap(0, size - 1);
size--;
E e = array[size];
array[size] = null;
dowm(0);
return e;
}
//下沉代码
private void dowm(int parent) {
int left = (2 * parent) + 1;
int right = left + 1;
int max = parent;
if (left < size && array[left].priortiy() > array[max].priortiy()) {
max = left;
}
if (right < size && array[right].priortiy() > array[max].priortiy()) {
max = right;
}
if(max != parent){
swap(max,parent);
dowm(max);
}
}
private void swap(int i, int j) {
E e = array[i];
array[i] = array[j];
array[j] = e;
}
@Override
public E peek() {
if (isEmpty()) {
return null;
}
return array[0];
}
@Override
public Boolean isFull() {
return size == array.length;
}
@Override
public Boolean isEmpty() {
return size == 0;
}
}