将二叉树上色为红黑树


16

常见的采访问题是给出一种算法,以确定给定的二叉树是否高度平衡(AVL树定义)。

我想知道我们是否可以对红黑树做类似的事情。

给定一个任意的未着色的二叉树(具有NULL节点),是否存在一种“快速”算法,该算法可以确定我们是否可以对节点Red / Black进行着色(并查找着色),以便它们满足Red-Black树的所有属性(这个问题的定义)?

最初的想法是,我们可以删除NULL节点,然后尝试递归地验证生成的树是否可以是一棵红黑树,但这似乎没有任何用处。

我在网上做了简短的搜索,但是似乎找不到任何可以解决这个问题的文件。

我可能错过了一些简单的东西。


我敢肯定,一棵树可以是红色,黑色当且仅当每个节点,从中空节点的路径最长不超过两倍长于最短的一个。这样够快吗?
KarolisJuodelė2013年

Answers:


12

如果对于一棵树的每个节点,从树到叶节点的最长路径比最短的树长不超过两倍,则该树具有红黑颜色。

这是一种计算任何节点颜色的算法 n

if n is root,
    n.color = black
    n.black-quota = height n / 2, rounded up.

else if n.parent is red,
    n.color = black
    n.black-quota = n.parent.black-quota.

else (n.parent is black)
    if n.min-height < n.parent.black-quota, then
        error "shortest path was too short"
    else if n.min-height = n.parent.black-quota then
        n.color = black
    else (n.min-height > n.parent.black-quota)
        n.color = red
    either way,
        n.black-quota = n.parent.black-quota - 1

n.black-quota是您希望看到的从节点到叶子的黑色节点数,n并且n.min-height是到最近叶子的距离。

为简便起见,令h n =m n =b(n)= n.black-quotah(n)= n.heightm(n)= n.min-height

定理:修正了一个二叉树。如果为每个节点Ñ Ťħ Ñ 2 Ñ 和节点- [R = Ť b [R [ 1TnTh(n)2m(n)r=root(T)T具有红黑色,从根到叶的每条路径上都有正好是br)个黑色节点。b(r)[12h(r),m(r)]Tb(r)

证明:b(n)

验证高度为1或2的所有四棵树均满足定理b(n)=1

根据红黑树的定义,根是黑色。令为具有黑色父级p的节点,使得b p [ 1np。然后bÑ=bp-1ħÑ=^ hp-1^ hÑÑp-1b(p)[12h(p),m(p)]b(n)=b(p)1h(n)=h(p)1h(n)m(n)m(p)1

假设该定理对于所有根为树成立,b r < b qrb(r)<b(q)

如果,则nb(n)=m(n)n根据归纳假设可以为红黑色。

如果然后bn=b(p)=12h(p)n不满足归纳假设,因此必须为红色。令cn的子代。hc=hp2bc=bp1=1b(n)=12h(n)1ncnh(c)=h(p)2。然后b(c)=b(p)1=12h(p)1=12h(c)c can be red-black colored by the inductive assumption.

Note that, by the same reasoning, if b(n)(12h(r),m(r)), then both n and a child of n satisfy the inductive assumption. Therefore n could have any color.


@Aryabhata, any traversal is fine, as long as the parent is seen before its children. I don't have a formal proof written, but I have an idea of how it would look. I'll try writing something up when I can.
Karolis Juodelė

@Aryabhata, i added a proof. Sorry I took so long.
Karolis Juodelė

@Aryabhata, the idea is that if b(p) of some node p is withing certain bounds, a child or grandchild c of p can have b(c) within those same bounds. Having b(n) in those bounds may correspond to n being black. Most of the proof is about bounding h and m of a child or grandchild, given h and m of the parent or grandparent. Your tree is certainly colorable. b(root)=8, left child is black and right child is red, the path of length 16 is brbrbr, the path of length 8 is bbbbbbbb, paths of 9 and 12 can have multiple valid colorings.
Karolis Juodelė


2

I believe Karolis' answer is correct (and a pretty nice characterization of red-black trees, giving an O(n) time algorithm), just wanted to add another possible answer.

One approach is to use dynamic programming.

Given a tree, for each node n, you construct two sets: SR(n) and SB(n) which contains the possible black-heights for the subtree rooted at n. SR(n) contains the black-heights assuming n is coloured Red, and SB(n) assuming n is coloured black.

Now given the sets for n.Left and n.Right (i.e direct children of n), we can compute the corresponding sets for n, by taking appropriate intersections and unions (and incrementing as needed).

I believe this comes out be an O(nlogn) time algorithm.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.