Leftist heap merging is VERY slow

My implementation of a skew heap builds 50,000 items in a few milliseconds, but my leftist heap takes several seconds.

It's behaving properly when I trace through it with only ~10 elements, but it doesn't seem to be scaling properly with a larger number of items.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
struct Lode
{
    int getRank()
    {
        rank = 1;

        if (left || right)
        {
            int rankL = 0, rankR = 0;

            if (left)  rankL =  left->getRank();
            if (right) rankR = right->getRank();

            if (rankL < rankR)
                rank = rankL + 1;
            else
                rank = rankR + 1;
        }
        return rank;
    }
};

struct Leap
{
    Lode* merge(Lode* x, Lode* y)
    {
        if (!x) return y;
        if (!y) return x;

        if (x->data > y->data)
            swap(x,y);

        x->right = merge(x->right, y);

        x->getRank();
        int rankL = 0, rankR = 0;
        if (x->left)  rankL = x->left->rank;
        if (x->right) rankR = x->right->rank;

        if (rankL < rankR)
            swap(x->left, x->right);

        return x;
    }
}

Irrelevant(?) code omitted.
Last edited on
Topic archived. No new replies allowed.