Monday, December 12, 2011

How can I vary the number of connections to nodes on a tree (programming)?

I want to built a tree, but not limit it to being a binary tree. I want to vary the number of connections of each node according to the circumstances.





Usually I'd define a struct to have 2 pointers to the left and the right branch. But what if each node requires a variable number of branches?|||The normal way would be to have an array of pointers in each node. For a binary the left would go in [0], the right would go in [1]. Depending what language you're using, you could declare the array statically in the node definition or declare it as a pointer-to-pointer-to-node and dynamically allocate it.

No comments:

Post a Comment