Min-Heap and Hash Table help

Chris Pons cmpons at gmail.com
Mon Apr 2 15:47:56 PDT 2012


I'm trying to work with and implement and priority queue( 
min-heap ) and a hash table. This is the first time I've worked 
with these data structures so I looked up some information about 
them to gain an understanding.

 From what I read, a min-heap is a binary tree that is sorted by a 
priority. In my case I have a struct called Node for an A* 
algorithm that I wanted to place in a min-heap sorted by an 
integer, their f score. Initially the min-heap will only have one 
item in it with others added later.

 From looking at the Library reference I have gathered this:

struct Node
{
	bool walkable;		//Whether this node is blocked or open
	vect2 position;		//The tile's position on the map in pixels
	int	xIndex, yIndex; //The index values of the tile in the array
	Node*[4] connections; //An array of pointers to nodes this 
current node connects to
	Node* parent;
	int gScore;
	int hScore;
	int fScore;
}

Class AStar
{
Node[] a;
Node start;

void FindPath(Node[] PathGraph )
{
a ~= start;
//Heapify as min-heap by f Score?
openList = heapify(a, "a.fScore > b.fScore" );

//...After some work If I want to add a new Node

Node new;
//Will the list stay sorted??
openList.insert( new );
}
}

How would I create a min-heap sorted by fScore?
Will the list stay sorted after I add a new node?

As far as hash tables goes, it seems like I need to use an 
associative array, is that right?

What exactly would the key/hash be? How would I implement this if 
the hash table is supposed to contain the struct Node?



More information about the Digitalmars-d-learn mailing list