Please consider offering answers and suggestions to help other students!
And if you fix a problem by following a suggestion here,
it would be great if other interested students could see a short
"Great, fixed it!" followup message.
Is O(1) achievable?
The add_child function asks for a target of O(1), a tree data structure wouldn't be able to achieve this, finding from root the parent to link the child would be O(n). A set would meet O(1) but would have trouble with get_primogeniture_order which asks for O(n) and going through the set several times to find the eldest child of the eldest child of the... and so on would be O(n^2) because if it was held in n + n-1 + n-2 ... +1 that solves to O(n^2).
So far I have opted for a hashmap, which under most circumstances is O(1) under the very rare occurrence that everything hashes to the same key, then it would have worst case O(n).
Am I missing a data structure that could achieve all target time complexities or does target time complexity allow for hashtables that would be just off?
Kind Regards
Bill
I found that only the absolutely simplest structure lets me have O(1) so simple data structures built into python are a good start on finding the right one.