![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#41 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
i don't know. A min heap just fills them up left to right. I was told if you take the number of times something has been entered like keep a counter. Like if you enter the numbers 12, 14, 19 the counter would be 3 because 3 numbers have been entered. Then if you take the binary representation of that counter number it tells you where the next one should go. You put the binary numbers into an array and then look at each 1 or 0 ignoring the most significant 1. And then if the next number in the array is 1 move right if its 0 move left. Then when the numbers done wherever it is thats where the new value goes. Then you compare that value to the parents and swap accordingly. Is there a different easier way to keep track of the insert spot? I really need help because this has to be done by tuesday and i do not even have the insert done.
|
|
|
|
|
|
#42 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
Ah, I see. Where you are going wrong is the difference between this part of the expanation
Quote:
You need to use the binary representation of counter, not option I'll post some code changes in a bit. |
|
|
|
|
|
|
#43 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
DaWei, grumpy, jim mcnamara, and you The Dark are my programming examples (way more experienced and advanced than me). And so am interested in how you would handle such a thing. Quote:
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
||
|
|
|
|
|
#44 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
like you take the counter and mod it by 2 and then devide the counter by 2 and mod the new counter and then keep going until the counter is 0 and then you take each of those mod 2 numbers which each will be a 0 or 1 and put them into an array. Then you ignore the most significant 1 and all 0 before it and take the rest of the 1 and 0 s and they tell you which way to go to insert the next value
|
|
|
|
|
|
#45 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
@nnxion: I am not ignoring your examples, yours is the way I would do it (actually I would probably use arrays), but the OP seems to have to do it this way. I did a bit of googling to try to understand what he was getting at.
Basically for this type of min-heap, you always insert at the bottom in the next available spot and then percolate back up, that way the heap is always balanced. For an array implementation, the next available spot is just the entry just after the end of the current array. For this implementation, looking at the number of items counter as binary left/right indicators is a "quick" way of traversing down the tree to get to the next available location. @cwl157: here are some changes to get you going. 1. In setLeft void setLeft(heapNode *newLeft)
{
right = newLeft;
}2. In print() void print()
{
cout << root->left
<< root->right;
if(root->left != NULL && root->right != NULL)
{
print();
}
}My code loos like: void print()
{
printNode(root, 0);
}
void printNode(heapNode *node, int level)
{
if (node != NULL)
{
cout << "Level: " << level << " Val: " << node->getValue() << "\n";
printNode(node->getLeft(), level + 1);
printNode(node->getRight(), level + 1);
}
}3. In main while (option != 0) {
cout << "Please enter a number: ";
cin >> option;
myHeap->insertSpot(option);
}4. At the moment you are creating a new node in each level of your recursion, this is bad (and is one of the things that confused me in the first place). Create the node in "insertSpot" and pass it in to your function void insertSpot(int option)
{
heapNode *newNode = new heapNode(option);5. Change your binary left/right array calculation to use the counter rather than "option" (option is a funny name for that parameter btw). int temp = counter;
for(i = 0; i < 32; i++)
{
arrayValue = temp % 2;
temp = temp / 2;
ar[i] = arrayValue;
}6. You don't increment the counter variable anywhere, but you need to now that you are using it. It looks to me as if you need to add it before you start looking at the left/right binary, so that the second item added goes to the left of the root (2 = 0010 in binary). void insertSpot(int option)
{
heapNode *newNode = new heapNode(option);
// Increment counter first as the algorithm needs it
counter++;7. I wasn't sure how the whole left/right binary thing would work when there was no entries in the heap, so I made a special case for that void insertSpot(int option)
{
heapNode *newNode = new heapNode(option);
// Increment counter first as the algorithm needs it
counter++;
if (root == NULL)
{
root = newNode;
}
else
{8. The realInsert function needs to have access to all of the array, not just the first entry (as I said before). It also needs to know which parent node in the heap it is working on (currently yours works on a newly created node, which has nothing to do with the heap). It also needs the new node created in insertSpot (from #4). void realInsert(int ar[], int pos, heapNode *parent, heapNode *newNode) Give it a go and see how you progress. What environment are you working in? If you have a debugger, you should single step through the program to see what it does. If you don't have a debugger, you should put trace writes through your code. For example, call the heap print function at various stages to see how it goes. |
|
|
|
|
|
#46 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
im using emacs editor on a unix server. I do not really know completely how to use it. Thanks for all the suggestions ill make the changes and see what happens
|
|
|
|
|
|
#47 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
alright these are the errors i got after entering the new stuff i will try and work them out but i thought i would post them anyway:
Quote:
Last edited by cwl157; Apr 1st, 2006 at 8:00 PM. |
|
|
|
|
|
|
#48 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
alright now i got it down to this and im stuck
Quote:
|
|
|
|
|
|
|
#49 | |||
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
It is pretty hard to suggest how to fix errors when I can't see your code, but here goes:
Quote:
Quote:
Quote:
As to what to pass as parent and newNode. When you first call the realInsert function the parent would be root, from then on when you recurse down, you would use parent->getLeft() or parent->getRight(). Pass in the newNode that was created at the top of insertSpot as the newNode parameter. |
|||
|
|
|
|
|
#50 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
sorry i will post the whole code with the changes i made:
#include <iostream>
using namespace std;
class heapNode
{
private:
int value;
//heapNode *left;
//heapNode *right;
public:
heapNode *left;
heapNode *right;
//constructor
heapNode(int newValue)
{
value = newValue;
left = NULL;
right = NULL;
}
//get value
int getValue()
{
return value;
}
//get right
heapNode* getRight()
{
return right;
}
//get left
heapNode* getLeft()
{
return left;
}
//set value
void setValue(int newValue)
{
value = newValue;
}
// set right
void setRight(heapNode *newRight)
{
right = newRight;
}
// set left
void setLeft(heapNode *newLeft)
{
left = newLeft;
}
}; // end of heapNode class
class heap
{
private:
heapNode *root;
int counter;
public:
//constructor
heap()
{
counter = 0;
root = NULL;
}
void realInsert(int ar[], int pos, heapNode *parent, heapNode *newNode)
{
newNode = new heapNode(newValue);
if(newNode->left == NULL)
{
realInsert(ar, pos-1, parent, newNode);
if(newNode->left->getValue() < newNode->getValue())
{
newNode->setValue(newNode->left->getValue());
counter++;
}
}
else if(newNode->right == NULL)
{
realInsert(ar, pos-1, parent, newNode);
if(newNode->right->getValue() < newNode->getValue())
{
newNode->setValue(newNode->right->getValue());
counter++;
}
}
}
// find the spot to put new value
void insertSpot(int option)
{
heapNode *newNode = new heapNode(option);
int arrayValue;
bool isOne = false;
int ar[32];
int i = 31;
int temp = counter;
counter++;
if(root == NULL)
{
root = newNode;
}
else
{
for(int i = 0; i < 32; i++)
{
arrayValue = temp % 2;
option = temp / 2;
ar[i] = arrayValue;
}
for(int i = 0; i < 32; i++)
cout << ar[i] << " ";
cout << endl;
while(i >= 0)
{
if(ar[i] == 1)
isOne = true;
if(isOne = true)
break;
i--;
}
realInsert(*ar, i, parent, newNode);
}
}
//print
void print()
{
printNode(root, 0);
}
void printNode(heapNode *node, int level)
{
if(node != NULL)
{
cout << "Level: " << " Val: " << node->getValue() << "\n";
printNode(node->getLeft(), level + 1);
printNode(node->getRight(), level + 1);
}
}
// get root
heapNode* getRoot()
{
return root;
}
//insert a new value into the heap
// void insert(int value, heapNode *newNode)
//{
// if(counter == 0)
// {
// newNode = new heapNode(value);
// root = newNode;
// }
// else
// {
// insertSpot(counter);
// newNode = new heapNode(value);
// }
//while(newNode.getValue() != root.getValue())
//{
//if(newNode > parent)
//break;
//elseif(newNode < parent)
//counter-1 node.setValue(newNode.getValue());
// }
// counter++;
// }
/*
int delete(rootValue)
{
heapNode *tempNode = new heapNode(value, *left, *right)
go down the heap to the last one
make it root
last nodeValue = null
while(not at the bottom level of tree)
{
if(newRoot < *left)
break;
elseif(newRoot > *left)
swap with left;
}
return tempNode;
}
*/
}; // end of heap class
int main()
{
heap *myHeap;
myHeap = new heap();
int option=9;
while (option != 0) {
cout << "Please enter a number: ";
cin >> option;
myHeap->insertSpot(option);
}
myHeap->print();
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|