943,673 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 545
  • C++ RSS
Jun 5th, 2009
0

Tree help

Expand Post »
So in a binary tree the insertion looks something like this:

C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Jun 5th, 2009
2

Re: Tree help

The same way, except instead of dealing with only two links, you have more than two (usually in an array).
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 5th, 2009
0

Re: Tree help

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jun 5th, 2009
0

Re: Tree help

So If i had this how would I keep track of the root children?

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Jun 5th, 2009
2

Re: Tree help

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 5th, 2009
0

Re: Tree help

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: URGENT! It is for my assignment. I am so stuck. Please check my c++ work.
Next Thread in C++ Forum Timeline: Flie Handling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC