Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red-Black Trees.

Similar presentations


Presentation on theme: "Red-Black Trees."— Presentation transcript:

1 Red-Black Trees

2 Red-black trees: Overview
Red-black trees are a variation of binary search trees to ensure that the tree is balanced. Height is at most 2lg(n+1), O(lg n), where n is the number of nodes. Operations take O(lg n) time in the worst case.

3 Red-black Tree Binary search tree + 1 bit per node: the attribute color, which is either red or black. All other attributes of BSTs are inherited. All empty trees (leaves) are colored black. We use a single sentinel, NIL, for all the leaves of red-black tree T, with color(NIL)= black. The root’s parent (if parents are stored) is also NIL.

4 Red-black Tree – Example
Every internal node has two children, even though nil leaves are not usually shown. 26 17 41 NIL 30 47 38 50

5 Red-black Properties Every node is either red or black.
The root is black. Every leaf (nil) is black. If a node is red, then both its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes.

6 Height of a Red-black Tree
Height of a node: Number of edges in a longest path to a leaf. Black-height of a node x, bh(x): bh(x) is the number of black nodes (including NIL) on the path from x to leaf, not counting x. Black-height of a red-black tree is the black-height of its root. By Property 5, black height is well defined.

7 Height of a Red-black Tree
Example: Height of a node: h(x) = # of edges in a longest path to a leaf. Black-height of a node bh(x) = # of black nodes on path from x to leaf, not counting x. How are they related? bh(x) ≤ h(x) ≤ 2 bh(x) h=4 bh=2 26 h=3 bh=2 h=1 bh=1 17 41 h=2 bh=1 h=2 bh=1 30 47 h=1 bh=1 38 h=1 bh=1 50 NIL

8 Operations on RB Trees All operations can be performed in O(lg n) time. The query operations, which don’t modify the tree, are performed in exactly the same way as they are in BSTs. Insertion and Deletion are not straightforward.

9 Rotations Rotations are the basic tree-restructuring operation for almost all balanced search trees. Rotation takes a red-black-tree and a node, Changes pointers to change the local structure, and Won’t violate the binary-search-tree property. Left rotation and right rotation are inverses. y x Left-Rotate(T, x) Right-Rotate(T, y)

10 Insertion in RB Trees Insertion must preserve all red-black properties. Should an inserted node be colored Red? Black? Basic steps: Use Tree-Insert from BST (slightly modified) to insert a node x into T: Procedure RB-Insert(x). Color the node x red. Fix the modified tree by re-coloring nodes and performing rotation(s) to preserve RB tree property: Procedure RB-Insert-Fixup.

11 Insertion Possibilities
x is the root node, i.e., first node of red–black tree x's parent (P) is black x's parent (P) and uncle (U) are red x is added to right of left child, or x is added to left of right child (P is red and U is black) x is added to left of left child, or x is added to right of right child (P is red and U is black)

12 Insertion Possibilities
x is the root node, i.e., first node of red–black tree C C The current node is the root of the tree. In this case, it is repainted black to satisfy property 2 (the root is black).

13 Insertion Possibilities
x's parent (P) is black C A No RB Tree properties are invalidated. In this case, the tree is still valid and no changes are necessary.

14 Insertion Possibilities
x's parent (P) and uncle (U) are red Insert B C C C A D A D B B Parent and uncle are repainted black and the grandparent becomes red. However, the grandparent may now violate properties 2 (The root is black) or 4 (Both children of every red node are black) To fix this, the entire procedure is recursively performed on grandparent. In this case make the root black – now we are good.

15 Insertion Possibilities
P is red and U is black: x is added to right of left child, or x is added to left of right child C C A D B D A B A left rotation on the parent switches the roles of the current node and its parent; then, the former parent node is dealt with using case 5 (next slide).

16 Insertion Possibilities
P is red and U is black: x is added to left of left child, or x is added to right of right child C B B A C D D A A right rotation on the grandparent; then, the colors of parent and grandparent are switched. (or in symmetric case: a left rotation on grandparent…)

17 RB-Insert Example 8 11 10 18 26 22 7 3 Example: Insert x =15 15

18 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 8 11 10 18 26 22 7 3 Example: Insert x =15 Recolor, move the violation up the tree. 15

19 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 8 11 10 18 26 22 7 3 Example: Insert x =15 Recolor, move the violation up the tree. 15

20 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 18 Insert x =15 Recolor, move the violation up the tree. 10 22 8 11 26 RIGHT-ROTATE(18) 15

21 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 10 Insert x =15 Recolor, move the violation up the tree. 8 18 11 22 RIGHT-ROTATE(18) 15 26

22 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 10 Insert x =15 Recolor, move the violation up the tree. 8 18 11 22 RIGHT-ROTATE(18) 15 26 LEFT-ROTATE(7)

23 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 10 Example: Insert x =15 7 18 Recolor, move the violation up the tree. 3 8 11 22 15 26 RIGHT-ROTATE(18) LEFT-ROTATE(7)

24 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 10 Example: Insert x =15 7 18 Recolor, move the violation up the tree. 3 8 11 22 15 26 RIGHT-ROTATE(18) LEFT-ROTATE(7) and recolor.

25 Recolor, move the violation up the tree.
Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 10 Example: Insert x =15 7 18 Recolor, move the violation up the tree. 3 8 11 22 15 26 RIGHT-ROTATE(18) LEFT-ROTATE(7) and recolor. Done!

26 Deletion Deletion, like insertion, should preserve all the RB properties. The properties that may be violated depends on the color of the deleted node. Red – OK. Why? Black? Steps: Do regular BST deletion. Fix any violations of RB properties that may result.

27 Analysis O(lg n) time to get through RB-Delete up to the call of RB-Delete-Fixup. Within RB-Delete-Fixup: O(lg n) time.

28 videos https://www.youtube.com/watch?v=vDHFF4wjWYU


Download ppt "Red-Black Trees."

Similar presentations


Ads by Google