public heapnode parent; //parent of this node public heapnode(int gcost, int hcost, int x,int y, heapnode parent) { this.gcost = gcost; this.hcost = hcost; calculatefcost(); this.x = x; this.y = y; this.parent = parent; }
public void addtoheap(int gcost, int hcost, int x, int y, heapnode parent) { //append to the end of the array heaparray[nodecounter] = new heapnode(gcost, hcost, x, y, parent); nodecounter++; //compare to other values in the array until it is sorted int index = nodecounter-1; //index of the current node while(index>1 && heaparray[index].fcost < heaparray[index/2].fcost) { swapwithparent(index); index = index/2; } }
public void addtoheap(heapnode node) { //append to the end of the array heaparray[nodecounter] = node; nodecounter++; //compare to other values in the array until it is sorted int index = nodecounter-1; //index of the current node if(nodecounter>2) //if there's at least 2 elements in the array { while(index>1 && heaparray[index].fcost < heaparray[index/2].fcost) { swapwithparent(index); index = index/2; } } }
public binaryheap(heapnode firstnode) { heaparray[1] = firstnode; nodecounter++; }
//trace the path public List<int[]> tracepath(heapnode node) { List<int[]> pathlist = new List<int[]>(); if(node.parent != null) { pathlist.AddRange(tracepath(node.parent)); } pathlist.Add(new int[]{node.x, node.y}); //add the node to the list return pathlist; }
//swaps a node with it's parent public void swapwithparent(int index) { //swap positions in the array tempnode = heaparray[index]; //save current node to temporary location heaparray[index] = heaparray[index/2]; //replace current node with parent node heaparray[index/2] = tempnode; //replace parent node with saved current node }
public void replacenode(heapnode newnode) { for(int i=1; i<nodecounter; i++) { if(heaparray[i].x == newnode.x && heaparray[i].y == newnode.y) { heaparray[i] = newnode; return ; } } }
//trace the path void tracepath(heapnode node) { pathlist = new List<int[]>(); if(node.parent != null) { tracepath(node.parent); } //Debug.Log(node.x + "," + node.y + " on path"); pathlist.Add(new int[]{node.x, node.y}); //add the node to the list return; }