see:http://acm.sdut.edu.cn/bbs/read.php?tid=1177 Huangyuan: The characteristics and application of left-leaning tree
1. Definition:
The left-leaning tree (leftist) is an implementation of a scalable heap. Left-leaning tree is a binary tree, its node in addition to the two-fork tree node with the left and right subtree pointer (right), there are two properties: Key value and distance (Dist). The key values have been mentioned above and are used to compare the size of the nodes. The distance is defined as follows:
Node i is called an outer node (external node) if and only if the left subtree or the right subtree of node I is empty (left-hand (i) = null or OK (i) = null); The distance of the node I (Dist (i)) is the number of edges that the nearest outer node passes through the node I to its descendants. In particular, if the node I itself is an outer node, its distance is 0, and the distance of the null node is set to 1 (dist (NULL) =-1). In this article, we sometimes mention the distance of a left-leaning tree, which refers to the distance of the root node.
2. Advantages:
Merge complexity O (log N)
The merge complexity of the minimum heap is O (N).
3. Code:
#include <cstdio> #include <algorithm> using namespace std;
#define TYPEC int const int NA =-1;
const int N = 1000;
struct Node {typec key;
int L, R, F, Dist;
}tr[n];
Find I ' s root int iroot (int i) {if (i = = NA) {return i;
while (tr[i].f!= na) {i = TR[I].F;
return i;
}//Two root:rx, ry int merge (int rx, int ry) {if (Rx = na) {return ry;
} if (ry = = NA) {return rx;
} if (Tr[rx].key > Tr[ry].key) {swap (rx, ry);
int r = Merge (TR[RX].R, Ry);
TR[RX].R = R;
TR[R].F = Rx;
if (Tr[r].dist > Tr[tr[rx].l].dist) {swap (TR[RX].L, TR[RX].R);
} if (tr[rx].r = = na) {tr[rx].dist = 0;
else {tr[rx].dist = tr[tr[rx].r].dist + 1;
return RX;
//Add a new node (I, key) int ins (int i, typec key, int root) {Tr[i].key = key;
TR[I].L = TR[I].R = Tr[i].f = na;
tr[i].dist = 0;
return root = Merge (root, i);
}//delete node I int del (int i) {if (i = = NA) {return i;
int x, Y, L, R;
L = TR[I].L; R = TR[I].R;
y = tr[i].f;
TR[I].L = TR[I].R = Tr[i].f = na;
Tr[x=merge (l,r)].f = y;
if (y!= na && tr[y].l = i) {tr[y].l = x;
} if (y!= na && tr[y].r = i) {TR[Y].R = x;
for (; y!= na; x = y, y = tr[y].f) {if (Tr[tr[y].l].dist < tr[tr[y].r].dist) {swap (TR[Y].L, TR[Y].R);
} if (tr[tr[y].r].dist + 1 = tr[y].dist) {break;
} tr[y].dist = tr[tr[y].r].dist + 1;
} if (x!= na) {return iroot (x);
else {return iroot (y);
node top (int root) {return tr[root];}
Node pop (int &root) {node out = Tr[root];
int L = TR[ROOT].L, r = TR[ROOT].R;
TR[ROOT].L = TR[ROOT].R = Tr[root].f = na;
tr[l].f = Tr[r].f = na;
Root = Merge (L, R);
return out;
int add (int i, Typec val) {if (i = = NA) {return i;
} if (tr[i].l = na && tr[i].r = na && tr[i].f = na) {Tr[i].key + = val;
return i;
} typec key = Tr[i].key + val;
int rt = Del (i);
return ins (i, key, RT); } void init (int n) {for(int i = 1; i < N; i++)
{scanf ("%d", &tr[i].key);
TR[I].L = TR[I].R = Tr[i].f = na;
tr[i].dist = 0; }//Print the info of node I void print (int i) {printf ("Node%d:l->%d, r->%d, f->%d, dist->%d\n",
I, TR[I].L, TR[I].R, TR[I].F, tr[i].dist);
int main () {int root = NA;
for (int i = 1; i < i++) {root = Ins (I, I, root);
for (int i = 1; i < i++) {print (i);
} del (1);
for (int i = 1; i < i++) {print (i);
return 0;
}