Red-Black Tree

Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer

Proporties

  • A red–black tree is a type of self-balancing binary search tree.
  • The self-balancing is provided by painting each node with one of two colors (these are typically called ‘red’ and ‘black’, hence the name of the trees) in such a way that the resulting painted tree satisfies certain properties that don’t allow it to become significantly unbalanced.
  • The leaf nodes of red–black trees do not contain data. These leaves need not be explicit in computer memory—a null child pointer can encode the fact that this child is a leaf—but it simplifies some algorithms for operating on red–black trees if the leaves really are explicit nodes. To save memory, sometimes a single sentinel node performs the role of all leaf nodes; all references from internal nodes to leaf nodes then point to the sentinel node.
  • In addition to the requirements imposed on a binary search trees, with red–black trees:
    1. A node is either red or black.
    2. The root is black. (This rule is sometimes omitted. Since the root can always be changed from red to black, but not necessarily vice-versa, this rule has little effect on analysis.)
    3. All leaves (NIL) are black. (All leaves are same color as the root.)
    4. Every red node must have two black child nodes.
    5. Every path from a given node to any of its descendant leaves contains the same number of black nodes.
  • These constraints enforce a critical property of red–black trees: that the path from the root to the furthest leaf is no more than twice as long as the path from the root to the nearest leaf.

Operations

The searching in a Red-Black Tree is the same as that in a BST.

Insert

  • The first step of isertion in Red-Black Tree is almost the same as that in the BST, except for adding the new node as a red internal node with two black leaf nodes.
  • After that, to maintian the proporties, 5 cases should be considered. ‘N’ will denote the current node(‘P’ will denode the parent of ‘N‘, ‘G’ will denode grandparent of ‘N‘, ‘U’ will denode the uncle of ‘N‘) :
    1. N is the root of the tree. In this case, just repaint it black;
    2. P is red. In this case, nothing needs to do.
    3. P and U are all red. In this case, repaint P and U black, the repaint G red. Next G will become N, and check from case 1 again.
    4. P is red but U is black, and N is right child of P while P is left child of G(or N is left child of P while P is right child of G). In this case, performe left(or right) rotation to P, then becomes case 5;
    5. P is red but U is black, and N is left child of P while P is left child of G(or N is right child of P while P is right child of G). In this case, performe right(or left) rotation to G, the switch the color of P and G.

Remove

  • The first step of removing in a Red-Black tree is the same as that in a BST: if an internal one, replace it with the leftest node in the right subtree.
  • After that, 3 cases should be considered:
    • If a node with one child, delete it and paint it’s child black;
    • If a red leaf node, delete it directly;
    • If a black leaf node;
      • If the sibling is black:
        1. Sibling node has none red child: delete the node, repaint the sibling red and the parent black. If the parent was red, done; else, parent becomes the current node, continue;
        2. Sibling node has one red outer child: delete the node, if red child on the left, rotate right, else rotate left. Then repaint sibling the same color as the parent, repaint the parent and the red child black;
        3. Sibling node has one red iner child: delete the node, if red child on the right, first rotate left then rotate right, else first rotate right then rotate left. Then repaint the red child the same as the parent, repaint the parent black;
        4. Sibling node has two red children: delete the node, if sibling in the left, rotate first rotate left then rotate right, else first rotate right then rotate left. Then repaint the new root the same color as the previous root, repaint the previous root black.
      • If the sibling is red:
        1. If a left child, rotate left, switch the color of parent and sibling and delete it;
        2. If a right child, rotate right, switch the color of parent and sibling and delete it.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值