473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Implementing Tree

2
Hi all,

I am new to C++ especially Visual C++.
I am facing a heavy task here, so I would like to ask for some opinions.
If I want to build a tree, where each node can have unlimited number of children.
Then I can add a new node, delete a node or a subset of the tree.
And then I need to traverse the tree quickly using DFS.
Is there any way of doing this? Should I use linked list?
Please help. Thanks.

Regards,
Agus
Apr 6 '07 #1
4 5569
Ganon11
3,652 Expert 2GB
You should use a regular set of TreeNodes, where each Node has a pointer to each of its children. Since you can have an unlimited number of children for each node, you can't use two separate variables called left and right - you will have to use some dynamic storage class like vector or linked list.
Apr 6 '07 #2
JosAH
11,448 Expert 8TB
You should use a regular set of TreeNodes, where each Node has a pointer to each of its children. Since you can have an unlimited number of children for each node, you can't use two separate variables called left and right - you will have to use some dynamic storage class like vector or linked list.
Alternatively you can use a node with its data in it, a pointer to its leftmost
child and a pointer to the node's sibling on the right. You could add a pointer
to its parent (quite handy to have sometimes). The node itself would be
a bit more lightweight while it still allows you to traverse the entire tree quite
efficiently.

kind regards,

Jos
Apr 6 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
If you are actually using C++, then why write tree code???

Use the std::map or std::multimap templates for key/value situations.

Use the std::set ot std::multiset for sets of values only.

The C++ Standard Library is designed to avoid having to code the common data structures over and over and over...
Apr 6 '07 #4
ghozan
2
Expand|Select|Wrap|Line Numbers
  1. class CTTreeNode //transaction tree node
  2. {
  3.  public:
  4.  
  5.   CTTreeNode();
  6.   CTTreeNode(int s, CTTreeNode *p);
  7.   ~CTTreeNode();
  8.  
  9.   int id;
  10.   int count;
  11.  
  12.   set<CTTreeNode> *children;    
  13.   CTTreeNode *parent;
  14.  
  15.   bool operator< (const CTTreeNode  &tn) const {return id < tn.id;}  
  16. };
  17.  
  18. class CTTree  //transaction tree
  19. {
  20. public:
  21.     CTTree();
  22.     virtual ~CTTree();
  23.     void processTransaction(Transaction *t);
  24.  
  25.     set<CTTreeNode> *root;    
  26. };
  27.  
  28. CTTreeNode::CTTreeNode()
  29. {
  30.   count = 0;
  31.   parent = 0;
  32.   id = 0;
  33.   children = 0;
  34. }
  35.  
  36. CTTreeNode::~CTTreeNode()
  37. {
  38.  
  39. }
  40.  
  41. CTTreeNode::CTTreeNode(int s, CTTreeNode *p)
  42. {
  43.     id = s;
  44.     parent = p;
  45.  
  46.     count = 0; 
  47.     children = 0;
  48. }
  49.  
  50. CTTree::CTTree()
  51. {
  52.     root = new set<CTTreeNode>;
  53.     CTTreeNode rootnode(0, 0);
  54.     rootnode.children = new set<CTTreeNode>();
  55.     root->insert(rootnode);
  56. }
  57.  
  58. CTTree::~CTTree()
  59. {
  60.     set<CTTreeNode>::iterator it;
  61.     for(it = root->begin(); it != root->end(); it++) {
  62.         if(it->children != 0)
  63.             delete it->children;
  64.     }
  65.     delete root;
  66. }
  67.  
  68. void CTTree::processTransaction(Transaction *t)
  69. {
  70.     set<CTTreeNode>::iterator it;    
  71.     set<CTTreeNode>* nodes = root->begin()->children;
  72.     CTTreeNode *current = 0;
  73.  
  74.     for(int depth=0; depth < t->length; depth++) { 
  75.             it = nodes->insert(CTTreeNode(t->t[depth], current)).first;//because insert check first
  76.       it->count++;
  77.       current = &(*it);
  78.       if(it->children==0) 
  79.           it->children = new set<CTTreeNode>;  
  80.       nodes = it->children;
  81.     }  
  82. }
  83.  
  84. void CTTree::print(FILE* out)
  85. {
  86.     set<CTTreeNode>::iterator it = root->begin();
  87.     printRecur(it, out);
  88. }
  89.  
  90. void CTTree::printRecur(set<CTTreeNode>::iterator it, FILE* out)
  91. {
  92.     fprintf(out, "%d, %d\n", it->id, it->count);
  93.     //cout << it->id << ", " << it->count << endl;
  94.  
  95.     set<CTTreeNode> *children = it->children;
  96.     for(set<CTTreeNode>::iterator itC=children->begin(); itC != children->end(); itC++)
  97.             printRecur(itC, out);
  98. }
  99.  
I have tried to implement the tree using std::set like the above definitions.
But when I tried to delete an element of the set, It gave error (the program
hang. This is the procedure which I used to traverse the tree and if a spesific
condition met the element will be removed.

Expand|Select|Wrap|Line Numbers
  1. void CTMmine::DeleteNodeRecur(set<CTTreeNode>::iterator it, int mSup, set<CElement> *relist)
  2. {
  3.     bool inRelist;
  4.     bool child = HasChild(it);
  5.     int tmpId = it->id;
  6.  
  7.     if (!child)
  8.     {
  9.         inRelist = FindNodeInRelist(relist, tmpId);
  10.         if (!inRelist && (it->count<mSup))
  11.         {
  12.             cout << "deleted1 : " << it->id << "*, mSup : " << mSup << endl;
  13.             fprintf(out, "deleted1 : %d*, mSup : %d\n", it->id, mSup);
  14.             try 
  15.             {
  16.                 m_pTransactionTree->root->erase(it);
  17.             }
  18.             catch (CException* e)
  19.             {
  20.                 e->Delete();
  21.             }
  22.         }
  23.     }
  24.     else
  25.     {
  26.         set<CTTreeNode> *children = it->children; 
  27.         set<CTTreeNode>::iterator itC = children->begin();
  28.         while (itC != children->end())
  29.         {
  30.             cout << "delete children : " << itC->id << ", count : " << itC->count << endl;
  31.             fprintf(out, "delete children : %d, count : %d\n", itC->id, itC->count);
  32.             DeleteNodeRecur(itC, mSup, relist);
  33.             itC++;
  34.         }
  35.         inRelist = FindNodeInRelist(relist, it->id);
  36.         if (!inRelist && (it->count<mSup)) 
  37.         { 
  38.             cout << "deleted2 : " << it->id << "*, mSup : " << mSup << endl;
  39.             fprintf(out, "deleted2 : %d*, mSup : %d\n", it->id, mSup);
  40.             try 
  41.             {
  42.                 m_pTransactionTree->root->erase(it);
  43.             }
  44.             catch (CException* e)
  45.             {
  46.                 e->Delete();
  47.             }
  48.         }
  49.     }
  50. }
  51.  
Is there something I have missed? Thanks.
Apr 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Robin Tucker | last post by:
Hi, I'm currently implementing a database with a tree structure in a table. The nodes in the tree are stored as records with a column called "Parent". The root of the tree has a "NULL" parent....
0
by: nimisha | last post by:
I have been trying to learn how to do a c++ tic tac toe game, with first implementing the game tree in a function and then having the AI in another function which uses minimax algorithm. I came...
10
by: Will Honea | last post by:
I have a data set which I need to analyze but I am having a problem figuring out a structure for the database - or whether there are better ways of attacking the problem. The base data set is a...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
8
by: Travis | last post by:
I have a tree of pointers and I need the ability to search the tree for a specific node. The problem is I can't provide that pointer for the node I'm looking for as criteria. The only critieria I...
6
by: csharpula csharp | last post by:
Hello , I would like to build a tree structure in c# using the arraylist or hash table. what is the best way to implement it if I want to add children and to print my tree. Thank you! ***...
1
by: ursskmdali | last post by:
Hi... all.. I don't know Python completely. I have a question in mind, is there any advanced controls for implementing Tree View control in Python. I am currently working on C#. I just want to...
2
by: ursskmdali | last post by:
Hi.. All I know Core Java. I just want to know is there any built-in controls for implementing tree view control in JAVA. I am currently working on C#. I just want to confirm that is it easy to...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
5
by: nandor.sieben | last post by:
I am using a small set of functions that implements an n-ary tree. The library is disscusses here: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.