How would you calculate the time and space complexity for a tree algorithm that creates a copy of a tree, but reuses as much of the original tree as possible?
A /|\ B C G /|\ D E F
To change E, we can take a more efficient approach, rather than copying the entire tree:
- Copy E and change the value, call this E'
- Copy C and point it to E' instead of E, call this C'
- Copy A and point it to C', call this A'
The new tree looks like this.
A' /|\ B C'G /|\ D E'F
The time taken for updates completely depends which value you want to change and the number of parent nodes between it and the root, rather than the total number of items.
In a worst case scenario, to update a node, you must change
h items, where
h is the height (maximum depth) of the tree. Is it sane to express the complexity as
O(h) (rather than in terms of
If so, what would be a sane way to plot this on a graph that was using the y axis for
n and the
x axis for Big O? Include a secondary
y axis as
h? How would you show that
h is likely to be far smaller than