analysis of red-black tree because of the rules of the red-black tree, its height is at most 2log(n...
TRANSCRIPT
Analysis of Red-Black Tree
Because of the rules of the Red-Black tree, its height is atmost 2log(N + 1).
Meaning that it is a balanced tree
Time Analysis:
1. Finding any node will take O(log N) time.2. Inserting any node will take O(log N) time.3. Deleting any node will take O(log N) time.
What is a Red-Black Tree?
Other than being a Binary Search Tree it has these specialcoloring properties:
1. Every node is colored either red or black.2. The root is always black.3. If a node is red, its children must be black.
4. Every path from a node to a null reference must containthe same number of black nodes.
Yes rule #4 is somewhat confusing Don’t Panic we will go over it
Rule #4Every path from a node to a null reference must containthe same number of black nodes.
B
DA
C
1
2 3
4 5
X Y1. Side X you count 1 blacknode to any null reference.
2. Side Y you count 1 blacknode to any null reference.
This Balance must bemaintained through out thetree.
More on Coloring1. On any path from the root to a leaf, red nodes must not be adjacent.
2. However, any number of black nodes may appear in a sequence.
3. Every time you add a node it begins as red.
4. All null references are defined to be black.
Now to Insert a node
Case 1a) x’s uncle is Red, and x is right child.
B
DA
C
1
2 3
4 5
x
y
B
DA
C
1
2 3
4 5
New x
X represents the newly inserted node
Insertion
A
DB
C
1 2
3 4 5
x
y
A
DB
C
1 2
3 4 5
New x
Case 1b) x’s uncle is Red, and x is left child
Note: For all Cases when x is rootchange x to black.
InsertionCase 2) x’s uncle is Black, and x is a right child.
Note: Triangles representNull reference.
C
B
A
C
B
A
1 2
4 4
31
32
x
Left rotationof A
InsertionCase 3) x’s uncle is Black, x is left child.
C
B
A
1 2
4
3
B
CA
1 2 43
Right rotation and recolor.
When Inserting a NodeRemember:1. Insert nodes one at a time, and after every Insertionbalance the tree.
2. Every node inserted starts as a Red node.
3. Consult the cases, Every time two Red nodes touchmust rebalance at that point.
4. The root will always be Black.
Deleting a NodeAnd you thought Inserting a Node was fun, lets try Deleting.
Rules:If we delete a Red node, tree is Still a Red-Black tree.
Assume we delete a Black node:Let x be the child of the deleted node and w be its sibling.
If (x is Red, change it to Black and stop)
Else If (x is Black, mark it double black and applythe following)
Deleting a NodeCase 1) x’s sibling is red.
65
21 43654
1 2
3
A C
D
E
B
C
A
E
D B
X stays at the same black height, Continue down the tree.
x
x
w
w
Single rotation and Recolor.
Deleting a NodeCase 2a) x’s sibling is black, x’s parent is black
65431 2
B
D
CA
E
65431 2
B
CA
E
Dx
Re-color w, Decrease x height by one and continue up tree.
wx
Deleting a NodeCase 2b) x’s sibling is black, x’s parent is red.
65431 2
B
CA
E
D
65431 2
D
CA
E
B
Swap x’s parent and x’s, siblings color.Terminal case is Red-Black Tree.
xw
Deleting a NodeCase 3) x’s parent is either, x’s sibling is black, x’s siblings leftchild is red, x’s siblings right child is black.
65431 2
D
B
AEC
xw
65
4
3
1 2
C
B
A
E
D
xw
X’s black height stays same, change to case 4.
Deleting a NodeCase 4) x’s parent is either, x’s sibling is black, x’s siblingsleft child is either, x’s siblings right child is red.
B
C
B
D
E
DA E
A C
65
436543
1 2
1 2
x w
Terminal case, tree is Red-Black tree.
Deleting a Node
To deleting a node that has two children, we replace thedeleted node with the smallest node on its right sub-tree.
You cannot use given cases unless the node to be deletedhas one or fewer children.
Java Demo
http://gauss.ececs.uc.edu/RedBlackMozilla/redblack.html
Great Demo that shows step by step Insertionand Deletion.
Also contains Java source code.
Review for QuizInsertion:1. When building a tree, insert One node at a time.2. After inserting a node, consult your cases and balancethe tree.
Deletion:3. After deleting a node, consult cases, and balance tree.
Remember:4. No two Red nodes can touch.5. All paths through tree must contain same number ofBlack nodes.6. Must always have Binary Search Tree balance.