Hello,all!
I work on a kind of algorithm where the speed and memory consumption are critical. I understand that these are tradeofs but I want to try to reach an optimal solution.
Now I'll try describe the problem: I have a tree where every node have a variable number of children (maximum . The number of elements can reach many millions. In the algoritm I need to pass through all when for every node I pass through all his children one after another.
I want to ask your advice about the implementation. I have some possibilites and want to choose the optimal. So, the possibilities are:
1) To build it in a tree manner. I define a structure that is similar to a linked list, but the field 'next' will be dynamically allocated to contain the actual number of children. This seems to me the most straightforward implementation,
however it begins to be a bit complicated to allocate the memory everytime and to free all of it at the end. This way also forces me to work in the recursive way and I don't know how efficient it is.
2) To build one single linked list when I arrange the elements in a such way that for a node m at level n the children will be after the children of a node at place m-1. For every node I add a link to his first child and save the number of his children.
In this way, given a node I can pass through all his children at O(N) time.This also forces me to work on the algorithm in the recursion.
3) To work like in step "2" but instead of a linked list to add elements to the array using realloc function. However I don't if this way is fast and efficient. Also if I have to pass through all the nodes one after another, does the array have the advantage over a linked list in the access speed?
4) To work like in "2" and after it to create an array and transfer the elements from the linked list to the array.
Basically,the main advantage of the array is that it has an access of O(1) to every element and I can implement an algorithm in a non recursive way.
Therefore the main question is: for a recursive solution which is better 2 or 1, and for non recursive 3 or 4?
Thanks alot!
mshr25