Red-Black Trees

by math_explorer, Oct 22, 2010, 10:39 AM

A red-black tree is a binary search tree with extra "nil" nodes as leaves of every content node with a child-relationship to spare, and a "color", one of red or black, associated with every node, such that these properties are satisfied:
1. Every nil node is black.
2. No red node has a red node as a child.
3. Any path from the root to a leaf has the same number of black nodes.

A red-black tree can never have a depth more than $2 \lg n$ (maybe plus 1), where $n$ is the number of nodes. Therefore, operations like searching will always take $O(\lg n)$ time.
The only difficulty is the coding of the algorithms that modify the tree, namely, insert and delete, while preserving the three rules.
Note that we can always set the root to be black if it isn't.

Okay. Insert.

Proceed adding the node like in normal search trees. Search from the root to a "nil" node which could be replaced with the node. Replace it, color it red, and give it two black "nil" nodes. As a result, any path through that node would still get one black node from it or its children, so rule 3 is preserved. Rule 2 may not, unfortunately.

So, let the node in question be x. If x's parent is black, no problem. Of course according to Finagle's, x's parent will turn out to be red. (Note that since rule 2 held before we started, x's grandparent must be black (or not exist, but that case would be too easy (? (left to the reader))).) We look at the color of x's uncle, i.e. x's grandparent's other child.

If x's uncle is red, we can color x's parent and uncle black, x's grandparent red, set x to x's grandparent, and repeat.

If x's uncle is black, we'll do this awkward thing called "rotation", which involves switching a bunch of subtrees around, with the nodes, which goes like this:
Y ---  X
   X  3--- 1  Y
  1 2  ---2 3

"Left-rotation" turns the thing on the right to the thing on the left.
"Right-rotation" turns the thing on the left to the thing on the right.

Anyway, first we rotate x and x's parent if necessary, so that the path from x to x's grandparent is straight (right twice or left twice). Since x and x's parent are both red, no problem with rule 3 occurs. We'd also have to let x's previous-parent-and-now-child be the new x.

Next, remembering that x's uncle was black, we'll rotate-away-from-x x's grandparent and x's parent, and color x's previous-grandparent-and-now-sibling red (this works since x's uncle is black, which I've already stressed too much) and x's previous-parent-and-now-still-parent black. And we're done!

Deletion comes later.
This post has been edited 2 times. Last edited by math_explorer, Aug 19, 2011, 9:40 AM

Comment

0 Comments

♪ i just hope you understand / sometimes the clothes do not make the man ♫ // https://beta.vero.site/

avatar

math_explorer
Archives
+ September 2019
+ February 2018
+ December 2017
+ September 2017
+ July 2017
+ March 2017
+ January 2017
+ November 2016
+ October 2016
+ August 2016
+ February 2016
+ January 2016
+ September 2015
+ July 2015
+ June 2015
+ January 2015
+ July 2014
+ June 2014
inv
+ April 2014
+ December 2013
+ November 2013
+ September 2013
+ February 2013
+ April 2012
Shouts
Submit
  • how do you have so many posts

    by krithikrokcs, Jul 14, 2023, 6:20 PM

  • lol⠀⠀⠀⠀⠀

    by math_explorer, Jan 20, 2021, 8:43 AM

  • woah ancient blog

    by suvamkonar, Jan 20, 2021, 4:14 AM

  • https://artofproblemsolving.com/community/c47h361466

    by math_explorer, Jun 10, 2020, 1:20 AM

  • when did the first greed control game start?

    by piphi, May 30, 2020, 1:08 AM

  • ok..........

    by asdf334, Sep 10, 2019, 3:48 PM

  • There is one existing way to obtain contributorship documented on this blog. See if you can find it.

    by math_explorer, Sep 10, 2019, 2:03 PM

  • SO MANY VIEWS!!!
    PLEASE CONTRIB
    :)

    by asdf334, Sep 10, 2019, 1:58 PM

  • Hullo bye

    by AnArtist, Jan 15, 2019, 8:59 AM

  • Hullo bye

    by tastymath75025, Nov 22, 2018, 9:08 PM

  • Hullo bye

    by Kayak, Jul 22, 2018, 1:29 PM

  • It's sad; the blog is still active but not really ;-;

    by GeneralCobra19, Sep 21, 2017, 1:09 AM

  • dope css

    by zxcv1337, Mar 27, 2017, 4:44 AM

  • nice blog ^_^

    by chezbgone, Mar 28, 2016, 5:18 AM

  • shouts make blogs happier

    by briantix, Mar 18, 2016, 9:58 PM

91 shouts
Contributors
Tags
About Owner
  • Posts: 583
  • Joined: Dec 16, 2006
Blog Stats
  • Blog created: May 17, 2010
  • Total entries: 327
  • Total visits: 356617
  • Total comments: 368
Search Blog
a