SlideShare una empresa de Scribd logo
1 de 8
Multiway Search Trees
Intuitive Definition

A multiway search tree is one with nodes that have two or more children. Within each
node is stored a given key, which is associated to an item we wish to access through the
structure.
Given this definition, a binary search tree is a multiway search tree.

More Formal Definition

Let T be a multiway search tree, then T has the following properties:

   •   T is ordered, meaning that the all the elements in subtrees to the left of an item are
       less than the item itself, and all the elements in subtrees to the right of an item are
       greater.
   •   Each internal node of T has at least 2 children.
   •   Each d-node (node with d children) v of T, with children v1,...,vd stores d-1 items
       (k1, x1),...,(kd-1, xd-1). Where the ki's are keys and xi is the element associated with
       key number i.
   •   External nodes are empty




   A multiway search tree with a successful search path for
   the number 6 (in green), and an unsuccessful search path
                  for the number 26 (in red)
Definition of a (2,4)-tree
A (2,4)-tree is simply a multiway search tree (as defined above) that also satisfies the
following properties:

  I.       SIZE: every node can have no more than 4 children.
 II.       DEPTH: all external nodes have the same depth.

Assuming that we are able to maintain these properties (which still remains to be seen!),
then we can deduce a couple of useful properties of this structure:

       1. if follows from the the SIZE property that the number of items at each node is
          less than or equal to 4. Hence dmax is constant and our search time is already down
          to O(h)!
       2. luckily, the DEPTH property ensures that the tree is balanced, but also that the
          height is restricted to THETA(logn) (where n is the number of nodes in the tree).
          Click here if you want to see the proof

Observations 1 and 2 imply that the worst case search time in a (2,4)-tree is O(logn)
under the assumption that we can efficiently maintain properties I and II.

So we've got to see if we can insert and delete items from this tree efficiently without
disturbing these properties. This will be the topic of the next two sections.

[Top] [Bottom] [Home]

6.7 Insertion
In this section we will show that:

       •   The SIZE and DEPTH depth properties of (2,4)-trees can be maintained upon
           insertion of a new item.
       •   The maintenance cost is bounded above by the height of the tree

6.7.1 The insertion algorithm

Let's begin with a basic algorithm for insertion and work from there. We would like to
INSERT a key k into a (2,4)-tree T. Here are the steps we follow:

       1. perform a SEARCH for k in T.
          if it succeeds then we don't need to INSERT the item and we're done,
          otherwise (if it fails) then the search terminates at an external node z.
       2. let v be the parent node of z, insert k into the appropriate place in v and add a new
          child w to v on the left of z (see figure 6.2(a)).
And that's IT (well...essentially)! Since the procedure did not change the depth of T, the
tree is balanced and we have in no way violated the DEPTH property, but what about the
SIZE property?




Figure 6.2: insertion of new key k resulting in new node w
(in red) (a) with no overflow, (b) with an overflow at node
                             v

Consider what would happen if v already had 4 children before we inserted k into it (see
figure 6.2(b)). After insertion v would now be a 5-node thereby violationg the size
property of T! This is called an overflow at node v, and it must be resolved in order for
our algorithm to be valid for (2,4)-trees.

We fix this little glitch by SPLITTING v into two smaller nodes as follows (figure
6.3): Let v1,...,v5 be the children of v (which stores keys k1,...,k4),

   1. Split v by replacing it with v' (a 3-node that stores keys k1 and k2) and v" (a 2-node
      that stores k4).
   2. Store k3 in what was the parent of v (if v was the root we create a new node and
      store it in there). Call that node u.
   3. make v' and v" the children of u. (if v was the ith child of u, then v' and v" become
      the ith and (i+1)st children of u).
Figure 6.3: local state of T after the node v has been
                    split into v' and v".

First observe that this procedure has perfectly taken care of the overflow situation at node
v, but has potentially created an overflow at node u (since u now has had one child added
to it)! In this case we would simply repeat the SPLITTING procedure at node u.

Notice also that this procedure will eventually terminate at a node that doesn't overflow
or at the root (where we create a brand new node that obviously doesn't overflow).

Finally, you may have spotted the fact that the we change the depth of the tree when we
create a new root. However this doesn't violate our DEPTH property since every node in
the tree's depth increases by 1 (hence the tree stays balanced).

6.7.2 Analysis of insertion

   •   We began with a search procedure which take O(logn) time.
   •   Next we insert the key into v in O(1) time.
   •   Finally, we had a maximum of O(h) split operations to maintain the SIZE
       property. As we showed in section 6.6, h is THETA(logn). Hence we have at
       worst case O(logn) splits.
   •   Each split affects a constant number of items in a constant number of nodes of T
       and hence takes O(1) time.

Therefore, insertion can be performed in (2,4)-trees in O(logn) time (where n is the
number of nodes in the tree).

[Top] [Bottom] [Home]

6.8 Deletion
In the previous section, we saw that the SIZE and DEPTH properties of (2,4)-trees can be
maintained efficiently as new items are inserted into the tree. We now show that the same
result holds as items are removed.

6.8.1 The deletion algorithm

Once again we'll begin with a basic algorithm that we'll adjust. We want to DELETE a
key k from T. For now, we shall assume that k is stored in a node v whose children are all
external nodes. Here are the steps we follow:

       1. perform a SEARCH for k in T,
          if it fails then we don't need to DELETE the item so we exit,
          otherwise (if it succeds) then we find k in a node v with only external children
          (by our assumption).
       2. now we simply remove k from v and delete the external node child to the left
          of k (see figure 6.4(a)).

Simple enough. Since we only deleted external nodes, we didn't change the depth of
T, so that property is safe. However, we may have violated the SIZE property once
again.




 Figure 6.4: deletion of key k resulting in (a)two children
  remaining in v so no underflow, (b) underflow at node v
           because it is left with only one child

This is because (as shown in figure 6.4(b)) v may have only had two children
before we removed k. In such a case, it will be left as a 1-node, which violates the
definition of a multiway search tree (see section 6.2.2). This situation is called an
underflow at node v. Again, we're going to have to resolve this in order to validate
our deletion algorithm.

Let u be the parent of v. To solve this problem, we consider two seperate cases:

  I.      v has a sibling w that is a 3-node or a 4-node.
          In this case we perform a TRANSFER operation as follows (figure 6.5):
            a. Move a child of w to v.
            b.    Move a key from w to u.
            c. Move a key from u to v.
This may seem a little cryptic, but one look at the figure should make this
quite clear. As you can see, this operation has the following effects:

   q      It adds a child to v and removes one from w (thereby making v a 2-
        node and w a 2 or 3-node, resolving the underflow at v).
   )      But now we must transfer the corresponding keys without
        destroying the ordered nature of the tree. We do this by pushing the
        extra key from w through the appropriate key in the parent u, and
        finally into v.
   .      The net effect is that the number of keys in w has been reduced by 1,
        and the number of keys in v has been increased by 1 as desired!




         Figure 6.5: deletion of key 4 resulting in an
        underflow at v. (a) transfer operation, (b) the
              resulting tree after the transfer.



 ΙΙ.    v has no such siblings (i.e., they are all 2-nodes).
        This case requires a FUSION of two nodes as follows (figure 6.6):

   a.     Merge v with a 2-node sibling w creating a new node v'.
   b.     Move a key from u (v's parent) to v'.
After step (a), v' has 3 children, but stores only one key, and u has lost a child
       (since two of it's children merged into one), hence we move a key from u to v'
       to preserve the tree.

Note that the FUSION operation reduces the number of u's children by 1, potentially
causing an underflow at u. In such a case we would remedy this with another
FUSION or TRANSFER.
Observe also that this process also terminates at the root (an underflow at the root
simply causes its deletion), and is hence bounded above by O(logn) as was the
SPLIT operation in the INSERTION algorithm.




Figure 6.6: deletion of key 11 resulting in an underflow at
v. (a) fusion of v and w, (b) the resulting tree after the
                          fusion.

Almost done! However, remember that everything above only applies if the key
we're trying to remove is stored at a node with only external nodes for children (if
you don't remember making this assumption click here)! What do we do if this is
not the case? Well, there just so happens to be a very easy way to swap any key in a
(2,4)-tree with one that is stored in a node with the property we desire without
destroying the ordering in the tree. This is done as follows:

Swap the internal key ki with the largest element in the subtree immediately to its
left. By the definition of a search tree, this key is the next smallest key in the tree
next to ki. So once we delete ki, the tree will be correctly ordered.
A natural question to ask would be "how do we find this element?" This is
accomplished by performing the following steps:

   1. Let v be the internal node in which the element we wish to delete (ki) is
      stored.
   2. Let w be the right-most internal node in the subtree rooted at the ith child of
      v.
   3. Swap ki with the last item of w.

Once this is done, the item we wish to delete will be at a node that has only external
nodes for children, and we will be able to use the above procedure to delete it.

6.8.2 Analysis of deletion

   •   We began with a search procedure which takes O(logn) time.
   •   This may be followed with a swap which also takes O(logn).
   •   Finally, we had a maximum total of O(logn) TRANSFER or FUSION
       operations (each of which takes constant time).

Therefore, deletion can also be accomplished in (2,4)-trees in O(logn) time.

Más contenido relacionado

Similar a 2 4 Tree

Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Getachew Ganfur
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.pptAmitShou
 
ADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationHemanth Kumar
 
Wavelets for computer_graphics_stollnitz
Wavelets for computer_graphics_stollnitzWavelets for computer_graphics_stollnitz
Wavelets for computer_graphics_stollnitzJuliocaramba
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bstchidabdu
 
Implement special cases here define delete t v let.pdf
Implement special cases here define delete t v      let.pdfImplement special cases here define delete t v      let.pdf
Implement special cases here define delete t v let.pdfADITIEYEWEAR
 
B tree by-jash acharya
B tree by-jash acharyaB tree by-jash acharya
B tree by-jash acharyaJash Acharya
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Splay trees balance trees in a different way from AVL trees. A.docx
Splay trees balance trees in a different way from AVL trees. A.docxSplay trees balance trees in a different way from AVL trees. A.docx
Splay trees balance trees in a different way from AVL trees. A.docxmckellarhastings
 
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 sir22001003058
 

Similar a 2 4 Tree (20)

Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 
s11_bin_search_trees.ppt
s11_bin_search_trees.ppts11_bin_search_trees.ppt
s11_bin_search_trees.ppt
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
 
ADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_Allocation
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Wavelets for computer_graphics_stollnitz
Wavelets for computer_graphics_stollnitzWavelets for computer_graphics_stollnitz
Wavelets for computer_graphics_stollnitz
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Implement special cases here define delete t v let.pdf
Implement special cases here define delete t v      let.pdfImplement special cases here define delete t v      let.pdf
Implement special cases here define delete t v let.pdf
 
Lec15
Lec15Lec15
Lec15
 
B tree by-jash acharya
B tree by-jash acharyaB tree by-jash acharya
B tree by-jash acharya
 
HeapSort
HeapSortHeapSort
HeapSort
 
Heapsort
HeapsortHeapsort
Heapsort
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) Questions
 
Review session2
Review session2Review session2
Review session2
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Trees
TreesTrees
Trees
 
Splay trees balance trees in a different way from AVL trees. A.docx
Splay trees balance trees in a different way from AVL trees. A.docxSplay trees balance trees in a different way from AVL trees. A.docx
Splay trees balance trees in a different way from AVL trees. A.docx
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
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
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 

Más de vikram singh

Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2vikram singh
 
Web tech importants
Web tech importantsWeb tech importants
Web tech importantsvikram singh
 
Enterprise Java Beans( E)
Enterprise  Java  Beans( E)Enterprise  Java  Beans( E)
Enterprise Java Beans( E)vikram singh
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2vikram singh
 
Enterprise java beans(ejb)
Enterprise java beans(ejb)Enterprise java beans(ejb)
Enterprise java beans(ejb)vikram singh
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharingvikram singh
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servletvikram singh
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorialvikram singh
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1vikram singh
 

Más de vikram singh (20)

Agile
AgileAgile
Agile
 
Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2
 
Web tech importants
Web tech importantsWeb tech importants
Web tech importants
 
Enterprise Java Beans( E)
Enterprise  Java  Beans( E)Enterprise  Java  Beans( E)
Enterprise Java Beans( E)
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2
 
Enterprise java beans(ejb)
Enterprise java beans(ejb)Enterprise java beans(ejb)
Enterprise java beans(ejb)
 
23 Tree Best Part
23 Tree   Best Part23 Tree   Best Part
23 Tree Best Part
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharing
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
jdbc
jdbcjdbc
jdbc
 
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorial
 
Xml
XmlXml
Xml
 
Dtd
DtdDtd
Dtd
 
Xml Schema
Xml SchemaXml Schema
Xml Schema
 
JSP
JSPJSP
JSP
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
 
Servlet Part 2
Servlet Part 2Servlet Part 2
Servlet Part 2
 
Tutorial Solution
Tutorial SolutionTutorial Solution
Tutorial Solution
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 

Último

A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMANIlamathiKannappan
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfAdmir Softic
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...lizamodels9
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageMatteo Carbone
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Centuryrwgiffor
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...rajveerescorts2022
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Dipal Arora
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Neil Kimberley
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxWorkforce Group
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communicationskarancommunications
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxpriyanshujha201
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...amitlee9823
 
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Lviv Startup Club
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with CultureSeta Wicaksana
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyEthan lee
 

Último (20)

A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
 

2 4 Tree

  • 1. Multiway Search Trees Intuitive Definition A multiway search tree is one with nodes that have two or more children. Within each node is stored a given key, which is associated to an item we wish to access through the structure. Given this definition, a binary search tree is a multiway search tree. More Formal Definition Let T be a multiway search tree, then T has the following properties: • T is ordered, meaning that the all the elements in subtrees to the left of an item are less than the item itself, and all the elements in subtrees to the right of an item are greater. • Each internal node of T has at least 2 children. • Each d-node (node with d children) v of T, with children v1,...,vd stores d-1 items (k1, x1),...,(kd-1, xd-1). Where the ki's are keys and xi is the element associated with key number i. • External nodes are empty A multiway search tree with a successful search path for the number 6 (in green), and an unsuccessful search path for the number 26 (in red)
  • 2. Definition of a (2,4)-tree A (2,4)-tree is simply a multiway search tree (as defined above) that also satisfies the following properties: I. SIZE: every node can have no more than 4 children. II. DEPTH: all external nodes have the same depth. Assuming that we are able to maintain these properties (which still remains to be seen!), then we can deduce a couple of useful properties of this structure: 1. if follows from the the SIZE property that the number of items at each node is less than or equal to 4. Hence dmax is constant and our search time is already down to O(h)! 2. luckily, the DEPTH property ensures that the tree is balanced, but also that the height is restricted to THETA(logn) (where n is the number of nodes in the tree). Click here if you want to see the proof Observations 1 and 2 imply that the worst case search time in a (2,4)-tree is O(logn) under the assumption that we can efficiently maintain properties I and II. So we've got to see if we can insert and delete items from this tree efficiently without disturbing these properties. This will be the topic of the next two sections. [Top] [Bottom] [Home] 6.7 Insertion In this section we will show that: • The SIZE and DEPTH depth properties of (2,4)-trees can be maintained upon insertion of a new item. • The maintenance cost is bounded above by the height of the tree 6.7.1 The insertion algorithm Let's begin with a basic algorithm for insertion and work from there. We would like to INSERT a key k into a (2,4)-tree T. Here are the steps we follow: 1. perform a SEARCH for k in T. if it succeeds then we don't need to INSERT the item and we're done, otherwise (if it fails) then the search terminates at an external node z. 2. let v be the parent node of z, insert k into the appropriate place in v and add a new child w to v on the left of z (see figure 6.2(a)).
  • 3. And that's IT (well...essentially)! Since the procedure did not change the depth of T, the tree is balanced and we have in no way violated the DEPTH property, but what about the SIZE property? Figure 6.2: insertion of new key k resulting in new node w (in red) (a) with no overflow, (b) with an overflow at node v Consider what would happen if v already had 4 children before we inserted k into it (see figure 6.2(b)). After insertion v would now be a 5-node thereby violationg the size property of T! This is called an overflow at node v, and it must be resolved in order for our algorithm to be valid for (2,4)-trees. We fix this little glitch by SPLITTING v into two smaller nodes as follows (figure 6.3): Let v1,...,v5 be the children of v (which stores keys k1,...,k4), 1. Split v by replacing it with v' (a 3-node that stores keys k1 and k2) and v" (a 2-node that stores k4). 2. Store k3 in what was the parent of v (if v was the root we create a new node and store it in there). Call that node u. 3. make v' and v" the children of u. (if v was the ith child of u, then v' and v" become the ith and (i+1)st children of u).
  • 4. Figure 6.3: local state of T after the node v has been split into v' and v". First observe that this procedure has perfectly taken care of the overflow situation at node v, but has potentially created an overflow at node u (since u now has had one child added to it)! In this case we would simply repeat the SPLITTING procedure at node u. Notice also that this procedure will eventually terminate at a node that doesn't overflow or at the root (where we create a brand new node that obviously doesn't overflow). Finally, you may have spotted the fact that the we change the depth of the tree when we create a new root. However this doesn't violate our DEPTH property since every node in the tree's depth increases by 1 (hence the tree stays balanced). 6.7.2 Analysis of insertion • We began with a search procedure which take O(logn) time. • Next we insert the key into v in O(1) time. • Finally, we had a maximum of O(h) split operations to maintain the SIZE property. As we showed in section 6.6, h is THETA(logn). Hence we have at worst case O(logn) splits. • Each split affects a constant number of items in a constant number of nodes of T and hence takes O(1) time. Therefore, insertion can be performed in (2,4)-trees in O(logn) time (where n is the number of nodes in the tree). [Top] [Bottom] [Home] 6.8 Deletion
  • 5. In the previous section, we saw that the SIZE and DEPTH properties of (2,4)-trees can be maintained efficiently as new items are inserted into the tree. We now show that the same result holds as items are removed. 6.8.1 The deletion algorithm Once again we'll begin with a basic algorithm that we'll adjust. We want to DELETE a key k from T. For now, we shall assume that k is stored in a node v whose children are all external nodes. Here are the steps we follow: 1. perform a SEARCH for k in T, if it fails then we don't need to DELETE the item so we exit, otherwise (if it succeds) then we find k in a node v with only external children (by our assumption). 2. now we simply remove k from v and delete the external node child to the left of k (see figure 6.4(a)). Simple enough. Since we only deleted external nodes, we didn't change the depth of T, so that property is safe. However, we may have violated the SIZE property once again. Figure 6.4: deletion of key k resulting in (a)two children remaining in v so no underflow, (b) underflow at node v because it is left with only one child This is because (as shown in figure 6.4(b)) v may have only had two children before we removed k. In such a case, it will be left as a 1-node, which violates the definition of a multiway search tree (see section 6.2.2). This situation is called an underflow at node v. Again, we're going to have to resolve this in order to validate our deletion algorithm. Let u be the parent of v. To solve this problem, we consider two seperate cases: I. v has a sibling w that is a 3-node or a 4-node. In this case we perform a TRANSFER operation as follows (figure 6.5): a. Move a child of w to v. b. Move a key from w to u. c. Move a key from u to v.
  • 6. This may seem a little cryptic, but one look at the figure should make this quite clear. As you can see, this operation has the following effects: q It adds a child to v and removes one from w (thereby making v a 2- node and w a 2 or 3-node, resolving the underflow at v). ) But now we must transfer the corresponding keys without destroying the ordered nature of the tree. We do this by pushing the extra key from w through the appropriate key in the parent u, and finally into v. . The net effect is that the number of keys in w has been reduced by 1, and the number of keys in v has been increased by 1 as desired! Figure 6.5: deletion of key 4 resulting in an underflow at v. (a) transfer operation, (b) the resulting tree after the transfer. ΙΙ. v has no such siblings (i.e., they are all 2-nodes). This case requires a FUSION of two nodes as follows (figure 6.6): a. Merge v with a 2-node sibling w creating a new node v'. b. Move a key from u (v's parent) to v'.
  • 7. After step (a), v' has 3 children, but stores only one key, and u has lost a child (since two of it's children merged into one), hence we move a key from u to v' to preserve the tree. Note that the FUSION operation reduces the number of u's children by 1, potentially causing an underflow at u. In such a case we would remedy this with another FUSION or TRANSFER. Observe also that this process also terminates at the root (an underflow at the root simply causes its deletion), and is hence bounded above by O(logn) as was the SPLIT operation in the INSERTION algorithm. Figure 6.6: deletion of key 11 resulting in an underflow at v. (a) fusion of v and w, (b) the resulting tree after the fusion. Almost done! However, remember that everything above only applies if the key we're trying to remove is stored at a node with only external nodes for children (if you don't remember making this assumption click here)! What do we do if this is not the case? Well, there just so happens to be a very easy way to swap any key in a (2,4)-tree with one that is stored in a node with the property we desire without destroying the ordering in the tree. This is done as follows: Swap the internal key ki with the largest element in the subtree immediately to its left. By the definition of a search tree, this key is the next smallest key in the tree next to ki. So once we delete ki, the tree will be correctly ordered.
  • 8. A natural question to ask would be "how do we find this element?" This is accomplished by performing the following steps: 1. Let v be the internal node in which the element we wish to delete (ki) is stored. 2. Let w be the right-most internal node in the subtree rooted at the ith child of v. 3. Swap ki with the last item of w. Once this is done, the item we wish to delete will be at a node that has only external nodes for children, and we will be able to use the above procedure to delete it. 6.8.2 Analysis of deletion • We began with a search procedure which takes O(logn) time. • This may be followed with a swap which also takes O(logn). • Finally, we had a maximum total of O(logn) TRANSFER or FUSION operations (each of which takes constant time). Therefore, deletion can also be accomplished in (2,4)-trees in O(logn) time.