![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 345
Rep Power: 4
![]() |
pointer help
alright so i have 2 pointers pointing to the left and right child nodes of a heap. I have only done the heapNode class so far. When i compile it gives me some errors. I know the errors are the same problem just once for the right and once for the left. I am pretty sure they are in the get methods for the left and right child. I will post the code and the errors.
#include <iostream>
using namespace std;
class heapNode
{
private:
int value;
heapNode *left;
heapNode *right;
public:
//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)
{
right = newLeft;
}
}; // end of heapNode classQuote:
|
|
|
|
|
|
|
#2 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
'left' and 'right' are of type heapNode*. So, if you want to return their value you will have to dereference them. If you want to return the actuall pointer you need to change the return type of the accessor members to: heapNode*
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 345
Rep Power: 4
![]() |
i think i want to return the actual value so how do i dereference them?
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
Before dereferencing any pointer, check that it is not NULL.
You most probably don't want to return heapNode by value, because this uses copying. Cache's suggestion of returning the pointer is probably closer to what you need. If you want to access the int value data member contained in the pointed-to-object, you can use the -> operator. right->value; (*right).value; |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 345
Rep Power: 4
![]() |
yea because i have one that returns the value. I want to return the right and left pointers so i can do something to them or something?i dont know all i know is i am trying to add things to a min heap using nodes and need to have a method to access the left and right children of the node. In the constructor i set them to null becuase there is nothing attached to them. I do not even know how to attach a new node to them. I will post the program and if someone has some idea on how i can add a new node and make it work that would be appreciated. Right now i have a counter that goes to the place where the new node is supposed to go but can i just say to make a new node? I somehow need to make the new node connect to the right or left child of the previous node or something? The counter is wierd. It just counts how many have been entered and then take the binary number of that and for 0 moves left and 1 moves right. However, right now i just have it saying move left or move right instead of actually moving. How do i make it where it actually moves and enters the node in the right spot?
Here is the method that finds a spot: // find the spot to put new value
void insertSpot(int counter)
{
int arrayValue;
bool isOne = false;
int ar[32];
for(int i = 0; i < 32; i++)
{
arrayValue = option % 2;
option = option / 2;
ar[i] = arrayValue;
}
for(int i = 0; i < 32; i++)
cout << ar[i] << " ";
cout << endl;
for(int i = 31; i >= 0; i--)
{
if(isOne)
{
if(ar[i] == 0)
{
//cout << "\t\t\t\tmove left\n";
}
if(ar[i] == 1)
{
//cout << "\t\t\t\tmove right \n";
}
}
if(ar[i] == 1)
isOne = true;
}
}here is the add method so far: // insert a new value into the heap
insert(int value, heapNode *newNode)
{
if(counter == 0)
{
newNode = new heapNode(value);
root = newNode;
}
else
{
insertSpot(counter)
newNode = new heapNode(value);
}
}Here is my heapNode class that has the get methods that do not work and everything else class heapNode
{
private:
int value;
heapNode *left;
heapNode *right;
public:
//constructor
heapNode(int newValue)
{
value = newValue;
left = NULL;
right = NULL;
}
//get value
int getValue()
{
return value;
}
//get right
heapNode getRight()
{
return right->right;
}
//get left
heapNode getLeft()
{
return left->left;
}
//set value
void setValue(int newValue)
{
value = newValue;
}
// set right
void setRight(heapNode *newRight)
{
right = newRight;
}
// set left
void setLeft(heapNode *newLeft)
{
right = newLeft;
}
}; // end of heapNode classLast edited by cwl157; Mar 28th, 2006 at 10:54 PM. |
|
|
|
|
|
#6 | |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
Quote:
//get right
heapNode getRight()
{
return right;
}
//get left
heapNode getLeft()
{
return left;
} |
|
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 345
Rep Power: 4
![]() |
i am still confused on how to fix the problem. So to get the thing that right points to i need to set the return type of those functions to heapNode*? And that will correctly return the node right or the node left points to?
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 345
Rep Power: 4
![]() |
ok so now i think i have the whole input done but when i try to enter a number it prints the array on which way to go ok and then i get a segmentation fault. Here is the code:
#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)
{
right = 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, int newValue)
{
heapNode *newNode;
newNode = new heapNode(newValue);
if(newNode->left == NULL)
{
realInsert(ar, pos-1, newValue);
if(newNode->left->getValue() < newNode->getValue())
newNode->setValue(newNode->left->getValue());
}
else if(newNode->right == NULL)
{
realInsert(ar, pos-1, newValue);
if(newNode->right->getValue() < newNode->getValue())
newNode->setValue(newNode->right->getValue());
}
}
// find the spot to put new value
void insertSpot(int option)
{
int arrayValue;
bool isOne = false;
int ar[32];
int i = 31;
for(int i = 0; i < 32; i++)
{
arrayValue = option % 2;
option = option / 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, option);
}
// get root
heapNode* getRoot()
{
return root;
}
/*
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=0;
cout << "Please enter a number: ";
cin >> option;
myHeap->insertSpot(option);
return 0;
} |
|
|
|
|
|
#9 | |
|
Professional Programmer
|
for(int i = 31; i >= 0; i--) four posts up, first [code] tags, you know this is going to loop until i == -1, right?
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#10 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 882
Rep Power: 4
![]() |
If you trace through your realInsert code, you can see where it will crash (comments in red):
void realInsert(int ar, int pos, int newValue)
{
heapNode *newNode;
newNode = new heapNode(newValue); // Calls constructor, constructor sets value to newValue and left and right to NULL
if(newNode->left == NULL) // Always true due to above constructor
{
realInsert(ar, pos-1, newValue); // Recursively calls this function, because we will always get to this same spot in the recusive call as well, it causes an infinite recursion, which will eventually crash.
// None of the code below here will ever get executed
if(newNode->left->getValue() < newNode->getValue())
newNode->setValue(newNode->left->getValue());
}
else if(newNode->right == NULL)
{
realInsert(ar, pos-1, newValue);
if(newNode->right->getValue() < newNode->getValue())
newNode->setValue(newNode->right->getValue());
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|