 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Peter L. Guest
|
Posted: Wed Jan 28, 2004 2:31 pm Post subject: pointer of pointer to a class |
|
|
Hi,
I really would appreciate any comment on my problem. Thank you.
I have a tree with nodes and now I want to swap two nodes
but I can't set the new destination correctly.
A method compares the values of root-father and root. If the
root value is bigger the movenode function is called with
"movenode(root)"
I think the problem occurs when I set the pointers:
root->right->father = root->father;
The pointers are not changed correctly.
A possible binary tree structure:
if you go right you're on the same level
if you go left you're on a deeper level
o -- o
¦
o -- o -- o
¦ ¦
o o
¦
o
Have a nice day.
Peter
class node
{
public:
int num;
int importance;
vector<string> values;
node *left;
node *right;
node *father;
node() ; // constructor
};
// move node to the left
// this method is called from checknodeimportance
int tree::movenode(node *& root)
{
if (root->father->father->left == root->father) // we are on the
right side of a node that is the last node of the level
{
cout << "root->values " << root->values[0] << endl;
cout << "root->values " << root->right->father->values[0] << endl;
cout << "root->values->father " << root->father->values[0] << endl;
if (root->right != NULL)
{
cout << "has right brother" << endl;
root->right->father = root->father; // set link from moved link to
his right node
root->father->right = root->right;
}
cout << "root->values " << root->values[0] << endl;
cout << "root->values-left " <<
root->right->father->left->values[0] << endl;
cout << "root->values->father " << root->father->values[0] << endl;
if (root->father->father != NULL)
{
root->father->father->setleft(root);
}
cout << "root->father->father->setleft(root) 73 " <<
root->father->father->left->values[0] << endl;
root->setright(root->father); // set link from root to moved link
root->father = root->father->father; // copy link to father from
moved link
root->right->father = root;
cout << "after move " << root->father->values[0] << root->values[0]
<< root->right->values[0] << endl;
}
else
{
cout << "not last node on the right" << endl;
if (root->right != NULL)
{
root->right->father = root->father; // set link from moved link to
his right node
root->father->setright(root->right);
}
root->father->father->setright(root);
root->setright(root->father); // set link from root to moved link
root->father = root->father->father; // copy link to father from
moved link
root->right->father = root;
cout << "after move " << root->father->values[0] << root->values[0]
<< root->right->values[0] << endl;
checknodeimportance(root);
}
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Jan 28, 2004 8:43 pm Post subject: Re: pointer of pointer to a class |
|
|
Peter L. wrote:
| Quote: | I really would appreciate any comment on my problem. Thank you.
class node
{
public:
int num;
int importance;
vector<string> values;
node *left;
node *right;
node *father;
node() ; // constructor
};
|
A class with only public data. Make that a struct, preferably nested in
class tree.
| Quote: | int tree::movenode(node *& root)
{
[...]
return 0;
}
|
Why is 'root' passed by reference? It is never touched here, afaict, except
possibly in the call to checknodeimportance().
What is the return-value? Why is it always zero? What is it used for?
Further, this method seems not to touch any members of the tree except
perhaps in the final call to checknodeimportance(). If you refactor this to
become a memberfunction of class node, you might be able to remove lots of
'root->', reducing clutter and making it easier to read.
| Quote: | if (root->father->father->left == root->father)
|
You are dereferencing several pointers, without first having assert()ed that
these are not null. This leads to soubtle and hard to find bugs.
| Quote: | root->father->father->setleft(root);
|
No member setleft() declared in class node.
| Quote: | cout << "root->father->father->setleft(root) 73 "
root->father->father->left->values[0] << endl;
|
This says one thing and does another. Weird at least.
| Quote: | root->setright(root->father); // set link from root to moved link
|
No member setright() in class node.
Peter, you really need to refactor your code. If not for yourself, then for
the people here to be able to understand it. One rule for posting examples
here, is that you first remove things that aren't necessary for the
example. checknodeimportance() might or might not be such a thing, the
input and output might be other things that could make a difference.
One last thing: it might be helpful to draw a diagram of the nodes before
and after the move for the general case. Then, draw one for the special
cases like the node to move being the only node or other things like that.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|