0% found this document useful (0 votes)
21 views57 pages

Avl Redblack 22 Aug

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views57 pages

Avl Redblack 22 Aug

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Balance Binary Search Tree

• Worst case height of binary search tree: N-1


– Insertion, deletion can be O(N) in the worst case
• We want a tree with small height
• Height of a binary tree with N node is at least
(log N)
• Goal: keep the height of a binary search tree
O(log N)
• Balanced binary search trees
– Examples: AVL tree, red-black tree
Balanced Tree?
• Suggestion 1: the left and right subtrees of root have the
same height
– Doesn’t force the tree to be shallow
• Suggestion 2: every node must have left and right subtrees
of the same height
– Only complete binary trees satisfy
– Too rigid to be useful
• Our choice: for each node, the height of the left and right
subtrees can differ at most 1
AVL Tree
• An AVL (Adelson-Velskii and Landis 1962) tree
is a binary search tree in which
– for every node in the tree, the height of the left
and right subtrees differ by at most 1.
AVL property
violated here
AVL tree
AVL Tree with
Minimum Number of Nodes

N0 = 1 N1 = 2 N2 =4 N3 = N1+N2+1=7
• Let x be the root of an AVL tree of height h
• S(h) : minimum number of nodes in an AVL
tree of height h
• S(h)=S(h-1)+S(h-2)+1
Smallest AVL tree
of height 7
Smallest AVL tree
of height 8

Smallest AVL tree of height 9


Properties
• The depth of a typical node in an AVL tree is very
close to the optimal log N.
• Consequently, all searching operations in an AVL
tree have logarithmic worst-case bounds.
• An update (insert or remove) in an AVL tree could
destroy the balance. It must then be rebalanced
before the operation can be considered complete.
• After an insertion, only nodes that are on the
path from the insertion point to the root can have
their balances altered.
Rebalancing
• Suppose the node to be rebalanced is X. There are
4 cases that we might have to fix (two are the
mirror images of the other two):
1. An insertion in the left subtree of the left child of X,
2. An insertion in the right subtree of the left child of X,
3. An insertion in the left subtree of the right child of X, or
4. An insertion in the right subtree of the right child of X.
• Balance is restored by tree rotations.
Balancing Operations: Rotations
• Case 1 and case 4 are symmetric and requires
the same operation for balance.
– Cases 1,4 are handled by single rotation.
• Case 2 and case 3 are symmetric and requires
the same operation for balance.
– Cases 2,3 are handled by double rotation.
Single Rotation
• A single rotation switches the roles of the parent and
child while maintaining the search order.
• We rotate between a node and its child.
– Child becomes parent. Parent becomes right child in case
1, left child in case 4.
• The result is a binary search tree that satisfies the
AVL property.
Single rotation to fix case 1: Rotate right
Symmetric single rotation to fix case 4 : Rotate left
Single rotation fixes an AVL tree after insertion of 1.
Single Rotation Example
• Sequentially insert 3, 2, 1, 4, 5, 6 to an AVL Tree
3
3 2 2 2
2
2 1 3 1 3 1 3
Insert 3, 2 1 Single rotation Insert 4
4 Insert 5,
Insert 1 4
violation at node 3
violation at node 3
2 2 5
4
1 4 1 4
2 5
3 5 3 5
1 3 6
Single rotation Insert 6,
violation at node 2 6 Single rotation
Analysis
• One rotation suffices to fix cases 1 and 4.
• Single rotation preserves the original height:
– The new height of the entire subtree is exactly the same as
the height of the original subtree before the insertion.
• Therefore it is enough to do rotation only at the first
node, where imbalance exists, on the path from
inserted node to root.
• Thus the rotation takes O(1) time.
• Hence insertion is O(logN)
Double Rotation
• Single rotation does not fix the cases 2 and 3.
• These cases require a double rotation,
involving three nodes and four subtrees.
Single rotation does not fix case 2.
Left–right double rotation to fix case 2
Lift this up:
first rotate left between (k1,k2),
then rotate right between (k3,k2)
Left-Right Double Rotation
• A left-right double rotation is equivalent to a
sequence of two single rotations:
– 1st rotation on the original tree:
a left rotation between X’s left-child and
grandchild
– 2nd rotation on the new tree:
a right rotation between X and its new left child.
Double rotation fixes AVL tree after the insertion of 5.
Right–Left double rotation to fix case 3.
• If we continue to insert 7, 16, 15, 14, 13, 12, 11, 10, 8, 9

4 4

2 5 2 6

1 3 6 1 3 5 7

Insert 7,
violation at node 5 7 Single rotation

2 6

1 3 5 7

Insert 16, fine 16


Insert 15
violation at node 7 15
4 4

2 6 2 6

1 3 5 7 k1 1 3 5 15 k2

Insert 16, fine 16 k3 7 16


Insert 15 Double rotation
violation at node 7 k1 k3
15 k2
4 4

2 6 k1 2 7 k2
A

1 3 5 15 k3 1 3 6 k1 15 k3

k2 7 16 D 5 14 16

Insert 14 14 C Double rotation

4 k1 7
X 2 7 k2
4 15

1 3 6 15 2 6 14 16

Insert 13 5 14 16
1 3 5 13
Y Z Single rotation
13
7 7
4 15 4 15

2 6 14 16 2 6 13 16

1 3 5 13 1 3 5 12 14

Insert 12 12 Single rotation

7
7
4 13
4 15

2 6 12 15
2 6 13 16

1 3 5 11 14 16
1 3 5 12 14

Insert 11 Single rotation


11
7 7

4 13 4 13

2 6 12 15 2 6 11 15

1 3 5 11 14 16 1 3 5 10 12 14 16

Insert 10 10 Single rotation

7
7
4 13
4 13
2 6 11 15
2 6 11 15
1 3 5 9 12 14 16
1 3 5 10 12 14 16
8 10
8 Insert 8, fine
9 then insert 9 double rotation
AVL Trees - Implementation
struct node
{
int data;
struct node * llink;
struct node * rlink;
int height;
};

Typedef struct node * NODE;

Note:height of null tree is -1


Compute height of an AVL node
int height(NODE P)
{
if( P == NULL)
return -1;
else
return P->height;
}
Insertion into an AVL tree
NODE insert(int x, NODE root)
{
if( root == NULL)
{
root = (NODE) malloc(sizeOf(struct node));
root->data = x;
root->height = 0;
root->llink = root->rlink = NULL;
}
else if(x < root->data)
{
root->llink = insert(x, root->llink);
if(height(root->llink) – height(root->rlink) == 2)
if(x < root->llink->data)
root=singleRotateWithLeft(root);
else
root = doubleRotateWithLeft(root);
}
Insertion into an AVL tree
else if(x > root->data)
{
root->rlink = insert(x, root->rlink);
if(height(root->rlink) – height(root->llink) == 2)
if(x > root->rlink->data)
root=singleRotateWithRight(root);
else
root = doubleRotateWithRight(root);
}
root->height = max(height(root->llink), height(root->rlink))+1;
return root;
}
Single Rotation
NODE singleRotateWithLeft(NODE k2)
{
NODE k1;
k1 = k2->llink;
k2->llink = k1->rlink;
k1->rlink = k2;
k2->height = Max(height(k2->llink, height(k2->rlink))+1;
k1->height = Max(height(k1->llink), k2->height)+1;
return k1;
}
Double Rotation
NODE doubleRotateWithLeft(NODE k3)
{
//rotate between k1 & k2
k3->llink = singleRotateWithRight(k3->llink);

//rotate between k3 & k2


return singleRotateWithLeft(k3);
}
Red-Black Trees
• invented in 1972 by Rudolf Bayer
• A red-black tree is a kind of self-balancing binary
search tree where each node has an extra bit,
and that bit is often interpreted as the color (red
or black).
• These colors are used to ensure that the tree
remains balanced during insertions and
deletions.
• balance of the tree is not perfect
• reduces the searching time and maintain it
around O(log n) time
AVL vs. Red-black
• AVL trees are more balanced compared to Red-
Black Trees
• AVL - more rotations during insertion and
deletion compare to red-black
• if application involves frequent insertions and
deletions, then Red-Black trees should be
preferred
• if the insertions and deletions are less frequent
and search is a more frequent operation, then
AVL tree should be preferred over the Red-Black
Tree.
Red-Black Trees
A RED-BLACK TREE IS A BINARY SEARCH TREE
IN WHICH THE ROOT ITEM IS COLORED
BLACK, EVERY OTHER ITEM IS COLORED
RED OR BLACK, AND

1. (RED RULE) A RED ITEM CANNOT HAVE


ANY RED CHILDREN;

2. (PATH RULE) THE NUMBER OF BLACK


ITEMS IS THE SAME IN ANY PATH FROM
THE ROOT ITEM TO AN ITEM WITH
NO CHILDREN OR WITH ONE CHILD.
Red Black Tree Rules
1. Every node is colored either Red or
black
2. The root is black
3. If a node is red its children must be
black. (the red rule)
4. Every path from a node to a null link
must contain the same number of
black nodes (path rule)
Red Black Trees 36
Red-Black Tree
• A red-black tree can also be defined as a binary search tree that
satisfies the following properties:
– Root Property: the root is black
– External Property: every leaf is black
– Internal Property: the children of a red node are black
– Depth Property: all the leaves have the same black depth

4 15

2 6 12 21

7
Path rule Path rule Red rule
Example of a red-black tree

Search 11
60

30 80

20 50 70 90

ADD 40?
60

30 80

20 50 70 90

40
VIOLATES RED RULE,
NEED TO FLIP SOME COLORS (50)
60

30 80

20 50 70 90

40
1. Perform standard BST insertion and make the colour of
newly inserted nodes as RED.
2. If x is the root, change the colour of x as BLACK (Black
height of complete tree increases by 1).
3. Do the following if the color of x’s parent is not BLACK and x
is not the root.
a) If x’s uncle is RED
(i) Change the colour of parent and uncle as BLACK.
(ii) Colour of a grandparent as RED.
(iii) Change x = x’s grandparent, repeat steps 2 and 3 for
new x.
b) If x’s uncle is BLACK, then there can be four configurations
for x, x’s parent (p) and x’s grandparent (g) (similar to AVL)
(i) Left Left Case (p is left child of g and x is left child of p)
(ii) Left Right Case (p is left child of g and x is the right
child of p)
(iii) Right Right Case (Mirror of case i)
(iv) Right Left Case (Mirror of case ii)
4. Re-coloring after rotations:
• For Left Left Case [3.b (i)] and Right Right case [3.b (iii)],
swap colors of grandparent and parent after rotations
• For Left Right Case [3.b (ii)]and Right Left Case [3.b (iv)],
swap colors of grandparent and inserted node after
rotations
•Left Left Case (LL rotation):
•Left Right Case (LR rotation):
•Right Right Case (RR rotation):
•Right Left Case (RL rotation):
Analysis
• Go up the tree performing Case 1, which only
recolors nodes.
• If Case 2 or Case 3 occurs, perform 1 or 2
rotations, and terminate.
Running time: O(lg n) with O(1) rotations.
Applications:
1.Most of the self-balancing BST library functions like
map, multiset, and multimap in C++ ( or java
packages like java.util.TreeMap and java.util.TreeSet )
use Red-Black Trees.

2. It is used to implement CPU Scheduling Linux.

3. It is also used in the K-mean clustering algorithm in


machine learning for reducing time complexity.

4. Moreover, MySQL also uses the Red-Black tree for


indexes on tables in order to reduce the searching and
insertion time.
Insertion into a red-black tree

Example:
Insertion into a red-black tree Example:
• Insert x =15.
• Recolor, moving the
violation up the tree.
Insertion into a red-black tree
Create a red-black tree for the following data:

3,2,5,6,10,4,7,8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy