Subj : Re: How to deal with Tree with several branchs? To : comp.programming From : Willem Date : Tue Jul 26 2005 08:56 pm Davy wrote: ) My program deal with a several level tree with several branchs. The ) amount of branchs from father node is not known. So I want to creat a ) tree with dynamic branchs. ) ) Now I use the structure: ) typedef struct gnodes ) { ) int level; //tree level ) struct gnodes* pfather; ) struct gnodes* pbrother; ) struct gnodes* pchild; ) struct gnodes* puncle; ) } gnode; ) ) The child pointer link one by one form a chain, something like: ) Father(i)->Child(1)->Child(2)->...->Child(n-1)->Child(n). ) But I can not search child randomly. ) ) How to design a better structure? ) Any suggestions will be appreciated! One classical way is to have a tree with multiple branches is like this: struct node { struct node *sibling; struct node *child; <... node data ...> } Why yes, that does look an awful lot like a binary tree, doesn't it ? In fact, you can store any tree in a binary tree. Another classical way is to have something like: struct node { struct node **children; <... node data ...> } Which requires allocating space from the heap every time, but means you can access the last child without having to walk the sibling list. Take your pick SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT .