/** * Adds a new element to this heap. * @param newElement the element to add */ public void Add(NodoAE newElement) { if (elementsDict.ContainsKey(new Vector2(newElement.punto.x, newElement.punto.y))) { elementsDict[new Vector2(newElement.punto.x, newElement.punto.y)] = newElement; } else { elementsDict.Add(new Vector2(newElement.punto.x, newElement.punto.y), newElement); } // Add a new leaf elements.Add(null); int index = elements.Count - 1; // Demote parents that are larger than the new element while (index > 1 && getParent(index).CompareTo(newElement) > 0) { elements[index] = getParent(index); index = getParentIndex(index); } // Store the new element into the vacant slot elements[index] = newElement; }
public NodoAE find(NodoAE nodo) { if (elementsDict.ContainsKey(new Vector2(nodo.punto.x, nodo.punto.y))) { return(elementsDict[new Vector2(nodo.punto.x, nodo.punto.y)]); } else { return(null); } }
/** * Turns the tree back into a heap, provided only the root * node violates the heap condition. */ private void fixHeap() { NodoAE root = elements[1]; int lastIndex = elements.Count - 1; // Promote children of removed root while they are larger than last int index = 1; bool more = true; while (more) { int childIndex = getLeftChildIndex(index); if (childIndex <= lastIndex) { // Get smaller child // Get left child first NodoAE child = getLeftChild(index); // Use right child instead if it is smaller if (getRightChildIndex(index) <= lastIndex && getRightChild(index).CompareTo(child) < 0) { childIndex = getRightChildIndex(index); child = getRightChild(index); } // Check if larger child is smaller than root if (child.CompareTo(root) < 0) { // Promote child elements[index] = child; index = childIndex; } else { // Root is smaller than both children more = false; } } else { // No children more = false; } } // Store root element in vacant slot elements[index] = root; }
/** * Removes the minimum element from this heap. * @return the minimum element */ public NodoAE remove() { NodoAE minimum = elements[1]; // Remove last element int lastIndex = elements.Count - 1; NodoAE last = elements[lastIndex]; elements.RemoveAt(lastIndex); if (lastIndex > 1) { elements[1] = last; fixHeap(); } return(minimum); }
public List <PuntoNavegable> caminoMinimo(int oi, int oj, int di, int dj) { NavMinHeap abiertos = new NavMinHeap(); NavMinHeap cerrados = new NavMinHeap(); NodoAE auxNodo; NodoAE auxNodoHermano; NodoAE nodoEncontradoAbiertos; NodoAE nodoEncontradoCerrados; PuntoNavegable origen; PuntoNavegable destino; List <PuntoNavegable> minPath = new List <PuntoNavegable>();; origen = puntosNavegables[oi][oj]; destino = puntosNavegables[di][dj]; auxNodo = new NodoAE(); auxNodo.punto = origen; auxNodo.costo = 0; abiertos.Add(auxNodo); minPath.Add(destino); while (abiertos.size() > 0) { auxNodo = abiertos.remove(); foreach (PuntoNavegable hermano in auxNodo.punto.hermanos) { if (hermano != null && !hermano.objeto.GetComponent <NavPunto>().blocked) { if ((hermano.x == destino.x) && (hermano.y == destino.y)) { minPath.Add(auxNodo.punto); while (auxNodo.padre != null) { auxNodo = auxNodo.padre; minPath.Add(auxNodo.punto); } return(minPath); } auxNodoHermano = new NodoAE(); auxNodoHermano.padre = auxNodo; auxNodoHermano.punto = hermano; auxNodoHermano.distanciaAlOrigen = auxNodo.costo + 1; auxNodoHermano.distanciaAlFinal = distHeuristica(auxNodoHermano.punto, destino); auxNodoHermano.costo = auxNodoHermano.distanciaAlOrigen + auxNodoHermano.distanciaAlFinal; nodoEncontradoAbiertos = abiertos.find(auxNodoHermano); nodoEncontradoCerrados = cerrados.find(auxNodoHermano); if ((nodoEncontradoAbiertos != null) && (nodoEncontradoAbiertos.costo < auxNodoHermano.costo)) { continue; } if ((nodoEncontradoCerrados != null) && (nodoEncontradoCerrados.costo < auxNodoHermano.costo)) { continue; } abiertos.Add(auxNodoHermano); } } cerrados.Add(auxNodo); } return(null); }