SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Kürzlich hochgeladen (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.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