private void BHInsertCell(CellSearch ns) { //We use index 0 as the root! if (sortedOpenList.Count == 0) { sortedOpenList.Add(ns); openList [ns.ID].sortedIndex = 0; return; } sortedOpenList.Add(ns); bool canMoveFurther = true; int index = sortedOpenList.Count - 1; openList [ns.ID].sortedIndex = index; while (canMoveFurther) { int parent = (int)Math.Floor(((double)(index - 1) / 2)); if (index == 0) //We are the root { canMoveFurther = false; openList [sortedOpenList [index].ID].sortedIndex = 0; } else { if (sortedOpenList [index].F < sortedOpenList [parent].F) { CellSearch s = sortedOpenList [parent]; sortedOpenList [parent] = sortedOpenList [index]; sortedOpenList [index] = s; //Save sortedlist index's for faster look up openList [sortedOpenList [index].ID].sortedIndex = index; openList [sortedOpenList [parent].ID].sortedIndex = parent; //Reset index to parent ID index = parent; } else { canMoveFurther = false; } } } }
private void BHSortCell(int id, int F) { bool canMoveFurther = true; int index = openList [id].sortedIndex; sortedOpenList [index].F = F; while (canMoveFurther) { int parent = (int)Math.Floor((double)(index - 1) / 2); if (index == 0) //We are the root { canMoveFurther = false; openList [sortedOpenList [index].ID].sortedIndex = 0; } else { if (sortedOpenList [index].F < sortedOpenList [parent].F) { CellSearch s = sortedOpenList [parent]; sortedOpenList [parent] = sortedOpenList [index]; sortedOpenList [index] = s; //Save sortedlist index's for faster look up openList [sortedOpenList [index].ID].sortedIndex = index; openList [sortedOpenList [parent].ID].sortedIndex = parent; //Reset index to parent ID index = parent; } else { canMoveFurther = false; } } } }
private int BHGetLowest() { if (sortedOpenList.Count == 1) //Remember 0 is our root { int ID = sortedOpenList [0].ID; sortedOpenList.RemoveAt(0); return(ID); } else if (sortedOpenList.Count > 1) { //save lowest not, take our leaf as root, and remove it! Then switch through children to find right place. int ID = sortedOpenList [0].ID; sortedOpenList [0] = sortedOpenList [sortedOpenList.Count - 1]; sortedOpenList.RemoveAt(sortedOpenList.Count - 1); openList [sortedOpenList [0].ID].sortedIndex = 0; int index = 0; bool canMoveFurther = true; //Sort the list before returning the ID while (canMoveFurther) { int child1 = (index * 2) + 1; int child2 = (index * 2) + 2; int switchIndex = -1; if (child1 < sortedOpenList.Count) { switchIndex = child1; } else { break; } if (child2 < sortedOpenList.Count) { if (sortedOpenList [child2].F < sortedOpenList [child1].F) { switchIndex = child2; } } if (sortedOpenList [index].F > sortedOpenList [switchIndex].F) { CellSearch ns = sortedOpenList [index]; sortedOpenList [index] = sortedOpenList [switchIndex]; sortedOpenList [switchIndex] = ns; //Save sortedlist index's for faster look up openList [sortedOpenList [index].ID].sortedIndex = index; openList [sortedOpenList [switchIndex].ID].sortedIndex = switchIndex; //Switch around idnex index = switchIndex; } else { break; } } return(ID); } else { return(-1); } }