Skip to main content
GameDev.net gamedev.net
🔒 Locked

Binary Heap Insertion

Started by adam23 May 12, 2010 at 9:47 PM 9 replies 4.2k views
Original Post
adam23
adam23
So I've been banging my head on a quick way to implement insert for a binary heap data structure built with a binary tree. The problem comes in finding the best possible insertion point to then peculate the node up into its position. Heres the algorithm i'm running now for insert which is way to slow.

	//!  Insert a new item into the heap
	void Insert( T data )
	{
		Leaf<T>* pNewNode = GetNewLeaf( data );
		Leaf< T >* pBestParent = NULL;


		if( m_pRoot == NULL )
		{
			m_pRoot = pNewNode;
		}
		else
		{
			int nDepth = 0;
			pBestParent = GetPositionInTree( m_pRoot, nDepth );

			//See which side to insert the node on
			if( pBestParent->m_pLeft == NULL )
				pBestParent->m_pLeft = pNewNode;
			else
				pBestParent->m_pRight = pNewNode;

			pNewNode->m_pParent = pBestParent;

			printf( "Depth = %d \n", nDepth );
		}

		//Now time to heapify
		if( pBestParent != NULL )
			HeapifyUp( pBestParent );
	}


And here is what the recursive call looks like

	//! Helper returns the best parent for this new node
	Leaf< T >* GetPositionInTree( Leaf< T >* pLeaf, int& nDepth )
	{
		//Simple check if there is a left and right keep recurring
		if( pLeaf->m_pLeft != NULL && pLeaf->m_pRight != NULL )
		{
			nDepth++;
			int nLeftDepth = nDepth;
			int nRightDepth = nDepth;
			Leaf< T >* pBestLeft = GetPositionInTree( pLeaf->m_pLeft, nLeftDepth );
			Leaf< T >* pBestRight = GetPositionInTree( pLeaf->m_pRight, nRightDepth );

			if( nLeftDepth > nRightDepth )
			{
				nDepth = nRightDepth;
				return pBestRight;
			}
			else
			{
				nDepth = nLeftDepth;
				return pBestLeft;
			}
		}

		//No children this is the best parent
		return pLeaf;
	}


The time to move the node into place is really fast, but I'd like to move my insertion speed to more of a rate of O(1) instead of 0(n) like it is now. So my question is does anyone know of a way I can improve this? Or is there a better way to perform insertion when using a binary tree for holding the data instead of an array? Thanks Adam
Adamhttp://www.allgamedevelopment.com
Sneftel
Sneftel
A binary heap is filled from the left. Maintain a pointer to the "last" element, that is, the one with the highest depth such that no equal-depth cousin is to the right of it. To move right from there, just walk up parent links until you're at a left child, then move to the parent, then the right child, then to left children until you hit a leaf. Amortized cost of traversing in this manner is O(1).
iMalc
iMalc
Here's the trick to filling the tree one level at a time:

Take the count of how many items are in the tree prior to the insertion and add one
Examine this number in binary, finding the leftmost bit that is a one
Skip the leading one bit
Repeat the following until you have processed the least significant bit:
- if this bit is a zero, go down the tree to the left
- if this bit is a one, go down the tree to the right
- move along to the next bit
You should now be at the point where you want to insert the new node

So, imagine a tree with 10 items in it already:
           1        /     \.      /         \.     2           3   /   \       /   \.  4     5     6     7 / \   /8   9 10
(the dots are so that it doesn't screw up the diagram)
Also note that the numbers shown are not in heap-order, they're just showing the order that items were inserted.

You want to insert the 11th item.
11 in binary is 1011
Remove the leading 1, giving 011
That means go left(0) then right(1), then right(1).
Stop and insert the 11th item there.

Out of curiosity, why are you implementing a heap using a tree data structure rather than an array?
adam23
adam23
Quote:
Original post by Sneftel
A binary heap is filled from the left. Maintain a pointer to the "last" element, that is, the one with the highest depth such that no equal-depth cousin is to the right of it. To move right from there, just walk up parent links until you're at a left child, then move to the parent, then the right child, then to left children until you hit a leaf. Amortized cost of traversing in this manner is O(1).



Very cool thanks, this is very similar to an idea that I had late last night, but in my solution it still required an expensive calculation to jump from the far right of the tree to the far left of the tree. I think my solution is figuring out when the tree is filled and doing a quick linear traversal along the far left side, instead of the more complicated check every node approach I was trying to make work.
Adamhttp://www.allgamedevelopment.com
adam23
adam23
Quote:
Original post by iMalc
Here's the trick to filling the tree one level at a time:

Take the count of how many items are in the tree prior to the insertion and add one
Examine this number in binary, finding the leftmost bit that is a one
Skip the leading one bit
Repeat the following until you have processed the least significant bit:
- if this bit is a zero, go down the tree to the left
- if this bit is a one, go down the tree to the right
- move along to the next bit
You should now be at the point where you want to insert the new node

So, imagine a tree with 10 items in it already:
           1        /     \.      /         \.     2           3   /   \       /   \.  4     5     6     7 / \   /8   9 10
(the dots are so that it doesn't screw up the diagram)
Also note that the numbers shown are not in heap-order, they're just showing the order that items were inserted.

You want to insert the 11th item.
11 in binary is 1011
Remove the leading 1, giving 011
That means go left(0) then right(1), then right(1).
Stop and insert the 11th item there.

Out of curiosity, why are you implementing a heap using a tree data structure rather than an array?



I'm really excited to give this one a try, I've never heard of this insertion approach before Thanks!

To be honest I've seen a lot of array based implementations of Binary heaps, but not many binary tree based implementations, so I wanted to see the comparisons when using a "binary tree heap" vs an "array heap" when used in a priority queue. Also one of the limitations I always disliked about the array based implementations is needing to grow the array, and using a binary tree fixes that.

I guess call it curiosity more than anything.


Thanks again for all the suggestions!
Adam


Adamhttp://www.allgamedevelopment.com
adam23
adam23
I updated my Heap with the new insertion and it makes a huge difference, theres probably some more optimizations I can do, but here's what I have so far.

	//! Returns first empty location	Leaf< T >* GetFirstEmptySlot( )	{		//Determine the path to take in the tree		//First find the most significant bit and remove it		unsigned int nMask = 1 << 31;		while( ( m_nCount & nMask ) == 0 )		{			nMask = nMask >> 1;		}		//Now remove the bit		nMask >>= 1;		//Traverse down the tree until a parent is found		Leaf< T >* pNewParent = m_pRoot;		while( nMask > 1 )		{			//0 Go Left, else go right			if( ( nMask & m_nCount ) == 0 )			{				pNewParent = pNewParent->m_pLeft;			}			else			{				pNewParent = pNewParent->m_pRight;			}			nMask >>= 1;		}		return pNewParent;	}
Adamhttp://www.allgamedevelopment.com
Zahlman
Zahlman
The array-based implementations are binary trees. It's just that the child pointers are implicit. The implementation takes advantage of the fact that the tree will always be "full" to make this work, setting a mathematical relationship between the index of the parent and the indices of its children.
adam23
adam23
Quote:
Original post by Zahlman
The array-based implementations are binary trees. It's just that the child pointers are implicit. The implementation takes advantage of the fact that the tree will always be "full" to make this work, setting a mathematical relationship between the index of the parent and the indices of its children.



That is an excellent point, and I do appreciate the array based implementations. I did some research on how different implementations were done before starting the tree version, and I have to say the array based method does seem very useful. I do understand the principal of the array based versions being binary tree's where indexing is used to find their children, I guess I worded my response incorrectly.

With that said, say i'm building a priority queue to handle pathfinding in a game. I've found that I need more flexibility than a fixed size array that needs to keep doing full copies when it runs low on memory. I'm sure in the end i'll go back to the array based implementation as performing a sort on it is much easier than the tree version, but I really wanted to give this one a try.
Adamhttp://www.allgamedevelopment.com
iMalc
iMalc
Quote:
Original post by adam23
With that said, say i'm building a priority queue to handle pathfinding in a game. I've found that I need more flexibility than a fixed size array that needs to keep doing full copies when it runs low on memory. I'm sure in the end i'll go back to the array based implementation as performing a sort on it is much easier than the tree version, but I really wanted to give this one a try.
It's an interesting thing to learn and practice with, but the array based version is far superrior in terms of both speed and memory usage.
If you store the heap inside a vector rather than an array, then you aren't limited to a fixed maximum size. As you may or may not know, vectors have an amortised constant time push_back method.

In fact C++ already provides heavily optimised functions for building and using a heap out of an array or vector. What more could we ask for! [smile]
Sneftel
Sneftel
Quote:
Original post by adam23
I've found that I need more flexibility than a fixed size array that needs to keep doing full copies when it runs low on memory.

Not until you understand amortized analysis.
Sneftel
Sneftel
Quote:
Original post by iMalc
In fact C++ already provides heavily optimised functions for building and using a heap out of an array or vector. What more could we ask for! [smile]
decrease-key.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.