EL CODIGO EN JAVA PARA INSERTAR UN ELEMENTO
public void insertItem(Object k, Object e) throws InvalidKeyException {
if(!comp.isComparable(k))
throw new InvalidKeyException("Invalid Key");
Position z = T.add(new Item(k, e));
Position u;
while(!T.isRoot(z)) { // bubbling-up
u = T.parent(z);
if(comp.isLessThanOrEqualTo(key(u),key(z)))
break;
T.swapElements(u, z);
z = u;
}
}
EL CODIGO EN JAVA PARA ELIMINAR UN ELEMENTO:
public Object removeMin() throws PriorityQueueEmptyException {
if(isEmpty())
throw new PriorityQueueEmptyException("Priority Queue Empty!");
Object min = element(T.root());
if(size() == 1)
T.remove();
else {
T.replaceElement(T.root(), T.remove());
Position r = T.root();
while(T.isInternal(T.leftChild(r))) {
Position s;
if(T.isExternal(T.rightChild(r)) || comp.isLessThanOrEqualTo(key(T.leftChild(r)),key(T.rightChild(r))))
s = T.leftChild(r);
else
s = T.rightChild(r);
if(comp.isLessThan(key(s), key(r))) {
T.swapElements(r, s);
r = s;
}
else
break;
}
}
}
No hay comentarios:
Publicar un comentario