SlideShare a Scribd company logo
1 of 19
AVL Trees
                                   6
                   v
                       3               8
                               z
                           4




       AVL Trees                           1
AVL Tree Definition
AVL trees are                                      4
balanced.                                     44
                            2
An AVL Tree is a
                                                                                   3
                                17                                            78
binary search tree                        1                 2                                   1
such that for every
                                     32                         50                         88
                                                   1
internal node v of T,                                  48            62
                                                                          1

the heights of the
children of v can differ
by at most 1.              An example of an AVL tree where the
                           heights are shown next to the nodes:

                       AVL Trees                                                       2
 
AVL Trees:
       AVL trees were invented in 1962 by two Russian
      scientist G.M Adelson Velsky & E.M Landis.
  
Definition:
       An AVL tree is a binary search tree in which the balance
factor of every node, which is defined as the difference b/w the
heights of the node’s left & right sub trees is either   0 or +1
or   -1 .
 Balance factor = ht of left sub tree – ht of right sub tree.
ht=height
Example :

            AVL tree                       BST ( not AVL tree )
                  1                                    2
                10                                    10


     0      5            1        20               05                020

 1   4      -1                0        1                -1
                 7                             4         7
                             12
     0                   0                 0                 0
 2                                                               8
                                           2
                     8
 



      If an insertion of a new node makes an AVL tree
unbalanced , we transform the tree by a rotation
 

         Rotation :
          A Rotation in an AVL tree is a local transformation of
its sub tree rooted at a node whose balance has become either
+2 or -2 ,if there are several such nodes, we rotate the tree
rooted at the unbalanced node that is the closest to the newly
inserted leaf.
 
Types of rotations :

       Totally there are four types of rotations

1.            Single right rotation or R-rotation

2.            Single left rotation or L-rotation

3.            Double right-left rotation or RL-rotation

4.            Double left-right rotation or LR-rotation
 
Points to remember to select different rotation technique :

1.Straight line with positive unbalanced.
              apply Right rotation for unbalanced node.
              +2
                5                            04

    +1    4                        0             0
                                            2        5

  0
      2
 

2.Straight line with negative unbalanced.
               apply left rotation for unbalanced node.

      -2
           5
                                                 06

                6 -1                     0            0
                                             5            7

                       70
 

3.Curved line with positive unbalanced.
             apply left-right rotation.

       Right rotation for unbalanced node and left rotation
for the nearest node.

                  +2
             5                                   0
                                                 4
        4   -1
                                           0 2        5   0
              2        0
 

4.Curved line with negative unbalanced.
                  apply right-left rotation.

       Left rotation for unbalanced node and right rotation
for the nearest node.

                  -2
              5                                      0
                                                      7
              +1       8
                                               0 5        8 0
      0   7
n(2)   3

                                                                      n(1)

Height of an AVL Tree
                                                                 4



 Fact: The height of an AVL tree storing n keys is O(log
 n).
 Proof: Let us bound n(h): the minimum number of internal
 nodes of an AVL tree of height h.
 We easily see that n(1) = 1 and n(2) = 2
 For n > 2, an AVL tree of height h contains the root node,
 one AVL subtree of height n-1 and another of height n-2.
 That is, n(h) = 1 + n(h-1) + n(h-2)
 Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So
 n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction),
 n(h) > 2in(h-2i)
 Solving the base case we get: n(h) > 2 h/2-1
 Taking logarithms: h < 2log n(h) +2
 Thus the height of an AVL Trees is O(log n)
                         AVL tree                                    11
Insertion in an AVL Tree
 Insertion is as in a binary search tree
 Always done by expanding an external node.
 Example:       44                       44

                                                                                      c=z
        17                       78               17                            78
                                                                a=y

             32             50          88             32             50             88


                       48        62                              48             62
                                                                                     b=x


                                                                           54
                                                                 w


                  before insertion                          after insertion

                                      AVL Trees                                             12
Trinode Restructuring
           let (a,b,c) be an inorder listing of x, y, z
           perform the rotations needed to make b the topmost node of the
           three
                                   (other two cases
                                                                             a=z                  case 2: double rotation
            a=z                    are symmetrical)
                                                                                                  (a right rotation about c,
                                                                                       c=y        then a left rotation about a)
                  b=y
                                                                       T0
      T0
                        c=x                                                      b=x
                                                                                             T3
            T1                                       b=y                                                           b=x
                                                                            T1         T2
                   T2         T3
                                          a=z                   c=x                                     a=z                   c=y


case 1: single rotation
(a left rotation about a)            T0         T1         T2         T3                           T0         T1         T2         T3

                                                           AVL Trees                                                      13
Insertion Example, continued
                             5
                        44
      2
                                                                  z       64
          17                                                          78         7
                                      3
                                          2y                                         1
                    1
               32                1        50                4                   88
                             1
                                                        2        x
                                 48
                                               1
                                                   3        62
                                                                 5
                                                   54                           T3
unbalanced...                                                     T2
                                 T0
                                                                                4
                                          T1                               44                              4
                                                   2                                                   3        x
                                                       17
                                                                                            2 y            62
                                                                                                                     z6
                                                                      1                   2                               2
                                                                 32
                                                                                1
                                                                                     1     50     3             5
                                                                                                                     78        7
                                                                                                       1                            1
                    ...balanced                                                      48           54                           88

                                                                                                                    T2

                                                                                     T0           T1                           T3
                                                   AVL Trees                                                              14
Restructuring
(as Single Rotations)
 Single Rotations:


            a=z                              single rotation               b=y
                       b=y                                      a=z                   c=x
                                  c=x

       T0                                                                                   T3
                  T1                    T3                 T0         T1         T2
                             T2



                             c=z        single rotation               b=y
                  b=y                                      a=x                   c=z
       a=x

                                   T3                 T3
  T0                    T2                                       T2         T1         T0
             T1
                                             AVL Trees                                           15
Restructuring
(as Double Rotations)
 double rotations:


            a=z                               double rotation               b=x
                                  c=y                            a=z                   c=y
                       b=x

       T0                                                                         T2
                             T2          T3                 T0         T1                     T3
                  T1


                              c=z         double rotation              b=x
        a=y                                                 a=y                    c=z
                   b=x

                                    T0                            T2
  T3         T2                                        T3                    T1          T0
                        T1
                                               AVL Trees                                           16
Removal in an AVL Tree
  Removal begins as in a binary search tree, which
  means the node removed will become an empty
  external node. Its parent, w, may cause an imbalance.
  Example:      44                     44


         17                           62               17                  62


              32            50               78                  50             78


                       48        54               88        48        54             88




                   before deletion of 32                    after deletion

                                           AVL Trees                                      17
Rebalancing after a Removal
  Let z be the first unbalanced node encountered while travelling
  up the tree from w. Also, let y be the child of z with the larger
  height, and let x be the child of y with the larger height.
  We perform restructure(x) to restore balance at z.
  As this restructuring may upset the balance of another node
  higher in the tree, we must continue checking for balance until
  the root of T is reached


                                                                      62
      a=z     44

                                                               44              78
  w     17                   62         b=y

                                                          17         50             88
                   50             78          c=x


                                                                48        54
             48         54             88


                                              AVL Trees                                  18
Running Times for
AVL Trees
 a single restructure is O(1)
    using a linked-structure binary tree
 find is O(log n)
    height of tree is O(log n), no restructures needed
 insert is O(log n)
    initial find is O(log n)
    Restructuring up the tree, maintaining heights is O(log n)
 remove is O(log n)
    initial find is O(log n)
    Restructuring up the tree, maintaining heights is O(log n)




                             AVL Trees                            19

More Related Content

What's hot

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureYaksh Jethva
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structureschauhankapil
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Treekhabbab_h
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data StructureMeghaj Mallick
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 

What's hot (20)

Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Hash table
Hash tableHash table
Hash table
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
b+ tree
b+ treeb+ tree
b+ tree
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Merge sort
Merge sortMerge sort
Merge sort
 
Red black tree
Red black treeRed black tree
Red black tree
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
Sorting
SortingSorting
Sorting
 
1.7 avl tree
1.7 avl tree 1.7 avl tree
1.7 avl tree
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 

Viewers also liked (20)

Avl trees
Avl treesAvl trees
Avl trees
 
Avl trees
Avl treesAvl trees
Avl trees
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Avl tree
Avl treeAvl tree
Avl tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Data Structures : AVL Trees
Data Structures : AVL TreesData Structures : AVL Trees
Data Structures : AVL Trees
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Advance data structure
Advance data structureAdvance data structure
Advance data structure
 
B tree short
B tree shortB tree short
B tree short
 
Digital Search Tree
Digital Search TreeDigital Search Tree
Digital Search Tree
 
Application of tries
Application of triesApplication of tries
Application of tries
 
Trie Data Structure
Trie Data StructureTrie Data Structure
Trie Data Structure
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data Structures
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Memory Mapping Cache
Memory Mapping CacheMemory Mapping Cache
Memory Mapping Cache
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Sorting
SortingSorting
Sorting
 
Application Letter ( Block Style )
Application Letter ( Block Style )Application Letter ( Block Style )
Application Letter ( Block Style )
 

Similar to AVL Trees: Self-Balancing Binary Search Trees

Similar to AVL Trees: Self-Balancing Binary Search Trees (20)

Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
Lec11
Lec11Lec11
Lec11
 
Avl trees
Avl treesAvl trees
Avl trees
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
4. avl
4. avl4. avl
4. avl
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
Avltrees
AvltreesAvltrees
Avltrees
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Avl tree
Avl treeAvl tree
Avl tree
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
 
Lecture3
Lecture3Lecture3
Lecture3
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 

Recently uploaded

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

AVL Trees: Self-Balancing Binary Search Trees

  • 1. AVL Trees 6 v 3 8 z 4 AVL Trees 1
  • 2. AVL Tree Definition AVL trees are 4 balanced. 44 2 An AVL Tree is a 3 17 78 binary search tree 1 2 1 such that for every 32 50 88 1 internal node v of T, 48 62 1 the heights of the children of v can differ by at most 1. An example of an AVL tree where the heights are shown next to the nodes: AVL Trees 2
  • 3.   AVL Trees: AVL trees were invented in 1962 by two Russian scientist G.M Adelson Velsky & E.M Landis.    Definition: An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference b/w the heights of the node’s left & right sub trees is either 0 or +1 or -1 . Balance factor = ht of left sub tree – ht of right sub tree. ht=height
  • 4. Example : AVL tree BST ( not AVL tree ) 1 2 10 10 0 5 1 20 05 020 1 4 -1 0 1 -1 7 4 7 12 0 0 0 0 2 8 2 8
  • 5.   If an insertion of a new node makes an AVL tree unbalanced , we transform the tree by a rotation   Rotation : A Rotation in an AVL tree is a local transformation of its sub tree rooted at a node whose balance has become either +2 or -2 ,if there are several such nodes, we rotate the tree rooted at the unbalanced node that is the closest to the newly inserted leaf.
  • 6.   Types of rotations : Totally there are four types of rotations 1. Single right rotation or R-rotation 2. Single left rotation or L-rotation 3. Double right-left rotation or RL-rotation 4. Double left-right rotation or LR-rotation
  • 7.   Points to remember to select different rotation technique : 1.Straight line with positive unbalanced. apply Right rotation for unbalanced node. +2 5 04 +1 4 0 0 2 5 0 2
  • 8.   2.Straight line with negative unbalanced. apply left rotation for unbalanced node. -2 5 06 6 -1 0 0 5 7 70
  • 9.   3.Curved line with positive unbalanced. apply left-right rotation. Right rotation for unbalanced node and left rotation for the nearest node. +2 5 0 4 4 -1 0 2 5 0 2 0
  • 10.   4.Curved line with negative unbalanced. apply right-left rotation. Left rotation for unbalanced node and right rotation for the nearest node. -2 5 0 7 +1 8 0 5 8 0 0 7
  • 11. n(2) 3 n(1) Height of an AVL Tree 4 Fact: The height of an AVL tree storing n keys is O(log n). Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. We easily see that n(1) = 1 and n(2) = 2 For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2in(h-2i) Solving the base case we get: n(h) > 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL Trees is O(log n) AVL tree 11
  • 12. Insertion in an AVL Tree Insertion is as in a binary search tree Always done by expanding an external node. Example: 44 44 c=z 17 78 17 78 a=y 32 50 88 32 50 88 48 62 48 62 b=x 54 w before insertion after insertion AVL Trees 12
  • 13. Trinode Restructuring let (a,b,c) be an inorder listing of x, y, z perform the rotations needed to make b the topmost node of the three (other two cases a=z case 2: double rotation a=z are symmetrical) (a right rotation about c, c=y then a left rotation about a) b=y T0 T0 c=x b=x T3 T1 b=y b=x T1 T2 T2 T3 a=z c=x a=z c=y case 1: single rotation (a left rotation about a) T0 T1 T2 T3 T0 T1 T2 T3 AVL Trees 13
  • 14. Insertion Example, continued 5 44 2 z 64 17 78 7 3 2y 1 1 32 1 50 4 88 1 2 x 48 1 3 62 5 54 T3 unbalanced... T2 T0 4 T1 44 4 2 3 x 17 2 y 62 z6 1 2 2 32 1 1 50 3 5 78 7 1 1 ...balanced 48 54 88 T2 T0 T1 T3 AVL Trees 14
  • 15. Restructuring (as Single Rotations) Single Rotations: a=z single rotation b=y b=y a=z c=x c=x T0 T3 T1 T3 T0 T1 T2 T2 c=z single rotation b=y b=y a=x c=z a=x T3 T3 T0 T2 T2 T1 T0 T1 AVL Trees 15
  • 16. Restructuring (as Double Rotations) double rotations: a=z double rotation b=x c=y a=z c=y b=x T0 T2 T2 T3 T0 T1 T3 T1 c=z double rotation b=x a=y a=y c=z b=x T0 T2 T3 T2 T3 T1 T0 T1 AVL Trees 16
  • 17. Removal in an AVL Tree Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance. Example: 44 44 17 62 17 62 32 50 78 50 78 48 54 88 48 54 88 before deletion of 32 after deletion AVL Trees 17
  • 18. Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height. We perform restructure(x) to restore balance at z. As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached 62 a=z 44 44 78 w 17 62 b=y 17 50 88 50 78 c=x 48 54 48 54 88 AVL Trees 18
  • 19. Running Times for AVL Trees a single restructure is O(1)  using a linked-structure binary tree find is O(log n)  height of tree is O(log n), no restructures needed insert is O(log n)  initial find is O(log n)  Restructuring up the tree, maintaining heights is O(log n) remove is O(log n)  initial find is O(log n)  Restructuring up the tree, maintaining heights is O(log n) AVL Trees 19

Editor's Notes

  1. Red-Black Trees 11/06/12 23:50