package prioriyQueue;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
public class PriorityQueue1<E>
{
public static final int initcalcapcity = 11;
private int size = 0;
private Object[] queue;
private final Comparator<? super E> comparator;
PriorityQueue1()
{
this(initcalcapcity,null);
}
PriorityQueue1(int initcalcapcity)
{
this(initcalcapcity,null);
}
PriorityQueue1(int initcalcapcity,Comparator<? super E> comparator)
{
if(initcalcapcity<1)
{
throw new IllegalArgumentException();
}
queue = new Object[initcalcapcity];
this.comparator = comparator;
}
public Boolean add(E e)
{
if(e == null)
{
throw new NullPointerException();
}
if(size>=queue.length)
{
grow(size+1);
}
if(size == 0)
{
queue[0] = e;
size++;
}else
{
shiftUp(size++,e);
}
return true;
}
private void grow(int minCapacity)
{
int oldCapacity = queue.length;
int newCapacity = ((oldCapacity < 64)?
((oldCapacity + 1) * 2):
((oldCapacity / 2) * 3));
if (newCapacity < 0) // overflow
newCapacity = Integer.MAX_VALUE;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
queue = Arrays.copyOf(queue, newCapacity);
}
private void shiftUp(int i, E e)
{
if(comparator!=null)
{
shiftUpComparator(i,e);
}
else
{
shiftUpComparable(i,e);
}
}
private void shiftUpComparator(int i, E e)
{
if(e == null)
{
throw new NullPointerException();
}
while(i > 0)
{
int parent = (i-1)>>>1;
if((comparator.compare(e, (E)queue[parent])) >= 0)
{
break;
}
queue[i] = queue[parent];
i = parent;
}
queue[i] = e;
}
private void shiftUpComparable(int i, E e)
{
if(e == null)
{
throw new NullPointerException();
}
Comparable<? super E> key = (Comparable<? super E>)e ;
while(i > 0)
{
int parent = (i-1)>>>1;
if(key.compareTo((E)queue[parent])>=0)
{
break;
}
queue[i] = queue[parent];
i = parent;
}
queue[i] = e;
}
public E peek()
{
if(size == 0)
{
return null;
}
else
{
return (E)queue[0];
}
}
public E poll()
{
if(size == 0)
{
return null;
}
E e = (E)queue[0];
queue[0] = queue[size-1];
queue[--size] = null;
shiftDown(0,(E)queue[0]);
return e;
}
private void shiftDown(int i, E e)
{
if(comparator!=null)
{
shiftDownComparator(i,e);
}
else
{
shiftDownComparable(i,e);
}
}
private void shiftDownComparable(int i, E e)
{
Comparable<? super E > key = (Comparable<? super E >) e;
int half = size>>>1;
while(i<half)
{
int child = i>>1 + 1;
int childRight = child + 1;
if(childRight<size&&((Comparable<? super E>) queue[child]).compareTo((E)queue[childRight])>0)
{
child = childRight;
}
if(key.compareTo((E)queue[child])<= 0)
{
break;
}
queue[i] = queue[child];
i = child;
}
queue[i] = e;
}
private void shiftDownComparator(int i, E e)
{
int half = size>>>1;
while(i<half)
{
int child = i>>1 + 1;
int childRight = child + 1;
if(childRight<size&&comparator.compare((E)queue[child], (E)queue[childRight])>0)
{
child = childRight;
}
if(comparator.compare((E)queue[child],e)> 0)
{
break;
}
queue[i] = queue[child];
i = child;
}
queue[i] = e;
}
public Boolean remove(E e)
{
int index = indexof(e);
if(index <0)
{
return false;
}
removeAt(index);
return true;
}
private void removeAt(int index)
{
if(index<0||index>=size)
{
throw new IndexOutOfBoundsException();
}
int i = --size;
if(index == size)
{
queue[size] = null;
}
else
{
E removed = (E)queue[i];
queue[i] = null;
shiftDown(index, removed);
if(queue[index]==removed)
{
shiftUp(index, removed);
}
}
}
private int indexof(E e)
{
for (int i = 0;i<size;i++)
{
if(((E)queue[i]).equals(e))
{
return i;
}
}
return -1;
}
public Iterator iterator()
{
return new PriorityQueueIterator();
}
private class PriorityQueueIterator implements Iterator<E>
{
int cursor = 0;
@Override
public boolean hasNext()
{
return cursor<size;
}
@Override
public E next()
{
return (E)queue[cursor++];
}
@Override
public void remove()
{
}
}
}
优先队列
最新推荐文章于 2024-06-22 20:10:28 发布