Tree help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

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

Tree help

 
0
  #1
Jun 5th, 2009
So in a binary tree the insertion looks something like this:

  1. void btree::insert(int num)
  2. {
  3. if(root!=NULL)
  4. {
  5. insert(num, root);
  6. }
  7. else
  8. {
  9. root=new Node;
  10. root->data=num;
  11. root->left=NULL;
  12. root->right=NULL;
  13. }
  14. }
  15.  
  16. void btree::insert(int num, Node *leaf)
  17. {
  18. if(num< leaf->data)
  19. {
  20. if(leaf->left!=NULL)
  21. {
  22. insert(num, leaf->left);
  23. }
  24. else
  25. {
  26. leaf->left=new Node;
  27. leaf->left->data=num;
  28. leaf->left->left=NULL;
  29. leaf->left->right=NULL;
  30. }
  31. }
  32. else if(num>=leaf->data)
  33. {
  34. if(leaf->right!=NULL)
  35. insert(num, leaf->right);
  36. else
  37. {
  38. leaf->right=new Node;
  39. leaf->right->data=num;
  40. leaf->right->left=NULL;
  41. leaf->right->right=NULL;
  42. }
  43. }
  44. }

But how would you insert for an n-ary tree?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Tree help

 
2
  #2
Jun 5th, 2009
The same way, except instead of dealing with only two links, you have more than two (usually in an array).
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Tree help

 
0
  #3
Jun 5th, 2009
It would be better if you divide that nary tree nodes in some hierarchical levels so that it would be helpful in deciding their positions in future.
I Surf in "C"....
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: Tree help

 
0
  #4
Jun 5th, 2009
So If i had this how would I keep track of the root children?

  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. class Tree
  7. {
  8. public:
  9. Tree() { root = NULL; }
  10.  
  11. bool isEmpty() const { return root==NULL; }
  12. void insert(int);
  13. void remove(int);
  14.  
  15. private:
  16. struct node
  17. {
  18. node *parent;
  19. vector <node*> children;
  20. int item;
  21. };
  22.  
  23. node* root;
  24. };
  25.  
  26. void Tree::insert(int num)
  27. {
  28. if(isEmpty())
  29. {
  30. root=new node;
  31. root->item = num;
  32. }
  33. else
  34. {
  35. //?
  36. }
  37.  
  38. }
  39.  
  40. int main()
  41. {
  42. return 0;
  43. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Tree help

 
2
  #5
Jun 5th, 2009
Well, do the children need to be in any specific order? If so, you know what to do: keep the vector sorted to maintain that order. That's how you keep track of the nodes.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Tree help

 
0
  #6
Jun 5th, 2009
Or may be a priority array which stores the data in the node for referencing the node and its level or priority in the tree.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC