2. Node based binary search tree
Automatically balance it’s height in the face of arbitrary item
insertions and deletions
Fig : Balanced Tree
3.
4. A special kind of binary search tree
Self balancing tree
Height of right sub tree ˞˞ height of left sub tree ≤ 1
Georgy Adelson-Velsky and Evgenii Landis' tree
Named after the inventors (1962)
5.
6. YES
Each left sub-tree has
height 1 greater than
each right sub-tree
NO
Left sub-tree has height 3,
but right sub-tree has height 1
27. Postorder Traversal
void postorder(node *t) {
if (t != NULL) {
postorder(t->leftChild); /* L */
postorder(t->rightChild); /* R */
printf(“%d ”, t->element); /* V */
}
}
28. Similar to normal unbalanced binary search tree.
Successful searches are limited by the height of the tree.
Unsuccessful searching time is very close to the height of the
tree.
29.
30. Used in many search applications where data is constantly
entering/leaving.
To security concerns and to parallel code.
Creating new types of data structures.
31.
32. • 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.
33.
34. • 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.
40. LEFT-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 ← x
x->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 ← x
x->p ← y
Runtime : O(1) for Both.
41. • 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)
42. 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
43. B
x
● Case 2:
■ “Uncle” is black
■ Node x is a right child
● Transform to case 3 via a left-rotation
C
A
C
By
A
x
case 2
y
Transform case 2 into case 3 (x is left child) with a left rotation
This preserves property 4: all downward paths contain same number of black nodes
44. ● Case 3:
■ “Uncle” is black
■ Node x is a left child
● Change colors; rotate right
B
Ax
case 3
C
B
A
x
y C
Perform some color changes and do a right rotation
Again, preserves property 4: all downward paths contain same number of black nodes
45. • 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)
56. TreeNode<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;
}
57. • If n has no children, we only have to remove n from the tree.
• If n has a single child, we remove n and connect its parent to its child.
• If n has two children, we need to :
• Find the smallest node that is larger than n, call it m.
• Remove m from the tree and Replace the value of n with m.
• Then restores the red-black tree properties.
58. TreeNode<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);
}
59. • Searching a node from a red-black tree doesn’t require more than the
use of the BST procedure, which takes O(log n) time.
60. All operations work in time O(height)
hence, all operations work in time O(log n)! – much
more efficient than linked list or arrays implementation
of sorted list!
61. • Completely Fair Scheduler in Linux Kernel.
• Computational Geometry Data structures.
• 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.
62. For small data :
• Insert: RB tree will be faster because on average it uses less rotation.
• Lookup: AVL tree is faster, because it has less depth.
• Delete: RB tree is faster for it’s runtime.
For large data :
• Insert: AVL tree is faster, because it maintains O(log n) which is better than RB
tree.
• 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.