DSA LAB Presetation
DSA LAB Presetation
AVL Tree
&
RED-BLACK
Tree
By
Farhan Arif DSAI231102002
Ayesha Anwar DSAI231102019
Atfa Fatima DSAI231102040
Maaz Ahmad DSAI231102047
Balanced Tree
Node based binary search tree
Automatically balance it’s height in the face of arbitrary item
insertions and deletions
Features :
A special kind of binary search tree
Self balancing tree
Height of right sub tree ˞˞ height of left sub tree ≤ 1
Examples
AVL Tree or not
YES
NO
Each left sub-tree has
Left sub-tree has height 3,
height 1 greater than
but right sub-tree has height 1
each right sub-tree
Operations
Insertion
Deletion
Traversal
Searching
Rotation for Balancing
Insertion
It is performed as in binary search trees.
For insertions, one rotation is sufficient.
Sometimes it needs two rotations.
Insertion
Elements : Insert 1
1 2 3 6 15 -2 -5 -8 1 (0,0)
Insertion
Elements : Insert 2
1 2 3 6 15 -2 -5 -8 1 (0,1)
2 (0,0)
Insertion
Elements : Insert 3
1 2 3 6 15 -2 -5 -8 1 (0,2)
3 (0,0)
Insertion
Elements : Insert 3
1 2 3 6 15 -2 -5 -8 2 (1,1)
1 (0,0) 3 (0,0)
Insertion
Elements : Insert 6
1 2 3 6 15 -2 -5 -8 2 (1,2)
1 (0,0) 3 (0,1)
6 (0,0)
Insertion
Elements : Insert 15
1 2 3 6 15 -2 -5 -8 2 (1,3)
1 (0,0) 3 (0,2)
15 (0,0)
Insertion
Elements : Insert 15
1 2 3 6 15 -2 -5 -8 2 (1,2)
1 (0,0) 6 (1,1)
3 (0,0) 15 (0,0)
Insertion
Elements : Insert -2
1 2 3 6 15 -2 -5 -8 2 (2,2)
1 (1,0) 6 (1,1)
1 (2,0) 6 (1,1)
-5 (0,0)
Insertion
Elements : Insert -5
1 2 3 6 15 -2 -5 -8 2 (2,2)
-2 (1,1) 6 (1,1)
-5 (0,0) 1 3
(0,0) 15 (0,0)
(0,0) (0,0)
Insertion
Elements : Insert -8
1 2 3 6 15 -2 -5 -8 2 (3,2)
-2 (2,1) 6 (1,1)
-5 (1,0) 1 3
(0,0) 15 (0,0)
(0,0) (0,0)
-8 (0,0)
Deletion
Deletion can make the tree unbalanced
One rotation is needed for rebalancing
Sometimes it needs two rotations
Deletion
Traversal
Maintains preorder, inorder and postorder traversal
Depends on the height of the tree
Traversal
(Algorithms)
Preorder Traversal
void preorder(node *t) {
if (t != NULL) {
printf(“%d ”, t->element);
preorder(t->leftChild);
preorder(t->rightChild);
}
}
Traversal (Algorithms)
Inorder Traversal
void inorder(node *t) {
if (t != NULL) {
inorder(t->leftChild);
printf(“%d ”, t->element);
inorder(t->rightChild);
}
}
Traversal (Algorithms)
Postorder Traversal
void postorder(node *t) {
if (t != NULL) {
postorder(t->leftChild); /* L */
postorder(t->rightChild); /* R */
printf(“%d ”, t->element); /* V */
}
}
Searching
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.
AVL Tree
Applications of AVL
Tree
Used in many search applications where data is constantly
entering/leaving.
To security concerns and to parallel code.
Creating new types of data structures.
Red-Black Tree
Introduction
• A balancing binary search tree.
Space O(n)
Search
O(log2 n)
Traversal O(n)
Insertion
O(log2 n)
Deletion
O(log2 n)
Red-Black Trees: Rotation
• Basic operation for changing tree structure is
called rotation:
Red-Black Trees: Rotation
y x
x C A y
A B B C
5 9
8 12
11
Rotation Example
• Rotate left about 9:
5 12
8 11
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)
Red-Black Insertion: Case 1
C C new x
A D y A D
x B
case 1
x B
Same action whether x is a left or a right child
C case 2
C
A y B y
B x A x
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
Red-Black Insertion: Case 3
● Case 3:
■ “Uncle” is black
■ Node x is a left child
● Change colors; rotate right
C case 3
B
B y x A C
A x
Perform some color changes and do a right rotation
Again, preserves property 4: all downward paths contain same number of black nodes
Red-Black Insert: Cases 4-6
• Cases 1-3 hold if x’s parent is a left child
32 71
93
Insertion Example
Insert 65
47
32 71
65 93
Insertion Example
Insert 65
Insert 82 47
32 71
65 93
Insertion Example
Insert 65 47
Insert 82
32 71
65 93
82
Insertion Example
Insert 65
Insert 82 47
32 71
65 93
82
Insertion Example
47
32 71
Insert 65
Insert 82 65 93
Insert 87
82
87
Insertion Example
47
32 71
Insert 65
Insert 82
65 93
Insert 87
82
87
Insertion Example
47
32 71
Insert 65
Insert 82 65 93
Insert 87
87
82
Insertion Example
47
32 71
Insert 65
Insert 82
65 93
Insert 87
change nodes’ colors
87
82
Insertion Example
47
Insert 65 32 71
Insert 82
Insert 87
65 87
82 93
Red-Black Tree Deletion
• 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.
Deletion
Red-black tree Searching:
• 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.
Red-Black Trees efficiency
All operations work in time O(height)
hence, all operations work in time O(log n)! – much
• To keep track of the virtual memory segments for a process - the start address
of the range serves as the key.