Implementation 1:
struct treenode{
int data;
struct treenode children[2];
}
Implementation 2:
struct treenode{
int data;
struct treenode *children;
}
Here's what I'm thinking:
Implementation 2 would be better in terms of space complexity because
- Undefined children wouldn't need a malloc in imp. 2, while (I presume) the arrays in imp.1 automatically malloc space for two treenodes on declaration?
However, each node ultimately just carries an int and a pointer for both implementations, so the space complexity can't differ that much, right? I used implementation 1 to avoid the possibility of using double pointers and getting confused (lol...), and my program ended up getting "Killed" when I tried building a tree with a large amount of data. Many of my classmates didn't have this problem, and I deviated from how the prof hinted that we should build the tree (using implementation 2).
Is there a good chance that I had this issue because I used implementation 1? Why is it so much more problematic if both nodes ultimately carry the same thing? Is it because big data leads to a ton of leaf nodes that get malloced in one case (1), but not in the other (2)? Or am I just completely wrong with my interpretation of how the space complexity differs in each case?