Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.

Similar presentations


Presentation on theme: "Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A."— Presentation transcript:

1 Red-Black Trees an alternative to AVL trees

2 Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced tree supports efficient operations, since most operations only have to traverse one or two root-to-leaf paths. There are many implementations of balanced BSTs, including AVL trees, Red- Black trees and AA trees.

3 Properties of Red-Black Trees A Red-Black tree satisfies the following properties: 1Every node is colored either red or black 2The root is black 3If a node is red, both of its children are black. 4Every path from a node to a null reference has the same number of black nodes

4 Advantage of Red-Black Trees The main advantage of Red-Black trees over AVL trees is that a single top-down pass may be used in both insertion and deletion routines.

5 Example of a Red-Black Tree 30 70 85 5 60 80 10 90 15 20 50 4055 65

6 Properties in the example In our drawing, the black nodes are uncolored, and the red ones are colored the black squares represent null references As you can see in the example Red-Black tree, there are 3 black nodes on every path from the root to a null reference

7 Properties of Red-Black trees If every path from the root to a null reference contains B black nodes, then there must be at least 2 B - 1 black nodes in the tree. Since the root is black and there cannot be two consecutive red nodes on a path, the height of a red-black tree is at most 2log(N + 1)

8 Bottom-up insertion A new item is always inserted as a leaf in the tree If we color a leaf black, we will create a longer path of black nodes (violating property 4) Therefore, a new item must be colored red [unless it is the root] if the parent is colored black, we are done

9 Bottom-up insertion 2 If the parent is red, we will have consecutive red nodes (violating property 3) We must adjust the tree to ensure property three, without introducing a violation to property 4 The operations are rotations and color changes.

10 Case 1a Sibling of the parent is black [adopt the convention that null references are black] Inserted node is an outside grandchild A single rotation between the parent and the grandparent, with appropriate color changes, restores property 3

11 Single rotation X P G S AB C DE X P G S AB C DE

12 Notes Notice that before insertion of node X, there was one black node from G to each of A, B, and C, and two black nodes from G to each of D and E After the rotation and recoloring, notice that the number of black nodes on each of those paths remains unchanged Property 3 has been restored

13 Case 1b Sibling of the parent is black [adopt the convention that null references are black] Inserted node is an inside grandchild A double rotation between the parent and the grandparent, with appropriate color changes, restores property 3

14 Double Rotation X P G S A BC DE P X G S AB C DE

15 Notes As in the case of the single rotation, before insertion of node X, there was one black node from G to each of A, B, and C, and two black nodes from G to each of D and E After the rotation and recoloring, notice that the number of black nodes on each of those paths remains unchanged Property 3 has been restored

16 Case 2 Sibling of the parent is red Neither single nor double rotations work, since both result in (possibly) consecutive red nodes

17 Single Rotation X P G S AB C DE X P G S AB C DE

18 Notes This fixes property 3 for this subtree What happens if the parent of this subtree is also red? We could percolate this procedure up toward the root until we no longer have two consecutive re nodes, or we reach the root The advantage over AVL trees has disappeared

19 Top-down Red-Black trees To avoid having to percolate rotations up the tree, we may apply a top-down procedure as we search down the tree for the insertion point Specifically, we guarantee that when we arrive at the insertion point, S, the sibling of the parent, will not be red. After inserting a red leaf, one rotation is sufficient to correct the tree

20 The procedure On the way down, when we see a node X that has two red children, we make X red and its two children black If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case

21 Example - insert 45 into this tree 30 70 85 5 60 80 10 90 15 20 50 4055 65 Two red children

22 Color flip 30 70 85 5 60 80 10 90 15 20 50 4055 65 flip colors - two red nodes

23 Single Rotation 30 70 85 5 60 80 10 90 15 20 50 4055 65

24 Now Insert 45 30 70 85 5 60 80 10 90 15 20 50 4055 65 45

25 Notes Since the parent of the newly inserted node was black, we are done Had the parent of the inserted node been red, one more rotation would have had to be performed Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees

26 Top-Down deletions Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child Nodes with two children are never removed. Their contents are just replaced

27 Top-down deletions 2 If the node to be deleted is red, there is no problem -- just delete the node If the node to be deleted is black, its removal will violate property 4 The solution is to ensure that any node to be deleted is red

28 Red-Black Tree Demo Demo is here


Download ppt "Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A."

Similar presentations


Ads by Google