Let's consider the worst case:
At each stage every tree is in the maximally imbalanced state, i.e. each node has at least one sub-tree of size 1.
In this extremal case the complexity of insert
is quite easily shown to be Ө(n)
where n
is the number of elements in the tree, as the height is ~ n/2
.
Based on the above constraint, we can deduce a recurrence relation for the time complexity of merge
:

where n, m
are the sizes of t1, t2
. It is assumed without loss of generality that the right sub-tree always contains a single element. The terms correspond to:
T(n - 2, 1)
: the inner call to merge
on the sub-trees of t1
T(n - 1, m)
: the outer call to merge
on t2
Ө(n + m)
: the final call to insert
To solve this, let's re-substitute the first term and observe a pattern:

We can solve this sum by stripping out the first term:

Where in step (*)
we used a change-in-variable substitution i -> i + 1
. The recursion stops when k = n
:

T(1, m)
is just the insertion of an element into a tree of size m
, which is obviously Ө(m)
in our assumed setup.
Therefore the absolute worst-case time complexity of merge
is

Notes:
- The order of the parameters matters. It is thus common to insert the smaller tree into the larger tree (in a manner of speaking).
- Realistically you are extremely unlikely to have maximally imbalanced trees at every stage of the procedure. The average case will naturally involve semi-balanced trees.
- The optimal case (i.e. always perfectly balanced trees) is much more complex (I am unsure that an analytical solution like the above exists; see
gdelab
's answer).
EDIT: How to evaluate the exponential sum
Suppose we want to compute the sum:

where a, b, c, n
are positive constants. In the second step we changed the base to e
(the natural exponential constant). With this substitution we can treat ln c
as a variable x
, differentiate a geometrical progression with respect to it, then set x = ln c
:

But the geometrical progression has a closed-form solution (a standard formula which is not difficult to derive):

And so we can differentiate this result with respect to x
by n
times to obtain an expression for Sn
. For the problem above we only need the first two powers:

So that troublesome term is given by:

which is exactly what Wolfram Alpha directly quoted. As you can see, the basic idea behind this was simple, although the algebra was incredibly tedious.