Insert Tree

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Insert Tree

 
0
  #1
Jun 9th, 2009
Would someone be so kind as to help me with this insert. The function insert takes two numbers, num and num2. num is the number to insert in the tree and num2 is the number of children to the parent node where num is inserted. I am getting errors for root->children = NULL. Here is what I have.

this is my tree base:
  1. struct node
  2. {
  3. node *parent;
  4. vector <node*> children;
  5. int item;
  6. };
  7. node* root;
And my insert function:
  1. void Tree::insert(int num, int num2)
  2. {
  3. if(isEmpty())
  4. {
  5. root = new node;
  6. root->children = NULL;
  7. root->item = num;
  8. }
  9. else if(root->children == NULL)
  10. {
  11. root->children = num;
  12.  
  13. for(int i = 0;i<num2;i++)
  14. {
  15. children = root;
  16. root->children = NULL;
  17. }
  18. }
  19. else
  20. root->children = num;
  21. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Insert Tree

 
0
  #2
Jun 9th, 2009
Childen is of type vector. I think that you should use push_back(value); to handle that.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Insert Tree

 
0
  #3
Jun 9th, 2009
Ah yeah that was just a blind man that didnt notice that. But now, how would I check for an open node?

  1. void Tree::insert(int num, int num2)
  2. {
  3. if(isEmpty())
  4. {
  5. root = new node;
  6. root->children.push_back(NULL);
  7. root->item = num;
  8. }
  9. else if(root->children == NULL)//here
  10. {
  11. root->children.push_back(num);
  12.  
  13. for(int i = 0;i<num2;i++)
  14. {
  15. children = root;
  16. root->children.push_back(NULL);
  17. }
  18. }
  19. else
  20. root->children.push_back(num);
  21. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Insert Tree

 
0
  #4
Jun 9th, 2009
Still doesn't make sense to me. Why would you push an integer onto a vector of node* ? Seems to me you should push a pointer to a node onto that vector.
Last edited by VernonDozier; Jun 9th, 2009 at 2:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Insert Tree

 
0
  #5
Jun 9th, 2009
Something like this?

  1. void Tree::insert(int num, int num2)
  2. {
  3. node* insert = new node;
  4. insert->item = num;
  5.  
  6. if(isEmpty())
  7. {
  8. root = insert;
  9. for(int i = 0;i<num2;i++)
  10. {
  11. root->children.push_back(0);
  12. }
  13. }
  14. else
  15. {
  16.  
  17. }
  18. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Insert Tree

 
0
  #6
Jun 9th, 2009
Originally Posted by JackDurden View Post
Something like this?

  1. void Tree::insert(int num, int num2)
  2. {
  3. node* insert = new node;
  4. insert->item = num;
  5.  
  6. if(isEmpty())
  7. {
  8. root = insert;
  9. for(int i = 0;i<num2;i++)
  10. {
  11. root->children.push_back(0);
  12. }
  13. }
  14. else
  15. {
  16.  
  17. }
  18. }

Well, 0 means NULL and NULL is a pointer, so there's no error there. But I prefer to use the word "NULL" so it's obvious that it's being used as a pointer, not as the integer 0.

This is confusing.

void Tree::insert(int num, int num2)
{
	node* insert = new node;
	insert->item = num;

I'd pick a different name for your new node. It's the same as your function's name. How about newNode ?

void Tree::insert(int num, int num2)
{
	node* newNode = new node;
	newNode->item = num;

The whole function looks fairly odd to me. I think I'd have to see the larger Tree class and the main function to get a better feel for how you're trying to use it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Insert Tree

 
0
  #7
Jun 9th, 2009
Here is the whole thing...

  1. class Tree
  2. {
  3. public:
  4. Tree() { root = NULL; }
  5.  
  6. bool isEmpty() const { return root==NULL; }
  7. void insert(int, int);
  8.  
  9. private:
  10. struct node
  11. {
  12. node *parent;
  13. vector <node*> children;
  14. int item;
  15. };
  16. node* root;
  17. };
  18.  
  19. void Tree::insert(int num, int num2)
  20. {
  21. node* current = new node;
  22. current->item = num;
  23.  
  24. if(isEmpty())
  25. {
  26. root = current;
  27. for(int i = 0;i<num2;i++)
  28. {
  29. root->children.push_back(NULL);
  30. }
  31. }
  32. else
  33. {
  34.  
  35. }
  36. }
  37.  
  38. int main()
  39. {
  40. Tree myTree;
  41.  
  42. myTree.insert(1, 3);
  43.  
  44. return 0;
  45. }

i want to do something like this for my insert function:

-open node?
if no
make new node and insert num to node
create children to that node based on num2
if yes
find open node, insert num to that node
create children to that node

It looks confusing because im not sure how to do it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Insert Tree

 
0
  #8
Jun 9th, 2009
Well vectors can be handled in majorly in 2 ways. One is to use an index as we do to an array and the other to use iterators.

for example assuming that the for loop in your insert function creates Null pointers which are to be filled here is what you can do.

  1. for(int x=0;x<root->children.size();x++)
  2. {
  3. if(root->children[x]==NULL)
  4. {
  5. //Assign a value
  6. }
  7. }
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Insert Tree

 
1
  #9
Jun 9th, 2009
My strong advice is to draw it out step-by step. Your tree is going to have children of children of children. Sky Diploma is on to something with his traversal idea, but you need to go further. In particular, dealing with children of children, make sure you are cautious about hard-coding root in the code.

for(int x=0;x<root->children.size();x++)
{
 if(root->children[x]==NULL)
 {
  //Assign a value
 }
}

You need to be able to check children of children and children of children of children and children of children of children of children, etc., which this cannot do. So at some point you may want to have a variable called parentNode (intentionally not named parent to avoid confusion with one of your node members), so you may want to have something like what Sky Diploma has, but replace root with parentNode . parentNode may START OFF being root , but it should potentially also be able to NOT point to root and instead point to some node that is lower in the tree.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 327 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC