hi kezia,
U seem to be
testing our datastructures/algo skills :-)
did that long back (uhhm well ,2nd year of my bachelors)
well let me try,
Consider a balanced tree ( i guess that's the name for a tree with equal number of nodes on either side of the root?? ..u need get this confirmed :-) for me.
if the height of the tree is h, then the maximum number of comparisons u need to make c'd be h+1 (1 for root as well).
If u traverse a tree from root covering the complete height h, then
number of nodes traversed
n = 1 (root) + 2 exp 1 + 2 (exp) 2 + 2 (exp) 3....2 (exp) h
= 2 exp 0 +.....
= 2 exp (h+1) - 1
now ignoring "1"s we get
n = 2 (exp) h
if u take log to base 2 of both the sides, u get
log n(base 2) = h; got it??
Now suppose u intend to add an elemnt to TreeSort which implemets SortedSet,
At the most u might have to travel all the way to hieght of the tree, so u need to make comparisons at everynode upto the tree, so that u can place it to the right/left child of a particular node, right?.
this max c'd be h+1 (1 for root as well).
so say c is the time for 1 comparison,
the total time w'd be c(h+1)?
now h = log n(base 2);
subsituting u'll have
total time = c(logn) /// ignore 1
total time = (some constant) X (logn) right?
so total time for addition is proportional to log(n).
Am not sure if my explanation made any sense to you.
Others can probably explain it in a better way.
karthik.