   Binary Heap Insertion
  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 heapvoid 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 onif( pBestParent->m_pLeft == NULL )pBestParent->m_pLeft = pNewNode;elsepBestParent->m_pRight = pNewNode;pNewNode->m_pParent = pBestParent;printf( "Depth = %d \n", nDepth );}//Now time to heapifyif( pBestParent != NULL )HeapifyUp( pBestParent );}And here is what the recursive call looks like//! Helper returns the best parent for this new nodeLeaf< T >* GetPositionInTree( Leaf< T >* pLeaf, int& nDepth ){//Simple check if there is a left and right keep recurringif( 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 parentreturn 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   
  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).   ;  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 oneExamine this number in binary, finding the leftmost bit that is a oneSkip the leading one bitRepeat 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 bitYou should now be at the point where you want to insert the new nodeSo, imagine a tree with 10 items in it already: 1   /\. /    \.2 3   /   \  /   \.  4567 / \   /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 1011Remove the leading 1, giving 011That 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?   ;Quote:Original post by SneftelA 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.;Quote:Original post by iMalcHere'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 oneExamine this number in binary, finding the leftmost bit that is a oneSkip the leading one bitRepeat 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 bitYou should now be at the point where you want to insert the new nodeSo, imagine a tree with 10 items in it already: 1   /\. /    \.2 3   /   \  /   \.  4567 / \   /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 1011Remove the leading 1, giving 011That 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;  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 locationLeaf< T >* GetFirstEmptySlot( ){//Determine the path to take in the tree//First find the most significant bit and remove itunsigned int nMask = 1 << 31;while( ( m_nCount & nMask ) == 0 ){nMask = nMask >> 1;}//Now remove the bitnMask >>= 1;//Traverse down the tree until a parent is foundLeaf< T >* pNewParent = m_pRoot;while( nMask > 1 ){//0 Go Left, else go rightif( ( nMask & m_nCount ) == 0 ){pNewParent = pNewParent->m_pLeft;}else{pNewParent = pNewParent->m_pRight;}nMask >>= 1;}return pNewParent;};  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.   ;Quote:Original post by ZahlmanThe 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.;Quote:Original post by adam23With 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]   ;Quote:Original post by adam23I'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.   