Inserting and Deleting of Red-Black Trees

Inserting and Deleting of Red-Black Trees

For I hate to remember so many cases, like RRr, RLb, RLr, following are some in-order methods covering every case for operations in Red-Black Trees.

Inserting:
1. Find Uncle.
2. If uncle is red, change the colors of parent, uncle, and grandparent.
3. If black, rotate to balanced tree, and make the second level RED.

Deleting:
1. Find Sibling’s red descendant (including itself having lowest priority, inner path having highest priority, and outer path).
2. If inner path, change to balanced tree. If outer path, rotate in sequence.
3. If fail to find red descendant, make sibling RED in order to make parent be the new deficient node, and recurse.

Explanation:
Uncle and sibling, obviously, are the other child of grandparent and parent, respectively. They are d and v in the cases following.
[img]http://dl.iteye.com/upload/picture/pic/77245/663cf4bc-9335-3f0f-acfc-e0e725ab5916.png[/img][img]http://dl.iteye.com/upload/picture/pic/77251/371e5cf7-41ca-3765-ba89-4607b04ea13f.png[/img]

Rotate in sequence: the path has no polyline there, and move every node in path forward, keeping left and right positions the same.
[img]http://dl.iteye.com/upload/picture/pic/77249/6774b68c-8140-301e-8745-59cc46950b3a.png[/img]
Changing to a balanced tree, which is for inner path (or polyline path), make a node having smaller height to be the root of sub-tree.
[img]http://dl.iteye.com/upload/picture/pic/77247/a9ebadde-86cd-357e-ad4d-abe215183226.png[/img]

[table]
||Rotations|Complexity
|Insert in AVL|O(1)|O(log n)
|Delete in AVL|O(log n)|O(log n)
|Insert in Red-Black Tree|O(2)|O(log n)
|Delete in Red-Black Tree|O(3)|O(log n)
[/table]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值