red black tree
TRANSCRIPT
![Page 1: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/1.jpg)
RED-BLACK TREE
![Page 2: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/2.jpg)
INTRODUCTION A balancing binary search tree.
A data structure requires an extra one bit color field in each node which is red or black.
Leonidas J. Guibas and Robert Sedgewick derived the red-black tree from the symmetric binary B-tree.
![Page 3: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/3.jpg)
EXAMPLE OF RED BLACK TREE
![Page 4: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/4.jpg)
PROPERTIES OF RED BLACK TREE The root and leaves (NIL’s) are black.
A RED parent never has a RED child. in other words: there are never two successive RED nodes in
a path
Every path from the root to an empty subtree contains the same number of BLACK nodes called the black height
We can use black height to measure the balance of a red-black tree.
![Page 5: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/5.jpg)
RED BLACK TREE OPERATIONS
Average
Space O(n)Search
O(log2 n)Traversal
*O(n)Insertion
O(log2 n)
Deletion O(log2 n)
![Page 6: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/6.jpg)
RED BLACK TREES: ROTATION Basic operation for changing tree structure is
called rotation:
![Page 7: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/7.jpg)
RB TREES: ROTATION
x
y
y
x
A lot of pointer manipulation x keeps its left child y keeps its right child x’s right child becomes y’s left child x’s and y’s parents change
A B
C AB C
![Page 8: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/8.jpg)
ROTATION ALGORITHMLEFT-ROTATE(T, x) y ← x->right x->right← y->left y->left->p ← x y->p ← x->p
if x->p = Null then T->root ← y
else if x = x->p->left then x->p->left ← y else x->p->right ← y
y->left ← xx->p ← y
RIGHT-ROTATE(T, x) y ← x->left x->left← y->right y->right->p ← x y->p ← x->p
if x->p = Null then T->root ← y else if x = x->p->right then x->p->right ← y else x->p->left ← y
y->right ← xx->p ← y
Runtime : O(1) for Both.
![Page 9: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/9.jpg)
ROTATION EXAMPLE Rotate left about 9:
12
5 9
7
8
11
![Page 10: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/10.jpg)
ROTATION EXAMPLE Rotate left about 9:
5 12
7
9
118
![Page 11: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/11.jpg)
RED-BLACK TREES: INSERTION Insertion: the basic idea
Insert x into tree, color x red Only r-b property 3 might be violated (if p[x] red)
If so, move violation up tree until a place is found where it can be fixed
Total time will be O(log n)
![Page 12: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/12.jpg)
Insertion AlgorithmTreeNode<T> rbInsert(TreeNode<T> root,TreeNode<T> x)// returns a new root{ root=bstInsert(root,x); // a modification of BST insertItem x.setColor(red); while (x != root and x.getParent().getColor() == red) { if (x.getParent() == x.getParent().getParent().getLeft()) { //parent is left child y = x.getParent().getParent().getRight() //uncle of x if (y.getColor() == red) {// uncle is red x.getParent().setColor(black); y.setColor(black); x.getParent().getParent().setColor(red); x = x.getParent().getParent(); } else { // uncle is black if (x == x.getParent().getRight()) { x = x.getParent(); root = left_rotate(root,x); } x.getParent().setColor(black); x.getParent().getParent().setColor(red); root = right_rotate(root,x.getParent().getParent()); }} } else // ... symmetric to if } // end while root.setColor(black); return root;}
![Page 13: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/13.jpg)
RB INSERT: CASE 1
B
x
● Case 1: “uncle” is red● In figures below, all ’s
are equal-black-height subtrees
C
A D
C
A D
y
new x
Same action whether x is a left or a right child
B
x case 1
![Page 14: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/14.jpg)
RB INSERT: CASE 2
B
x
● Case 2:■ “Uncle” is black■ Node x is a right child
● Transform to case 3 via a left-rotation
CA
CBy
A
x
case 2
y
Transform case 2 into case 3 (x is left child) with a left rotationThis preserves property 4: all downward paths contain same number of black nodes
![Page 15: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/15.jpg)
RB INSERT: CASE 3● Case 3:
■ “Uncle” is black■ Node x is a left child
● Change colors; rotate right
BAx
case 3CB
A
x
y C
Perform some color changes and do a right rotationAgain, preserves property 4: all downward paths contain same number of black nodes
![Page 16: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/16.jpg)
RB INSERT: CASES 4-6 Cases 1-3 hold if x’s parent is a left child If x’s parent is a right child, cases 4-6 are
symmetric (swap left for right)
![Page 17: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/17.jpg)
INSERTION EXAMPLE
Insert 6547
7132
93
![Page 18: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/18.jpg)
INSERTION EXAMPLE
Insert 6547
7132
65 93
![Page 19: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/19.jpg)
INSERTION EXAMPLE
Insert 6547
7132
65 93
Insert 82
![Page 20: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/20.jpg)
INSERTION EXAMPLE
82
Insert 65 47
7132
65 93
Insert 82
![Page 21: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/21.jpg)
INSERTION EXAMPLE
82
Insert 6547
7132
65 93
Insert 82
65
71
93
change nodes’ colors
![Page 22: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/22.jpg)
INSERTION EXAMPLE
9365
71
82
Insert 65
47
32
Insert 82
Insert 87
87
![Page 23: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/23.jpg)
INSERTION EXAMPLE
9365
71
82
Insert 65
47
32
Insert 82
Insert 87
87
![Page 24: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/24.jpg)
INSERTION EXAMPLE
9365
71
87
Insert 65
47
32
Insert 82
Insert 87
82
![Page 25: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/25.jpg)
INSERTION EXAMPLE
9365
87
Insert 65
47
32
Insert 82
Insert 87
82
71
87
93
change nodes’ colors
![Page 26: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/26.jpg)
INSERTION EXAMPLE
87
93
65
Insert 65
47
32Insert 82Insert 87
82
71
![Page 27: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/27.jpg)
RB TREE DELETION ALGORITHMTreeNode<T> rbDelete(TreeNode<T> root,TreeNode<T> z)//return new root, z contains item to be deleted{ TreeNode<T> x,y; // find node y, which is going to be removed if (z.getLeft() == null || z.getRight() == null) y = z; else { y = successor(z); // or predecessor z.setItem(y.getItem); // move data from y to z } // find child x of y if (y.getRight() != null) x = y.getRight(); else x = y.getLeft(); // Note x might be null; create a pretend node if (x == null) { x = new TreeNode<T>(null); x.setColor(black); }
![Page 28: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/28.jpg)
RED-BLACK TREE RETRIEVAL: Retrieving a node from a red-black tree
doesn’t require more than the use of the BST procedure, which takes O(log n) time.
![Page 29: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/29.jpg)
RB TREES EFFICIENCY All operations work in time O(height) and we have proved that heigh is O(log n)
hence, all operations work in time O(log n)! – much more efficient than linked list or arrays implementation of sorted list!
![Page 30: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/30.jpg)
RED BLACK TREE APPLICATION Completely Fair Scheduler in Linux Kernel.
Computational Geometry Data structures.
Red-black trees make less structural changes to balance themselves .
To keep track of the virtual memory segments for a process - the start address of the range serves as the key.
Red–black trees are also particularly valuable in functional programming
To keep track of the virtual memory segments for a process - the start address of the range serves as the key.
![Page 31: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/31.jpg)
COMPARISON BETWEEN AVL AND RB TREE For small data:
insert: RB tree & avl tree has constant number of max rotation but RB tree will be faster because on average RB tree use less rotation.
lookup: AVL tree is faster, because AVL tree has less depth.
delete: RB tree has constant number of max rotation but AVL tree can have O(log N) times of rotation as worst. and on average RB tree also has less number of rotation thus RB tree is faster.
![Page 32: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/32.jpg)
COMPARISON BETWEEN AVL AND RB TREE (CONTINUED) for large data:
insert: AVL tree is faster. because you need to lookup for a particular node before insertion. as you have more data the time difference on looking up the particular node grows proportional to O(log N). but AVL tree & RB tree still only need constant number of rotation at the worst case. Thus the bottle neck will become the time you lookup for that particular node.
lookup: AVL tree is faster. (same as in small data case)
delete: AVL tree is faster on average, but in worst case RB tree is faster. because you also need to lookup for a very deep node to swap before removal (similar to the reason of insertion). on average both trees has constant number of rotation. but RB tree has a constant upper bound for rotation.
![Page 33: Red Black Tree](https://reader036.vdocuments.net/reader036/viewer/2022070511/58a4e30d1a28ab34318b6fe9/html5/thumbnails/33.jpg)
Thank You