W.Z.Hai
W.Z.Hai

Reputation: 105

the lower bound time of building a binary search tree by inserting n random elements sequently

The time can be proportional to sum of depth of all nodes.

T(n): the minimal depth sum of all possible BSTs with n elements.

So, T(n) = n-1 + min(T(i)+T(n-1-i)), i from 0 to n-1.

I think that the lower bound should be Θ(n lg n), but I can not prove it.

Upvotes: 0

Views: 181

Answers (1)

trincot
trincot

Reputation: 350781

The lower bound time complexity for building a binary tree from an input stream would be achieved when we can add nodes as high up in the tree as possible (having the least depth). This way the tree remains optimally balanced throughout the process. For instance, we could have this input stream:

4 2 6 1 3 5 7

...which really provides the values for the future tree in level order (breadth-first order).

The time complexity for the individual insertions is linear to the number of nodes to traverse, i.e. the height of the tree at the moment of insertion. This height is O(log𝑛), where 𝑛 is the size of the tree at the moment of insertion.

In general, we have this equality for the combined work of all insertions:

      T𝑛 = Θ( ∑𝑖=1..𝑛 log𝑖 )

Which by the logarithm product rule is:

      T𝑛 = Θ(log(𝑛!))

Which by Stirling's approximation means that:

      T𝑛 = Θ(𝑛log𝑛)

Upvotes: 2

Related Questions