左偏树

xiaoxiao2021-02-28  173

左偏树:和二叉堆差不多的数据结构,有2个性质:①父节点都小于(大于)子节点;②对于一个节点,其左子树的最小深度>右子树的最小深度。

利用左偏树的性质,向左偏树中插入一个子树B时,总是往右边插入的,如果某一子树A的根节点的权值>B的根节点的权值,则将B代替子树A,再将子树A插入子树B中,这样可以保证所有父节点的值小于子节点。

struct node { int lson, rson, h; LL w; } t[MX]; void init(int rt, int w) { t[rt].w = w; t[rt].h = -1; t[rt].lson = t[rt].rson = 0; } int merge(int A, int B) { if (!A) return B; if (!B) return A; if (t[A].w < t[B].w) swap(A, B); t[A].rson = merge(t[A].rson, B); int ls = t[A].lson, rs = t[A].rson; if (t[ls].h < t[rs].h) { swap(ls, rs); swap(t[A].lson, t[A].rson); } t[A].h = t[rs].h + 1; return A; }

转载请注明原文地址: https://www.6miu.com/read-26122.html

最新回复(0)