SlideShare une entreprise Scribd logo
1  sur  31
   BINARY SEARCH TREES.
   MIN AND MAX.
   INSERT AND DELETE OPERATIONS.
   AVL TREES.
   SINGLE ROTATION AND DOUBLE
    ROTATION
        A binary search tree is a binary tree T such that
       Each internal node stores an item(k,e) of a dictionary.
       Keys stored at nodes in the left sub tree of v are less than or equal to k.
       Keys stored at nodes in the right sub tree of v are greater than or equal to
        k.
       Example sequence 2,3,6,5,7,8

                         6
                                       7
              3

    2                                              8
                          5
To find an element with key k in a tree T

compare k with key[root[T]].
if k<key[root[T]],search for k in left[root[T]]
otherwise, search for k in the right[root[T]]
Search (T, 11)
   Recursive version:
               Search(T,k)
                01   x=root[T]
                02   if x= NIL,then return NIL
                03   if x=key[x] then return x
                04   if x<key[x]
                05   then return search(left[x],t)
                06   else retuen search(right[x],t)

   Iterative version:
                search(T,k)
                01 x       root[T]
                02 while x ≠ NIL and k≠key[x] do
                03 if k<key[x]
                04 then x       left[x]
                05 else x        right[x]
                06 return x
   Running time on tree of height h is O(h)
   After the insertion of n keys, the worst-case
    running time of searching is O(n)
                A


                        B


                                C


                                       D
   Find the minimum key in a tree rooted at x

               TreeMinimum (x)
               01 while left[x]≠NIL
               02 do x     left [x]
               03 return x



   Running time O(h), i.e., it is proportional to the
    height of the tree
    Given x ,find the node with the smallest key
    greater than key[x]
   We can distinguish two cases, depending on
    the right subtree of x
   Case 1
       Right subtree of x is non empty
       Successor is leftmost node in the right subtree(why?)
       This can be done by returning TreeMinimum(right[x])
   Case 2
        The right subtree of x is empty
        Successor is the lowest ancestor of x whose
         left child is also an ancestor of x (why?)
   TreeSuccessor (x)
   01 if right[x]≠NIL
   02 then return TreeMinimum(right[x])
   03 y     p[x]
   04 while y≠NIL and x=right[y]
   05 x     y
   06 y     p[y]
   03 return y


   For a tree of height h, the running time is
    O(h)
   The basic idea is similar to searching

         Take an element z(whose left and right children are NULL)
          and insert it into T
         Find place in T in T where z belongs(as if searching for Z),
         And add z there


   The running on a tree of height h is O(h), i.e., it
    is proportional to the height of the tree
   Insert 8
   Delete anode x from a tree T

   We can distinguish three cases

   X has no children.
   X has one child.
   X has two children.
   If x has no children-just remove x
   If x has exactly one child, then to delete x,
    simply make p[x] point to that child
    if x has two children, then to
    delete it we have to
   Find its successor(or
    predecessor) y
   Remove y (note that y has at
    most one child-why?)
   Replace x with y
   These are invented in the year 1962 by two
    Russian mathematician named G.M. Adelson-
    Velskii and E.M. Landis and so named AVL

   AVL Trees are balanced

   An AVL Tree is a binary search tree such that
    for every internal node v of T, the heights of
    the children of v can differ by at most 1
Height of the node is the height of sub tree rooted at
that node.
   Consider an AVL Tree on n nodes.

   Consider a leaf which is closest to the root.

   Suppose this leaf is at level k.

   Then height of the Tree is at most 2k-1.
   In an AVL tree of height h, the leaf closest to
    the root is at level at least (h+1)/2
   On the first (h-1)/2 levels the AVL tree is a
    complete binary tree.
   After (h-1)/2 levels the AVL tree may start
    “thinning out””
   Number of nodes in the AVL tree is at least
    2(h-1)/2 and at most 2h
   Inserting a node v, into an AVL tree changes
    the heights of some of the nodes in T.
   The only nodes whose heights can increase are
    the ancestors of node v.
   If the insertion causes T to become unbalanced,
    then some ancestor of v would have a height-
    imbalance.
   We travel up the tree from v until we find the
    first node such that its grand parent z is
    balanced.
   To balance the sub tree rooted at z, we must
    perform a rotation.
   Insertion happens in sub tree
    T1.
   Ht(t1) increases from h to h+1.
   Since x remains balanced
    ht(t2) is h or h+1 or h+2
   If ht(t2)=h+2 then x is
    originally unbalanced
   If ht(t2)=h+1 then x does not
    increase
   Hence ht(t2)=h
   So ht( x) increases from h+1
    to h+2
   Since y remains balanced,
    ht(T3) us h+1 or h+2 or h+3
   If ht(t3)=h+3 then y is
    originally unbalanced
   If ht(t3)=h+2 then y does not
    increase
   so ht(t2)=h+1
   So ht(y) increases from h+2 to
    h+3.
   Since z was balanced ht(t4) is
    h+1 or h+2 or h+3
   Z is now unbalanced and so
    ht(T4)=h+1
The height of the sub tree remains the same after
rotation. Hence no further rotations required.
Final tree has same height
as original tree. Hence we
need not to go further up
the tree
   When deleting a node in a BST ,we either
    delete a leaf or a node with only one child.
   In an AVL tree if a node has only one child
    then that child is a leaf
   Hence in an AVL tree we either delete a leaf or
    the parent of a leaf.
THANK   YOU

Contenu connexe

Tendances (20)

Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
binary tree
binary treebinary tree
binary tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary trees
Binary treesBinary trees
Binary trees
 
Tree
TreeTree
Tree
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Binary tree
Binary tree Binary tree
Binary tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Trees
TreesTrees
Trees
 
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
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 

Similaire à Trees (20)

Lec8
Lec8Lec8
Lec8
 
Lec8
Lec8Lec8
Lec8
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Lecture3
Lecture3Lecture3
Lecture3
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
B trees
B treesB trees
B trees
 
lecture 13
lecture 13lecture 13
lecture 13
 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
Trees
TreesTrees
Trees
 
Lec12
Lec12Lec12
Lec12
 
7 Trees.pptx
7 Trees.pptx7 Trees.pptx
7 Trees.pptx
 
BST.pdf
BST.pdfBST.pdf
BST.pdf
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
Review session2
Review session2Review session2
Review session2
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
Lec16
Lec16Lec16
Lec16
 
trees.ppt
trees.ppttrees.ppt
trees.ppt
 
Lec16
Lec16Lec16
Lec16
 

Dernier

FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 

Dernier (20)

FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 

Trees

  • 1.
  • 2. BINARY SEARCH TREES.  MIN AND MAX.  INSERT AND DELETE OPERATIONS.  AVL TREES.  SINGLE ROTATION AND DOUBLE ROTATION
  • 3. A binary search tree is a binary tree T such that  Each internal node stores an item(k,e) of a dictionary.  Keys stored at nodes in the left sub tree of v are less than or equal to k.  Keys stored at nodes in the right sub tree of v are greater than or equal to k.  Example sequence 2,3,6,5,7,8 6 7 3 2 8 5
  • 4. To find an element with key k in a tree T compare k with key[root[T]]. if k<key[root[T]],search for k in left[root[T]] otherwise, search for k in the right[root[T]]
  • 6.
  • 7. Recursive version: Search(T,k) 01 x=root[T] 02 if x= NIL,then return NIL 03 if x=key[x] then return x 04 if x<key[x] 05 then return search(left[x],t) 06 else retuen search(right[x],t)  Iterative version: search(T,k) 01 x root[T] 02 while x ≠ NIL and k≠key[x] do 03 if k<key[x] 04 then x left[x] 05 else x right[x] 06 return x
  • 8. Running time on tree of height h is O(h)  After the insertion of n keys, the worst-case running time of searching is O(n) A B C D
  • 9. Find the minimum key in a tree rooted at x TreeMinimum (x) 01 while left[x]≠NIL 02 do x left [x] 03 return x  Running time O(h), i.e., it is proportional to the height of the tree
  • 10. Given x ,find the node with the smallest key greater than key[x]  We can distinguish two cases, depending on the right subtree of x  Case 1  Right subtree of x is non empty  Successor is leftmost node in the right subtree(why?)  This can be done by returning TreeMinimum(right[x])
  • 11. Case 2  The right subtree of x is empty  Successor is the lowest ancestor of x whose left child is also an ancestor of x (why?)
  • 12. TreeSuccessor (x)  01 if right[x]≠NIL  02 then return TreeMinimum(right[x])  03 y p[x]  04 while y≠NIL and x=right[y]  05 x y  06 y p[y]  03 return y  For a tree of height h, the running time is O(h)
  • 13. The basic idea is similar to searching  Take an element z(whose left and right children are NULL) and insert it into T  Find place in T in T where z belongs(as if searching for Z),  And add z there  The running on a tree of height h is O(h), i.e., it is proportional to the height of the tree
  • 14. Insert 8
  • 15.
  • 16. Delete anode x from a tree T  We can distinguish three cases  X has no children.  X has one child.  X has two children.
  • 17. If x has no children-just remove x
  • 18. If x has exactly one child, then to delete x, simply make p[x] point to that child
  • 19. if x has two children, then to delete it we have to  Find its successor(or predecessor) y  Remove y (note that y has at most one child-why?)  Replace x with y
  • 20. These are invented in the year 1962 by two Russian mathematician named G.M. Adelson- Velskii and E.M. Landis and so named AVL  AVL Trees are balanced  An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1
  • 21. Height of the node is the height of sub tree rooted at that node.
  • 22. Consider an AVL Tree on n nodes.  Consider a leaf which is closest to the root.  Suppose this leaf is at level k.  Then height of the Tree is at most 2k-1.
  • 23. In an AVL tree of height h, the leaf closest to the root is at level at least (h+1)/2  On the first (h-1)/2 levels the AVL tree is a complete binary tree.  After (h-1)/2 levels the AVL tree may start “thinning out””  Number of nodes in the AVL tree is at least 2(h-1)/2 and at most 2h
  • 24. Inserting a node v, into an AVL tree changes the heights of some of the nodes in T.  The only nodes whose heights can increase are the ancestors of node v.  If the insertion causes T to become unbalanced, then some ancestor of v would have a height- imbalance.  We travel up the tree from v until we find the first node such that its grand parent z is balanced.
  • 25. To balance the sub tree rooted at z, we must perform a rotation.
  • 26. Insertion happens in sub tree T1.  Ht(t1) increases from h to h+1.  Since x remains balanced ht(t2) is h or h+1 or h+2  If ht(t2)=h+2 then x is originally unbalanced  If ht(t2)=h+1 then x does not increase  Hence ht(t2)=h  So ht( x) increases from h+1 to h+2
  • 27. Since y remains balanced, ht(T3) us h+1 or h+2 or h+3  If ht(t3)=h+3 then y is originally unbalanced  If ht(t3)=h+2 then y does not increase  so ht(t2)=h+1  So ht(y) increases from h+2 to h+3.  Since z was balanced ht(t4) is h+1 or h+2 or h+3  Z is now unbalanced and so ht(T4)=h+1
  • 28. The height of the sub tree remains the same after rotation. Hence no further rotations required.
  • 29. Final tree has same height as original tree. Hence we need not to go further up the tree
  • 30. When deleting a node in a BST ,we either delete a leaf or a node with only one child.  In an AVL tree if a node has only one child then that child is a leaf  Hence in an AVL tree we either delete a leaf or the parent of a leaf.
  • 31. THANK YOU