PPT 2.5 AVL Trees is a ppt from trees topic dsa.ppt
1. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
DISCOVER . LEARN . EMPOWER
UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE
ENGINEERING
Bachelor of Engineering
Design and Analysis of
Algorithms(CSH-311/ITH-311)
Trees ( (AVL, Red black trees)) (CO2)
Topic: Trees
2. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Learning Objectives & Outcomes
Objective:
• To understand the meaning of Tree
• To Study its Hierarchical Structure .
• To analyze its Parent and Child node structure.
Outcome:
• Student will understand
Tree and operations performed on tree
Different Tree Types
3. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
AVL Tree
• AVL tree is a self-balancing Binary Search Tree (BST) where
the difference between heights of left and right subtrees
cannot be more than one for all nodes.
• Why AVL Trees?
Most of the BST operations (e.g., search, max, min, insert,
delete.. etc) take O(h) time where h is the height of the BST.
The cost of these operations may become O(n) for a skewed
Binary tree. If we make sure that height of the tree remains
O(Logn) after every insertion and deletion, then we can
guarantee an upper bound of O(Logn) for all these
operations
4. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Example
The above tree is AVL because differences between heights of
left and right subtrees for every node is less than or equal to 1.
5. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• AVL tree is a self-balancing Binary Search Tree (BST) where
the difference between heights of left and right subtrees
cannot be more than one for all nodes.
• What if the input to binary search tree comes in a sorted
(ascending or descending) manner? It will then look like
this −
•
6. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• It is observed that BST's worst-case performance is closest
to linear search algorithms, that is (n). In real-time data,
Ο
we cannot predict data pattern and their frequencies. So, a
need arises to balance out the existing BST.
• Named after their inventor Adelson, Velski & Landis, AVL
trees are height balancing binary search tree. AVL tree
checks the height of the left and the right sub-trees and
assures that the difference is not more than 1. This
difference is called the Balance Factor.
7. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• Here we see that the first tree is balanced and the next two trees are not balanced
• In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so
the difference is 2. In the third tree, the right subtree of A has height 2 and the left is
missing, so it is 0, and the difference is 2 again. AVL tree permits difference (balance
factor) to be only 1.
• BalanceFactor = height(left-sutree) − height(right-sutree)
• If the difference in the height of left and right sub-trees is more than 1, the tree is balanced
using some rotation techniques.
8. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• AVL Rotations
• To balance itself, an AVL tree may perform the following
four kinds of rotations
Left rotation
Right rotation
Left-Right rotation
Right-Left rotation
9. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• Left Rotation
• If a tree becomes unbalanced, when a node is inserted into
the right subtree of the right subtree, then we perform a
single left rotation −
• In our example, node A has become unbalanced as a node is
inserted in the right subtree of A's right subtree. We perform
the left rotation by making A the left-subtree of B.
10. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• Right Rotation
• AVL tree may become unbalanced, if a node is inserted in
the left subtree of the left subtree. The tree then needs a
right rotation.
• As depicted, the unbalanced node becomes the right child of
its left child by performing a right rotation.
11. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
12. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• Right-Left Rotation
• The second type of double rotation is Right-Left Rotation. It is a combination of
right rotation followed by left rotation.
13. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
14. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• Left-Right Rotation
• Double rotations are slightly complex version of already explained
versions of rotations. To understand them better, we should take note of
each action performed while rotation. Let's first check how to perform
Left-Right rotation. A left-right rotation is a combination of left rotation
followed by right rotation.
15. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
B Tree
• All leaves are at the same level.
• A B-Tree is defined by the term minimum degree ‘t’. The value of t
depends upon disk block size.
• Every node except root must contain at least t-1 keys. The root
may contain minimum 1 key.
• All nodes (including root) may contain at most 2t – 1 keys.
• Number of children of a node is equal to the number of keys in it
plus 1.
• All keys of a node are sorted in increasing order. The child
between two keys k1 and k2 contains all keys in the range from k1
and k2.
• B-Tree grows and shrinks from the root which is unlike Binary
Search Tree. Binary Search Trees grow downward and also shrink
from downward.
16. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Red black tree
• Red-Black Tree is a self-balancing Binary Search Tree (BST)
where every node follows following rules:
1) Every node has a color either red or black.
2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot
have a red parent or red child).
4) Every path from a node (including root) to any of its
descendant NULL node has the same number of black
nodes.
17. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Most of the BST operations (e.g., search, max, min, insert,
delete.. etc) take O(h) time where h is the height of the BST.
The cost of these operations may become O(n) for a skewed
Binary tree. If we make sure that height of the tree remains
O(Logn) after every insertion and deletion, then we can
guarantee an upper bound of O(Logn) for all these operations.
The height of a Red-Black tree is always O(Logn) where n is
the number of nodes in the tree.
Why Red black tree?
18. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
REFERENCES
Text books:
•Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd
edition 2012. problem, Graph coloring.
•Horowitz, Sahni and Rajasekaran, “Fundamentals of ComputerAlgorithms”,
University Press (India), 2nd
edition
Websites:
1.https://www.tutorialride.com/data-structures/trees-in-data-structure.htm
2.https://www.geeksforgeeks.org/avl-tree-set-1-insertion/
19. University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Summary
Introduction to Trees
• Basic terms
Types of Trees
• Binary tree
• B tree
• Balanced trees