SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
(2,4) Trees

   What are they?
     They are search Trees (but not binary search trees)
     They are also known as 2-4, 2-3-4 trees
Multi-way Search Trees
   Each internal node of a multi-way search tree T:
     has at least two children
     stores a collection of items of the form (k, x), where k
      is a key and x is an element
     contains d - 1 items, where d is the number of children
     Has pointers to d children

 Children of each internal node are “between”
  items
 all keys in the subtree rooted at the child fall
  between keys of those items.
Multi-way Searching
   Similar to binary searching
     If search key s<k1 search
                                            Searching       22
      the leftmost child                    for s = 8
     If s>kd-1 , search the                                Searching
                                                            for s = 12
      rightmost child                          5 10                      25

   That’s it in a binary tree;
                                     3 4
                                      3 4     6 8           14      23 24     27
    what about if d>2?
       Find two keys ki-1 and ki
                                                    11 13         17 18 19 20 21
        between which s falls, and
        search the child vi.                      Not found!
   What would an in-order
    traversal look like?
(2,4) Trees

   Properties:
     At most 4 children                         12

     All leaf nodes are at
      the same level.                5 10                 15
     Height h of (2,4) tree
      is at least log4 n and
                               3 4   6 8    11        13 14    17
      atmost log2 n
   How is the last fact
    useful in searching?
Insertion
                                        No problem if the node
  21 23 40 29       7                    has empty space



                                        13 22 32



           3 8 10                   18                  25                 35



1 2     4 5 6   9       11 12   14 15      20      24        26 28 30 33        37 39
Insertion(2)
                                   Nodes get split if there is
              29   7                insufficient space.



                                       13 22 32



         3 8 10                     18                25                 35



1 2   4 5 6   9        11 12   14 15      20 21   23 24    26 28 30 33        37 39 40
Insertion(3)
                    7                  One key is promoted to
                                        parent and inserted in there


                                        13 22 32



           3 8 10                   18                 25               35



1 2     4 5 6   9       11 12   14 15      20 21   23 24           33        37 39 40
                                                        26 28   29 30
Insertion(4)
       If parent node does not have sufficient space
        then it is split.
       In this manner splits can cascade.




                                      13 22 32



            3 8 10                18                 25 28           35


1 2               9   11 12   14 15      20 21   23 24          33        37 39 40
      4 5   6 7                                       26     29 30
Insertion(5)
       Eventually we may have to create a new root.
       This increases the height of the tree




                                   5 13            2232



          3         8 10
                                          18               25 28           35


1 2                 9      11 12   14 15       20 21   23 24          33        37 39 40
      4       6 7                                           26     29 30
Time for Search and Insertion

    A search visits O(log N) nodes
    An insertion requires O(log N) node splits
    Each node split takes constant time
    Hence, operations Search and Insert each take time
     O(log N)
Deletion
   Delete 21.
   No problem if key to be deleted is in a leaf with at least 2
    keys
                                               13


                                       5                22



              3         8 10
                                           18                    25 28



    1 2                 9      11 12   14 15        20 21    23 24
          4       6 7                                             26     29 30
Deletion(2)
 If key to be deleted is in an internal node then
  we swap it with its predecessor (which is in a
  leaf) and then delete it.
 Delete 25
                                               13


                                       5                 22



              3         8 10
                                           18                     25 28



    1 2                 9      11 12   14 15        20        23 24
          4       6 7                                              26     29 30
Deletion(3)
 If after deleting a key a node becomes empty
  then we borrow a key from its sibling.
 Delete 20
                                               13


                                       5                 22



              3         8 10
                                           18                      24 28



    1 2                 9      11 12   14 15        20        23
          4       6 7                                              26      29 30
Deletion(4)
 If sibling has only one key then we merge with it.
 The key in the parent node separating these two
  siblings moves down into the merged node.
 Delete 23                  13


                                   5              22



          3         8 10
                                        15                  24 28



1 2                 9      11 12   14        18        23
      4       6 7                                           26      29 30
Delete(5)
 Moving a key down from the parent corresponds
  to deletion in the parent node.
 The procedure is the same as for a leaf node.
                              13
 Can lead to a cascade .
 Delete 18              5        22



              3         8 10
                                            15                28



    1 2                 9      11 12   14        18
          4       6 7                                              29 30
                                                      24 26
(2,4) Conclusion
 The  height of a (2,4) tree is O(log n).
 Split, transfer, and merge each take O(1).
 Search, insertion and deletion each take
  O(log n) .
 Why are we doing this?
   (2,4)trees are fun! Why else would we do it?
   Well, there’s another reason, too.
   They’re pretty fundamental to the idea of
    Red-Black trees as well.

Weitere ähnliche Inhalte

Was ist angesagt?

Addandsubtractmixednumbers
AddandsubtractmixednumbersAddandsubtractmixednumbers
Addandsubtractmixednumbersimarianesm
 
Precalculus 1 chapter 2
Precalculus 1 chapter 2Precalculus 1 chapter 2
Precalculus 1 chapter 2oreves
 
Day 14 solving one step ineq mult div
Day 14 solving one step ineq mult divDay 14 solving one step ineq mult div
Day 14 solving one step ineq mult divErik Tjersland
 
Structures I session 18 11 8
Structures I session 18 11 8Structures I session 18 11 8
Structures I session 18 11 8bill_smith_535
 
Ch 3 rev trashketball exp logs
Ch 3 rev trashketball exp logsCh 3 rev trashketball exp logs
Ch 3 rev trashketball exp logsKristen Fouss
 
Day 14 solving one step ineq mult div
Day 14 solving one step ineq mult divDay 14 solving one step ineq mult div
Day 14 solving one step ineq mult divErik Tjersland
 
Eight Regression Algorithms
Eight Regression AlgorithmsEight Regression Algorithms
Eight Regression Algorithmsguestfee8698
 

Was ist angesagt? (10)

Addandsubtractmixednumbers
AddandsubtractmixednumbersAddandsubtractmixednumbers
Addandsubtractmixednumbers
 
Precalculus 1 chapter 2
Precalculus 1 chapter 2Precalculus 1 chapter 2
Precalculus 1 chapter 2
 
Day 14 solving one step ineq mult div
Day 14 solving one step ineq mult divDay 14 solving one step ineq mult div
Day 14 solving one step ineq mult div
 
Structures I session 18 11 8
Structures I session 18 11 8Structures I session 18 11 8
Structures I session 18 11 8
 
2s power
2s power2s power
2s power
 
Bessel 1 div_3
Bessel 1 div_3Bessel 1 div_3
Bessel 1 div_3
 
Btrees
BtreesBtrees
Btrees
 
Ch 3 rev trashketball exp logs
Ch 3 rev trashketball exp logsCh 3 rev trashketball exp logs
Ch 3 rev trashketball exp logs
 
Day 14 solving one step ineq mult div
Day 14 solving one step ineq mult divDay 14 solving one step ineq mult div
Day 14 solving one step ineq mult div
 
Eight Regression Algorithms
Eight Regression AlgorithmsEight Regression Algorithms
Eight Regression Algorithms
 

Andere mochten auch

Top 9 unique structures
Top 9 unique structuresTop 9 unique structures
Top 9 unique structuresfilipj2000
 
Investigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel anguloInvestigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel anguloFidel Angulo
 
Siberian dream
Siberian dreamSiberian dream
Siberian dreamfilipj2000
 
08 copia de tarjeta kardex
08 copia de tarjeta kardex08 copia de tarjeta kardex
08 copia de tarjeta kardexANGIE MARCELA
 
기업교육론 6장 학생발표자료
기업교육론 6장 학생발표자료기업교육론 6장 학생발표자료
기업교육론 6장 학생발표자료조현경
 
Kunstzinnige orientatie 2010
Kunstzinnige orientatie 2010Kunstzinnige orientatie 2010
Kunstzinnige orientatie 2010Linda de Groot
 
Enhancing Our Pedagogy Cit Conference
Enhancing Our Pedagogy Cit ConferenceEnhancing Our Pedagogy Cit Conference
Enhancing Our Pedagogy Cit ConferenceRosalyn Rufer
 
Met de trein_mee-elza
Met de trein_mee-elzaMet de trein_mee-elza
Met de trein_mee-elzafilipj2000
 
Direct mailresponsesuccessandfulfillment
Direct mailresponsesuccessandfulfillmentDirect mailresponsesuccessandfulfillment
Direct mailresponsesuccessandfulfillmentPatti Mazzara
 
Introduction to Steens Furniture
Introduction to Steens FurnitureIntroduction to Steens Furniture
Introduction to Steens FurnitureSteens Furniture
 
Capoeira power point- IKT
Capoeira power point- IKTCapoeira power point- IKT
Capoeira power point- IKTlanag
 

Andere mochten auch (20)

Lec7
Lec7Lec7
Lec7
 
Lec10
Lec10Lec10
Lec10
 
Lec1
Lec1Lec1
Lec1
 
Top 9 unique structures
Top 9 unique structuresTop 9 unique structures
Top 9 unique structures
 
Orosz fotosok
Orosz fotosokOrosz fotosok
Orosz fotosok
 
Investigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel anguloInvestigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel angulo
 
Siberian dream
Siberian dreamSiberian dream
Siberian dream
 
Draw1.
Draw1.Draw1.
Draw1.
 
08 copia de tarjeta kardex
08 copia de tarjeta kardex08 copia de tarjeta kardex
08 copia de tarjeta kardex
 
Presentation1
Presentation1Presentation1
Presentation1
 
기업교육론 6장 학생발표자료
기업교육론 6장 학생발표자료기업교육론 6장 학생발표자료
기업교육론 6장 학생발표자료
 
Kunstzinnige orientatie 2010
Kunstzinnige orientatie 2010Kunstzinnige orientatie 2010
Kunstzinnige orientatie 2010
 
Enhancing Our Pedagogy Cit Conference
Enhancing Our Pedagogy Cit ConferenceEnhancing Our Pedagogy Cit Conference
Enhancing Our Pedagogy Cit Conference
 
Grecia linda
Grecia lindaGrecia linda
Grecia linda
 
Presentatie openbare verlichting juni 2010
Presentatie openbare verlichting juni 2010Presentatie openbare verlichting juni 2010
Presentatie openbare verlichting juni 2010
 
Met de trein_mee-elza
Met de trein_mee-elzaMet de trein_mee-elza
Met de trein_mee-elza
 
Direct mailresponsesuccessandfulfillment
Direct mailresponsesuccessandfulfillmentDirect mailresponsesuccessandfulfillment
Direct mailresponsesuccessandfulfillment
 
Introduction to Steens Furniture
Introduction to Steens FurnitureIntroduction to Steens Furniture
Introduction to Steens Furniture
 
Valet Till Gymnasiet
Valet Till GymnasietValet Till Gymnasiet
Valet Till Gymnasiet
 
Capoeira power point- IKT
Capoeira power point- IKTCapoeira power point- IKT
Capoeira power point- IKT
 

Mehr von Anjneya Varshney (15)

Lec23
Lec23Lec23
Lec23
 
Lec22
Lec22Lec22
Lec22
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 
Lec16
Lec16Lec16
Lec16
 
Lec12
Lec12Lec12
Lec12
 
Lec11
Lec11Lec11
Lec11
 
Lec9
Lec9Lec9
Lec9
 
Lec8
Lec8Lec8
Lec8
 
Lec6
Lec6Lec6
Lec6
 
Lec5
Lec5Lec5
Lec5
 
Lec4
Lec4Lec4
Lec4
 
Lec3
Lec3Lec3
Lec3
 
Lec2
Lec2Lec2
Lec2
 

Lec13

  • 1. (2,4) Trees  What are they?  They are search Trees (but not binary search trees)  They are also known as 2-4, 2-3-4 trees
  • 2. Multi-way Search Trees  Each internal node of a multi-way search tree T:  has at least two children  stores a collection of items of the form (k, x), where k is a key and x is an element  contains d - 1 items, where d is the number of children  Has pointers to d children  Children of each internal node are “between” items  all keys in the subtree rooted at the child fall between keys of those items.
  • 3. Multi-way Searching  Similar to binary searching  If search key s<k1 search Searching 22 the leftmost child for s = 8  If s>kd-1 , search the Searching for s = 12 rightmost child 5 10 25  That’s it in a binary tree; 3 4 3 4 6 8 14 23 24 27 what about if d>2?  Find two keys ki-1 and ki 11 13 17 18 19 20 21 between which s falls, and search the child vi. Not found!  What would an in-order traversal look like?
  • 4. (2,4) Trees  Properties:  At most 4 children 12  All leaf nodes are at the same level. 5 10 15  Height h of (2,4) tree is at least log4 n and 3 4 6 8 11 13 14 17 atmost log2 n  How is the last fact useful in searching?
  • 5. Insertion  No problem if the node 21 23 40 29 7 has empty space 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 24 26 28 30 33 37 39
  • 6. Insertion(2)  Nodes get split if there is 29 7 insufficient space. 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 26 28 30 33 37 39 40
  • 7. Insertion(3) 7  One key is promoted to parent and inserted in there 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 33 37 39 40 26 28 29 30
  • 8. Insertion(4)  If parent node does not have sufficient space then it is split.  In this manner splits can cascade. 13 22 32 3 8 10 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 4 5 6 7 26 29 30
  • 9. Insertion(5)  Eventually we may have to create a new root.  This increases the height of the tree 5 13 2232 3 8 10 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 4 6 7 26 29 30
  • 10. Time for Search and Insertion  A search visits O(log N) nodes  An insertion requires O(log N) node splits  Each node split takes constant time  Hence, operations Search and Insert each take time O(log N)
  • 11. Deletion  Delete 21.  No problem if key to be deleted is in a leaf with at least 2 keys 13 5 22 3 8 10 18 25 28 1 2 9 11 12 14 15 20 21 23 24 4 6 7 26 29 30
  • 12. Deletion(2)  If key to be deleted is in an internal node then we swap it with its predecessor (which is in a leaf) and then delete it.  Delete 25 13 5 22 3 8 10 18 25 28 1 2 9 11 12 14 15 20 23 24 4 6 7 26 29 30
  • 13. Deletion(3)  If after deleting a key a node becomes empty then we borrow a key from its sibling.  Delete 20 13 5 22 3 8 10 18 24 28 1 2 9 11 12 14 15 20 23 4 6 7 26 29 30
  • 14. Deletion(4)  If sibling has only one key then we merge with it.  The key in the parent node separating these two siblings moves down into the merged node.  Delete 23 13 5 22 3 8 10 15 24 28 1 2 9 11 12 14 18 23 4 6 7 26 29 30
  • 15. Delete(5)  Moving a key down from the parent corresponds to deletion in the parent node.  The procedure is the same as for a leaf node. 13  Can lead to a cascade .  Delete 18 5 22 3 8 10 15 28 1 2 9 11 12 14 18 4 6 7 29 30 24 26
  • 16. (2,4) Conclusion  The height of a (2,4) tree is O(log n).  Split, transfer, and merge each take O(1).  Search, insertion and deletion each take O(log n) .  Why are we doing this?  (2,4)trees are fun! Why else would we do it?  Well, there’s another reason, too.  They’re pretty fundamental to the idea of Red-Black trees as well.