I'm currently working with a heap structure which is suppose to be used to sort numbers in an array. I have done something like this in the code when I want to sort the structure when I pop(dequeue) a element from the heap.
template<typename T>
inline void dHeap<T>::reHeapDown(int root, int bottom)
{
int minChild;
int rightChild;
int leftChild;
int temp;
// Get index of root's right/left child
leftChild = root * 2 + 1;
rightChild = root * 2 + 2;
//Then we are not done with re-heaping
if (leftChild <= bottom)
{
if (leftChild == bottom)
{
minChild = leftChild;
}
else
{
if (arr[leftChild] <= arr[rightChild])
minChild = leftChild;
else
minChild = rightChild;
}
if (arr[root] > arr[minChild])
{
// Swap these two elements
temp = arr[root];
arr[root] = arr[minChild];
arr[minChild] = temp;
// Make recursive call till reheaping completed
reHeapDown(minChild, bottom);
}
}
}
My thought here is that the lowest value in the heap always will be in the root and that's the value that I will be poped(dequeued) in my pop function.
But I'm having some problem that it wont sort the heap correctly.
is there something wrong with my logic in this function and if so, where is it?
Building a heap only enforces the property:
in case of min heap every parent is lesser than it children
in case of max heap every parent is greater than its children.
in case of min-max heap even depth levels (0,2,4..) are lesserr and odd levels (1,3,5...) are greater than their respective children.
However the heap will not necessarily be sorted. It will be balanced, because it is filled in order, level by level, from left to right.
Heapsort will sort an array by using heap functions. The final array will also work as a balanced and sorted heap.
Related
I'm having trouble writing the code to build minimum height BST from a sorted std::list.
For the node class:
class cBTNode
{
private:
cBTNode* m_LeftChild;
cBTNode* m_RightChild;
float m_Data;
}
For the BST class:
class cBTNodeTree
{
private:
cBTNode* m_Root;
public:
void LoadBalancedMain(std::list<float>& ls);
void LoadBalanced(std::list<float>& ls, cBTNode* root);
}
Implementation: (basically my method is to find the middle element of the list ls, put that into the root, put all the elements smaller than the middle element into ls_left, and all the elements bigger than it into ls_right. Then recursively build up the left and right subtree by recursively calling the same function on ls_left and ls_right)
void cBTNodeTree::LoadBalancedMain(std::list<float>& ls)
{
LoadBalanced(ls, m_Root); // m_Root is the root of the tree
}
void cBTNodeTree::LoadBalanced(std::list<float>& ls, cBTNode* root)
{
// Stopping Condition I:
if (ls.size() <= 0)
{
root = nullptr;
return;
}
// Stopping Condition II:
if (ls.size() == 1)
{
root = new cBTNode(ls.front());
return;
}
// When we have at least 2 elements in the list
// Step 1: Locate the middle element
if (ls.size() % 2 == 0)
{
// Only consider the case of even numbers for the moment
int middle = ls.size() / 2;
std::list<float> ls_left;
std::list<float> ls_right;
int index = 0;
// Obtain ls_left consisting elements smaller than the middle one
while (index < middle)
{
ls_left.push_back(ls.front());
ls.pop_front();
index += 1;
}
// Now we reach the middle element
root = new cBTNode(ls.front());
ls.pop_front();
// The rest is actually ls_right
while (ls.size() > 0)
{
ls_right.push_back(ls.front());
ls.pop_front();
}
// Now we have the root and two lists
cBTNode* left = root->GetLeftChild();
cBTNode* right = root->GetRightChild();
if (ls_left.size() > 0)
{
LoadBalanced(ls_left, left);
root->SetLeftChild(left);
}
else
{
left = nullptr;
}
if (ls_right.size() > 0)
{
LoadBalanced(ls_right, right);
root->SetRightChild(left);
}
else
{
right = nullptr;
}
}
}
My Question: Somehow I found that actually none of the elements has been inserted into the tree. For example, if I check the value of m_Root, the root of the tree, I got an error because it's still nullprt. I'm not sure where did I go wrong? I hope it's some stupid pointer mistake because I haven't slept well. (I'm pretty sure the 'new cBTNode(ls.front())' line works)
BTW although I have written a dozen functions for the BST, I'm still struggling with BST recursion. I noticed that in all the textbooks that I read, for the linked list version of BST, the insertion ALWAYS need a helper function that return a pointer to a node. I begin to feel that I don't actually understand the things going on behind the recursion...
1:
void cBTNodeTree::LoadBalanced(std::list<float>& ls, cBTNode* root)
Here cBTNode* root is passed by value.
Instead, you should pass by reference & or cBTNode** (pointer to a pointer).
Passing by reference would be simple, you won't need to change anything except the function signature.
void cBTNodeTree::LoadBalanced(std::list<float>& ls, cBTNode*& root)
Notice & before root in above statement.
2:
if (ls_right.size() > 0)
{
LoadBalanced(ls_right, right);
root->SetRightChild(left);
}
You are setting right child to left root which is not what you desire.
3:
cBTNode* left = root->GetLeftChild();
cBTNode* right = root->GetRightChild();
These are unnecessary.
4:
if (ls.size() % 2 == 0)
No need for two separate cases.
You can achieve this by just appropriately setting middle:
int middle = (ls.size()-1) / 2;
You pass the pointer to the root by value. Pass it by reference instead by changing the signature of LoadBalanced() appropriately.
I have made a binary tree based on Alex Allain's example found here. It throws a stack overflow exception after adding about 5000-6000 elements to it. Any idea of how to prevent a stack overflow? The cause is that Insert() calls itself recursivly.
Update 3/6/2013
Here is how I refactored the code to avoid stack overflow:
void Insert(Key_T key, Value_T val, QuickMapNode<Key_T, Value_T> *leaf)
{
while (true)
if(key < leaf->key)
{
if(leaf->left) leaf = leaf->left;
else
{
leaf->left = new QuickMapNode<Key_T, Value_T>;
leaf->left->key = key;
leaf->left->val = val;
leaf->left->parent = leaf;
leaf->left->left = NULL; // Sets the left child of the child node to null
leaf->left->right = NULL; // Sets the right child of the child node to null
break;
}
}
else if (key >= leaf->key)
{
if(leaf->right) leaf = leaf->right;
else
{
leaf->right = new QuickMapNode<Key_T, Value_T>;
leaf->right->key = key;
leaf->right->val = val;
leaf->right->parent = leaf;
leaf->right->left = NULL; // Sets the left child of the child node to null
leaf->right->right = NULL; // Sets the right child of the child node to null
break;
}
}
}
Like Öö Tiib said, you should change insert to be not recursive. Every recursive function can be turned into a non-recursive one by storing the data that would go on the stack in some other data structure. This way you can use the heap for those data and don't have the overhead of function calls on the stack (return address etc.) You can often use a vector or list that you use like a stack: take (and pop) the back() of the vector to get the current argument, and in places your current code would recursively call itself, you push_back() what you would have passed to the recursive function call.
Here's the insert() method from your link as an iterative version:
void btree::insert(int key, node *leaf)
{
std::list<node*> leafs;
leafs.push_back(leaf);
while (leafs.size() > 0)
{
leaf = leafs.back();
leafs.pop_back();
if(key < leaf->key_value)
{
if(leaf->left!=NULL)
leafs.push_back(leaf->left);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
leafs.push_back(leaf->right);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
}
I ran the code and it seems to work. It's much slower than the recursive version though, not sure why that is ... Both versions work fine with 10000 and more elements, so there's perhaps something else wrong in your implementation ...
Actually, when traversing a binary tree like we do here, there's no need to store any previous information, as we don't do any backtracking. Once the location for the new element is found, we're finished. So we can get rid of the list/vector altogether:
void btree::insert(int key, node *leaf)
{
while (leaf != NULL)
{
if(key < leaf->key_value)
{
if(leaf->left!=NULL)
leaf = leaf->left;
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
return;
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
leaf = leaf->right;
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
return;
}
}
}
}
Make algorithm of insert that is not recursive. You do need only to search place where to insert so you do not need stack of calls for that.
Taking guesses due to lack of provided details: assume worst case, that after 6000 inserts the stack depth is all 6000 recursive calls. Assume a reasonable stack frame size of maybe 20 bytes - then stack size is 6000 * 20 = 120,000 bytes. If the stack frame is actually 160 bytes (8x larger) then the stak size is 6000 * 160, slightly less than 1MB. I wonder ... is there a limit on your number of elements? What is the allocated stack size?
The comments above tell you how to actually solve the problem (balanced tree). I might add, just about any recursive algorithm can be converted to an iterative algorithm - it isn't as elegant and it will take effort to get it right, but then you won't fill up the stack. But if there really is (not just you think there is) a limit on the number of input elements, then it seems to me you could determine the size of the stack frame for insert and make the stack size large enough to fit #elements * stack frame size, the worst case, plus a little more for whatever extra is on the stack.
As mentioned above the most likely cause is the stack overflow due to too many recursive insert calls
The following are the different options
Use a self balancing tree http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree
Use a non recursive tree decent algorithmn.Lookk at this example Non-recursive add function in a binary tree using c++
void ReheapDown( int* heap, int top, int swpIndx, int numElements ) {
int leftChild = 2 * top + 1;
int rightChild = 2 * top + 2;
int minChild;
if (leftChild < numElements) {
// find subscript of smallest child
if (rightChild >= swpIndx || heap[leftChild] < heap[rightChild])
minChild = leftChild;
else
minChild = rightChild;
// if data at top is greater than smallest
// child then swap and continue
if (heap[top] > heap[minChild]) {
swap( heap[top], heap[minChild] );
ReheapDown( heap, minChild, swpIndx, numElements );
}
}
This is for a simple heap. ReheapDown is used partly for removing items in the heap. What does swpIndx do though? (I need to know to do a homework assignment, where I'm supposed to write the function that removes a certain key in the heap.)
To remove a key from a heap, we might want to swap it with the last key in the heap before deleting it, otherwise there would be a gaping hole in the heap.
However, swapping the last node with the node we want to remove can upset the ordering of the heap, which is where that ReheapDown method that you provided comes in.
I believe the swpIndex parameter is the index into which we have placed the element that we wish to remove. So that part of the code basically says:
if (there exists a left child) {
if (there is no right child || left child < right child)
minChild <- left child
else
minChild <- right child
}
I think that parameter is unnecessary though, since it seems that it's only purpose is to check for the existence of the left and right child; this can also be accomplished by comparing the leftChild and rightChild indices to numElements.
Hope this helps :)
I'm reading a book about AI and currently learning about pathfinding(currently doing the Dijkstra algorithm)
In the sample code he's using something he calls an IndexedPriorityQueue implemented as a two-way heap. I couldn't find any information on what a two-way heap is on google.
This search algorithm is implemented using an indexed priority queue.
A priority queue, or PQ for short, is a queue that keeps its elements
sorted in order of priority (no surprises there then). This type of
data structure can be utilized to store the destination nodes of the
edges on the search frontier, in order of increasing distance (cost)
from the source node. This method guarantees that the node at the
front of the PQ will be the node not already on the SPT that is
closest to the source node.
This is how it gets implemented:
//----------------------- IndexedPriorityQLow ---------------------------
//
// Priority queue based on an index into a set of keys. The queue is
// maintained as a 2-way heap.
//
// The priority in this implementation is the lowest valued key
//------------------------------------------------------------------------
template<class KeyType>
class IndexedPriorityQLow
{
private:
std::vector<KeyType>& m_vecKeys;
std::vector<int> m_Heap;
std::vector<int> m_invHeap;
int m_iSize,
m_iMaxSize;
void Swap(int a, int b)
{
int temp = m_Heap[a]; m_Heap[a] = m_Heap[b]; m_Heap[b] = temp;
//change the handles too
m_invHeap[m_Heap[a]] = a; m_invHeap[m_Heap[b]] = b;
}
void ReorderUpwards(int nd)
{
//move up the heap swapping the elements until the heap is ordered
while ( (nd>1) && (m_vecKeys[m_Heap[nd/2]] > m_vecKeys[m_Heap[nd]]) )
{
Swap(nd/2, nd);
nd /= 2;
}
}
void ReorderDownwards(int nd, int HeapSize)
{
//move down the heap from node nd swapping the elements until
//the heap is reordered
while (2*nd <= HeapSize)
{
int child = 2 * nd;
//set child to smaller of nd's two children
if ((child < HeapSize) && (m_vecKeys[m_Heap[child]] > m_vecKeys[m_Heap[child+1]]))
{
++child;
}
//if this nd is larger than its child, swap
if (m_vecKeys[m_Heap[nd]] > m_vecKeys[m_Heap[child]])
{
Swap(child, nd);
//move the current node down the tree
nd = child;
}
else
{
break;
}
}
}
public:
//you must pass the constructor a reference to the std::vector the PQ
//will be indexing into and the maximum size of the queue.
IndexedPriorityQLow(std::vector<KeyType>& keys,
int MaxSize):m_vecKeys(keys),
m_iMaxSize(MaxSize),
m_iSize(0)
{
m_Heap.assign(MaxSize+1, 0);
m_invHeap.assign(MaxSize+1, 0);
}
bool empty()const{return (m_iSize==0);}
//to insert an item into the queue it gets added to the end of the heap
//and then the heap is reordered from the bottom up.
void insert(const int idx)
{
assert (m_iSize+1 <= m_iMaxSize);
++m_iSize;
m_Heap[m_iSize] = idx;
m_invHeap[idx] = m_iSize;
ReorderUpwards(m_iSize);
}
//to get the min item the first element is exchanged with the lowest
//in the heap and then the heap is reordered from the top down.
int Pop()
{
Swap(1, m_iSize);
ReorderDownwards(1, m_iSize-1);
return m_Heap[m_iSize--];
}
//if the value of one of the client key's changes then call this with
//the key's index to adjust the queue accordingly
void ChangePriority(const int idx)
{
ReorderUpwards(m_invHeap[idx]);
}
};
Can anyone give me more information on what a 2-way heap is?
"Two-way heap" simply refers to the standard heap data structure. This code shows a very common way of implementing it, namely by flattening the tree structure of the heap into an array, in such a way that the index of a node's parent is always half of the index of the node (rounded down).
It is implemented AS a two way heap because he omits the 0 index in the heap to make parent- child calculation easier but the key values vektor begins at 0 index so inverted heap is storing indexes that keys to heap witch stores indexes that keys to keyVector to get appropriate value from keyVector you will need to do something like this keyVector[heap[invHeap[itemIndex] ] ] the rest of the code is just standard binary heap implementation.
I am trying to get the height of a BST using a stack. I was told that I should use preorder and measure find the largest size of the stack. However, this does not seem to work. Any ideas of what I am doing wrong.
int PBT::maxDepth() {
if (!root) {
return -1;
}
int depth=0;
stack<TreeNode *>s;
TreeNode * nodePtr=root;
for (; ; ) {
while (nodePtr) {
s.push(nodePtr);
if (s.size() > depth)
depth = s.size();
nodePtr=nodePtr->left;
}if (s.empty()) {
break;
}
nodePtr=s.top();
s.pop();
nodePtr=nodePtr->right;
}
return depth;
}
The stack size is incorrect value of depth for some nodes. Eg. if the current node is a right child of other node, the stack doesn't contain this other node (our parent). For the rightest node in the tree, the stack will have no items.
You'll have to calculate the depth correctly. In your case, you may go up more levels in one pop, so subtracting one won't work, but if you save your current depth to the stack (and restore it while popping), it will work.
To do that, you should change your stack definition to eg.
stack<pair<TreeNode*, unsigned> > stack;
and add a variable current_depth.
For each "nodePtr=nodeptr->left/right", you increment current_depth. Push with
s.push(make_pair(nodeptr, current_depth));
and before you pop, restore current_depth with
current_depth = s.top().second;
(The node pointer is obviously in .first)