SlideShare a Scribd company logo
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
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
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
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.
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 −
•
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.
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.
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
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.
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.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
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.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
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.
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.
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.
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?
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/
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
University Institute of Engineering (UIE)
THANK YOU

More Related Content

PDF
Lecture 10 - AVL Trees.pdf
PPTX
Lec 08 - Balanced BSTs and AVL Tree.pptx
PPTX
AVL Tree Data Structure
PPT
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
PPT
Data Structures 5
PDF
Lect 13, 14 (final)AVL Tree and Rotations.pdf
PPT
358 33 powerpoint-slides_10-trees_chapter-10
PPTX
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
Lecture 10 - AVL Trees.pdf
Lec 08 - Balanced BSTs and AVL Tree.pptx
AVL Tree Data Structure
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
Data Structures 5
Lect 13, 14 (final)AVL Tree and Rotations.pdf
358 33 powerpoint-slides_10-trees_chapter-10
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS

Similar to PPT 2.5 AVL Trees is a ppt from trees topic dsa.ppt (20)

PPT
16-avl-trees.ppt data structures prestentation
PPTX
Splay tree
PDF
Study about AVL Tree & Operations
PPTX
From_Binary_Trees_to_AVL_Trees_Presentation.pptx
PPTX
Design data Analysis Avl Trees.pptx by piyush sir
PPTX
AVL Tree.pptx
PPT
PPT 2.7 Heaps is a topic from dsa trees.ppt
PPT
lecture18(1) for the data structure .ppt
PPTX
Binary trees1
PPTX
Bsc cs ii dfs u-3 tree and graph
PDF
Problems in parallel computations of tree functions
PPT
PPT 2.1 Array.pptDFHFHGHCVNGVVGFHFHDFHDFHDFHDFH
PPTX
Presentation_30219_Content_Document_20250107125144AM.pptx
PDF
[Queue , linked list , tree]
PPTX
AVL tree PPT.pptx
PPTX
Bca ii dfs u-3 tree and graph
PPT
Data Structures 4
PDF
Avl tree detailed
16-avl-trees.ppt data structures prestentation
Splay tree
Study about AVL Tree & Operations
From_Binary_Trees_to_AVL_Trees_Presentation.pptx
Design data Analysis Avl Trees.pptx by piyush sir
AVL Tree.pptx
PPT 2.7 Heaps is a topic from dsa trees.ppt
lecture18(1) for the data structure .ppt
Binary trees1
Bsc cs ii dfs u-3 tree and graph
Problems in parallel computations of tree functions
PPT 2.1 Array.pptDFHFHGHCVNGVVGFHFHDFHDFHDFHDFH
Presentation_30219_Content_Document_20250107125144AM.pptx
[Queue , linked list , tree]
AVL tree PPT.pptx
Bca ii dfs u-3 tree and graph
Data Structures 4
Avl tree detailed
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
01-Introduction-to-Information-Management.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Microbial diseases, their pathogenesis and prophylaxis
01-Introduction-to-Information-Management.pdf
Pre independence Education in Inndia.pdf
Classroom Observation Tools for Teachers
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
PPH.pptx obstetrics and gynecology in nursing
Anesthesia in Laparoscopic Surgery in India
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Insiders guide to clinical Medicine.pdf
Cell Types and Its function , kingdom of life
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Ad

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
  • 20. University Institute of Engineering (UIE) THANK YOU