Subj : Re: How to deal with Tree with several branchs? To : comp.programming From : Rob Thorpe Date : Wed Jul 27 2005 02:08 am Willem wrote: > 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. I'd do it this way. Some common tree operation are simpler if you do it this way, rather than using the sort of tree first suggested where many child nodes branch out from one node. .