Click to See Complete Forum and Search --> : Tree class


shubhishubhi
September 25th, 2006, 12:30 AM
How to write a simple and efficient tree class in MFC. It can have any number of branches at each level. Just a general structure to represent hierarchical relationship.

CBasicNet
September 25th, 2006, 04:01 AM
That would depends on how you want to traverse the tree for its operation.

Binary Trees (http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1073433376&id=1073086407)

There are 4 types of tree traversal, namely, Breadth-First Traversal
Depth-First Traversal(pre-order, in-order and post-order). They can be written for either recursive access or iterative access. Through the link above is for binary tree, I found it useful when writing trees with more than 2 nodes at each level.

In order for your tree to be re-useful, the node data it keeps, have to be generic. In C++, it would means you have to write it using templates. On the other hand, you can use this library, Tree Container Library (http://www.codeguru.com/cpp/misc/misc/templatizedclasses/article.php/c11203) but I have to tell you I have not used it before, any problems, please ask the library author.

From my experience of writing trees, writing recursive traversal is quite easy but difficult to use, as you have to pass in a function pointer or function object which will be called for each node traversal, even more troublesome if you have to store the stateful information (in the function object) between each node traversal. Writing iterative traversal is much more difficult but it is much more easier to use, imo.

RoboTact
September 25th, 2006, 07:18 PM
How to write a simple and efficient tree class in MFC. It can have any number of branches at each level. Just a general structure to represent hierarchical relationship.struct node {
vector<node*> children;
}; (+some hiding, ownership, etc. to your test) - nothing more unless you specify what you really want...