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
4 5569
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.
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
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...
-
class CTTreeNode //transaction tree node
-
{
-
public:
-
-
CTTreeNode();
-
CTTreeNode(int s, CTTreeNode *p);
-
~CTTreeNode();
-
-
int id;
-
int count;
-
-
set<CTTreeNode> *children;
-
CTTreeNode *parent;
-
-
bool operator< (const CTTreeNode &tn) const {return id < tn.id;}
-
};
-
-
class CTTree //transaction tree
-
{
-
public:
-
CTTree();
-
virtual ~CTTree();
-
void processTransaction(Transaction *t);
-
-
set<CTTreeNode> *root;
-
};
-
-
CTTreeNode::CTTreeNode()
-
{
-
count = 0;
-
parent = 0;
-
id = 0;
-
children = 0;
-
}
-
-
CTTreeNode::~CTTreeNode()
-
{
-
-
}
-
-
CTTreeNode::CTTreeNode(int s, CTTreeNode *p)
-
{
-
id = s;
-
parent = p;
-
-
count = 0;
-
children = 0;
-
}
-
-
CTTree::CTTree()
-
{
-
root = new set<CTTreeNode>;
-
CTTreeNode rootnode(0, 0);
-
rootnode.children = new set<CTTreeNode>();
-
root->insert(rootnode);
-
}
-
-
CTTree::~CTTree()
-
{
-
set<CTTreeNode>::iterator it;
-
for(it = root->begin(); it != root->end(); it++) {
-
if(it->children != 0)
-
delete it->children;
-
}
-
delete root;
-
}
-
-
void CTTree::processTransaction(Transaction *t)
-
{
-
set<CTTreeNode>::iterator it;
-
set<CTTreeNode>* nodes = root->begin()->children;
-
CTTreeNode *current = 0;
-
-
for(int depth=0; depth < t->length; depth++) {
-
it = nodes->insert(CTTreeNode(t->t[depth], current)).first;//because insert check first
-
it->count++;
-
current = &(*it);
-
if(it->children==0)
-
it->children = new set<CTTreeNode>;
-
nodes = it->children;
-
}
-
}
-
-
void CTTree::print(FILE* out)
-
{
-
set<CTTreeNode>::iterator it = root->begin();
-
printRecur(it, out);
-
}
-
-
void CTTree::printRecur(set<CTTreeNode>::iterator it, FILE* out)
-
{
-
fprintf(out, "%d, %d\n", it->id, it->count);
-
//cout << it->id << ", " << it->count << endl;
-
-
set<CTTreeNode> *children = it->children;
-
for(set<CTTreeNode>::iterator itC=children->begin(); itC != children->end(); itC++)
-
printRecur(itC, out);
-
}
-
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. -
void CTMmine::DeleteNodeRecur(set<CTTreeNode>::iterator it, int mSup, set<CElement> *relist)
-
{
-
bool inRelist;
-
bool child = HasChild(it);
-
int tmpId = it->id;
-
-
if (!child)
-
{
-
inRelist = FindNodeInRelist(relist, tmpId);
-
if (!inRelist && (it->count<mSup))
-
{
-
cout << "deleted1 : " << it->id << "*, mSup : " << mSup << endl;
-
fprintf(out, "deleted1 : %d*, mSup : %d\n", it->id, mSup);
-
try
-
{
-
m_pTransactionTree->root->erase(it);
-
}
-
catch (CException* e)
-
{
-
e->Delete();
-
}
-
}
-
}
-
else
-
{
-
set<CTTreeNode> *children = it->children;
-
set<CTTreeNode>::iterator itC = children->begin();
-
while (itC != children->end())
-
{
-
cout << "delete children : " << itC->id << ", count : " << itC->count << endl;
-
fprintf(out, "delete children : %d, count : %d\n", itC->id, itC->count);
-
DeleteNodeRecur(itC, mSup, relist);
-
itC++;
-
}
-
inRelist = FindNodeInRelist(relist, it->id);
-
if (!inRelist && (it->count<mSup))
-
{
-
cout << "deleted2 : " << it->id << "*, mSup : " << mSup << endl;
-
fprintf(out, "deleted2 : %d*, mSup : %d\n", it->id, mSup);
-
try
-
{
-
m_pTransactionTree->root->erase(it);
-
}
-
catch (CException* e)
-
{
-
e->Delete();
-
}
-
}
-
}
-
}
-
Is there something I have missed? Thanks.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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!
***...
|
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...
|
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...
|
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...
|
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:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |