2-3 tree implementation analysis

Source: Internet
Author: User

In 2-3 trees, each internal node (non-leaf node) has two or three children, and all the leaves are on the same level. For example, figure 1 shows 2-3 trees with a height of 3. The nodes that contain two children are called 2-nodes, and the nodes in the binary tree are both 2-nodes. The nodes that contain three children are called 3-nodes.


Figure 1: 2-3 trees with a height of 3
2-3 trees are not binary trees, and their nodes can have three children. However, 2-3 trees are similar to full Binary Trees. If a tree contains no 3-nodes, it looks like a full binary tree. All its internal nodes can have two children, and all the leaves are at the same level. On the other hand, an internal node of 2-3 trees does have three children, so there are more nodes than full Binary Trees of the same height. 2-3 trees with a height of H contain more than the number of nodes that are equal to H, that is, at least 2 ^ h-1 nodes. For another angle analysis, the height of 2-3 trees Containing N nodes is not greater than that of [log2 (n + 1)] (that is, the minimum height of a Binary Tree Containing N nodes ).
We can see that 2-3 trees can be used to implement the ADT list. If a node is sorted by two or three trees and becomes a search tree, it can be used to implement the ADT table. The following is a recursive definition of 2-3 trees, which specifies the node sequence.
If one of the following conditions is met, T is a 2-3 tree:
(1) T is null, that is, 2-3 trees with a height of 0.
Or
(2) T form:

Where R is a node that contains a data item, TL and TR are two-3 trees high for the H-1. In this case, the search keyword in R must be greater than the search keyword in the subtree TL and smaller than the search keyword in tr.
Or
(3) T format:

R is a node that contains two data items, while TM, Tl, and TR are two-3 trees with a high h-1. In this case, the smaller search keyword in R must be greater than each search keyword in the left subtree TL and smaller than each search keyword in the seed number TM. The larger search keyword in R must be greater than each search keyword in the neutron tree TM and smaller than each search keyword in the right subtree tr.
According to this definition, a rule can be introduced to put data items into 2-3 Tree nodes:
(1) 2-the node has two children and must contain one data item. The search keyword is greater than the Search Keyword of the left child and less than the Search Keyword of the right child, as shown in 2-:
(2) The 3-node has three children and must contain two data items. The search keywords S and l meet the following conditions: S is greater than the Search Keyword of the left child, less than the Search Keyword of the child in the middle, more than the Search Keyword of the child in the middle, and less than the Search Keyword of the right child. 2-B:
(3) A leaf can contain one or two data items.

(A) 2-node (B) 3-node

Figure 2: 2-3 Tree nodes;


Figure 3: 2-3 trees
In this way, the items in the 2-3 tree are sorted by the search keyword. For example, figure 3 is a 2-3 tree.
The following C ++ statement can be used to represent any node in the 2-3 tree:

[CPP]
View plaincopyprint?
  1. Class treenode
  2. {
  3. PRIVATE:
  4. Treeitemtype smallitem, largeitem;
  5. Treenode * leftchildptr, middlechildptr, rightchildptr;
  6. Friend class twothreetree;
  7. };
class TreeNode{private:TreeItemType smallItem,largeItem;TreeNode *leftChildPtr,middleChildPtr,rightChildPtr;friend class TwoThreeTree;};

When a node contains only one data item, you can add it to smallitem and use leftchildptr and middlechildptr to point to the child of the node. For security, you can put null in rightchildptr.
The following describes how to traverse, search, insert, and delete 2-3 trees. These operations use recursive algorithms. By defining the basis examples of these recursive algorithms as leaves rather than empty subtree, we can avoid interference with actual details. In this case, the algorithm must assume that the empty tree cannot be passed to them as parameters.
Traverse 2-3 trees
You can use a method similar to ordinal traversal. Traverse 2-3 trees in order of search keywords

[CPP]
View plaincopyprint?
  1. Inorder (in tttree: twothreetree)
  2. // Traverse the noneempty 2-3 tree, tttree in sorted
  3. // Search-key order
  4. {
  5. If (tttree's root node R is a leaf)
  6. Visit the data items
  7. Else if (R has two data items)
  8. {
  9. Inorder (left subtree of tttree's root );
  10. Visit the first data item
  11. Inorder (middle subtree of tttree's root );
  12. Visit the last data item
  13. Inorder (right subtree of tttree's root );
  14. }
  15. Else
  16. {
  17. Inorder (left subtree of tttree's root)
  18. Visit the first data item
  19. Inorder (middle subtree of tttree's root );
  20. }
  21. }
inorder(in ttTree:TwoThreeTree)//Traverse the noneempty 2-3 tree ,ttTree in sorted//search-key order{if(ttTree's root node r is a leaf)visit the data itemselse if(r has two data items){inorder(left subtree of ttTree's root);visit the first data iteminorder(middle subtree of ttTree's root);visit the last data iteminorder(right subtree of ttTree's root);}else{inorder(left subtree of ttTree's root)visit the first data iteminorder(middle subtree of tttree's root);}}

Search for 2-3 trees
The node sorting in the Two-3 trees is similar to that in the binary tree. You can search for an item in the Two-3 trees. In fact, we can see from the following pseudo code that 2-3 tree search operations are very similar to binary tree search operations.

[CPP]
View plaincopyprint?
  1. Retrieveitem (in tttree: twothreetree, in searchkey: keytype,
  2. Out treeitem: ttreeitemtype): boollean
  3. // Retrieve into treeitem from a noneempty 2-3 tree tttree
  4. // The item whose search key equals searchkey, the operation fails if no
  5. // Such item exists, the function returns true if the item is found,
  6. // False otherwise
  7. {
  8. If (searchkey is in tttree's root node R)
  9. {
  10. Treeitem = the data portion of R
  11. Return true;
  12. }
  13. Else if (R is a leaf)
  14. {
  15. Return false;
  16. }
  17. Else if (R has two data items)
  18. {
  19. If (searchkey <smaller search key of R)
  20. Return retrieve (R's left subtree, searchkey, treeitem)
  21. Else if (searchkey <larger search key of R)
  22. Return retrieve (R's middle subtree, searchkey,
  23. Treeitem );
  24. Else
  25. Return retrieve (R's right subtree, searchkey,
  26. Treeitem)
  27. }
  28. Else
  29. {
  30. If (searchkey <R's search key)
  31. Return retrieve (R's left subtree, searchkey, treeitem );
  32. Else
  33. Return retrieve (R's middle subtree, searchkey, treeitem );
  34. }
  35. }
retrieveItem(in ttTree: TwoThreeTree,in searchKey :keyType,out treeItem:TTreeItemType):boollean//retrieve into treeItem from a noneempty 2-3 tree ttTree//the item whose search key equals searchkey,the operation fails if no//such item exists ,the function returns true if the item is found ,//false otherwise{if(searchKey is in ttTree's root node r){treeItem =the data portion of rreturn true;}else if(r is a leaf){return false;}else if(r has two data items){if(searchKey<smaller search key of r)return retrieve(r's left subtree,searchkey,treeitem)else if(searchKey<larger search key of r)return retrieve(r's middle subtree ,searchKey,treeItem);elsereturn retrieve(r's right subtree,searchKey,treeItem)}else{if(searchKey<r's search key)return retrieve(r's left subtree,searchKey,treeItem);elsereturn retrieve(r's middle subtree,searchKey,treeItem);}}

Insert Algorithm
To insert item I into a 2-3 tree, first locate the leaf that will be terminated by the search I operation. Insert new item I into the leaf. If the current leaf contains two items, the task is completed. However, if the leaf contains three items, it must be divided into two nodes, N1 and N2. 4. Place the minimum item s on N1, the condition item on N2, and move the Middle item M to the parent of the leaf. As a result, nodes N1 and N2 become both parent children. If the parent has only three children and contains two projects, the task is completed. However, if the parent has four children and three are included, You need to split them.


Figure 4: Split leaves in 2-3 trees
Through the leaf operation process above, split the internal node N containing three items, but the four children of N must be considered. 5. Split N into N1 and N2, place the minimum s of N to N1, and associate the two children on the left of N to N1. Place the limit Item L of N into N2, associate the two children on the Right of N to N2, and move the intermediate m of N to the parent of N.


Figure 5: splitting 2-3 Internal nodes
After that, the process of splitting a node and moving an item to a parent node is recursively performed, knowing that a node has only one item before being inserted, and there are only two items after accepting a new item. Note that in the previous insert sequence, the height of the tree remains unchanged, and it is always the original 3. Generally, as long as at least one node contains only one entry in the path from the root to the leaf of the new item inserted, the insertion will not increase the height of the tree. Therefore, compared with the basic binary tree search policy, the insert policy of 2-3 trees delays the growth of the tree height.
When the height of 2-3 trees increases, it completes from the top down of the item. If each node contains two items from the root to the leaf path of the inserted new item, the height of 2-3 trees will increase. In this case, the recursive process of splitting the node and moving the item to the parent node finally reaches the root R. At this time, like any other internal node, it must be split into R1 and R2 for R. However, you must create a node that contains the intermediate items of R. The node is the parent node of N1 and N2. As a result, the new node becomes a new tree, as shown in figure 6.


Figure 6: Split the root of 2-3 trees

The following algorithm summarizes the entire insert policy:

[CPP]
View plaincopyprint?
  1. Insertitem (in tttree: twothreetree, in newitem: treeitemtype)
  2. // Insert newitem into a 2-3 tree tttree whose items have
  3. // Distinct search keys that differ from newitem's search key.
  4. {
  5. Let's key be the search key of newitem
  6. Locate the leaf leafnode in which search key belongs
  7. Add newitem to leafnode
  8. If (leafnode now has three items)
  9. Split (leafnode );
  10. }
  11. Split (inout N: treenodek)
  12. // Splits node N, which contains 3 items, node: If n
  13. // Is not a leaf, it has 4 children. Throws treeexception
  14. // If node allocation fails.
  15. {
  16. If (n is the root)
  17. Create a new nodes P (refine later to throws exception)
  18. If (allocate failed)
  19. Throw
  20. Else
  21. Let p be the parent of the N
  22. Replaces node N with two node, N1 and N2, so that P
  23. Is their parent
  24. Give N1 the item in N with the smallest search-key value
  25. Give N2 the item in N with the largest search-key value
  26. If (N is not a left)
  27. {
  28. N1 become the parent of n's two leftmost children
  29. N2 become the parent of n's two rightmost children
  30. }
  31. Move the item in n that has the middle search key value
  32. Up to P
  33. If (P now has three items)
  34. Split (P );
  35. }
insertItem(in ttTree::TwoThreeTree,in newitem :TreeItemType)//insert newitem into a 2-3 tree ttTree whose Items have//distinct search keys that differ from newitem's search key.{Let's key be the search key of newitemLocate the leaf leafNode in which search key belongsAdd newitem to leafnodeif(leafNode now has three items)split(leafnode);}split(inout n:TreeNodek)//splits node n ,which contains 3 items ,Node :if n//is not a leaf ,it has 4 children .throws treeException//if node allocation fails.{if(n is the root)create a new nodes P(refine later to throws exception)if(allocate failed)throw elseLet P be the parent of the nReplaces node n with two node ,n1 and n2,so that p is their parentGive n1 the item in n with the smallest search-key valueGive n2 the item in n with the largest search-key valueif(n is not a left){n1 become the parent of n's two leftmost childrenn2 become the parent of n's two rightmost children}Move the item in n that has the middle search key value up to pif(p now has three items)Split(p);}

Delete An Algorithm
In short, to delete an I entry from the 2-3 tree, first locate the node n that contains it. If n is not a leaf, search for the intermediate sequence of I and exchange the intermediate sequence of I. After the switch, the deletion always starts from the leaf. If the leaf contains items other than I. You only need to delete I to complete the task. However, if the leaf only contains I, deleting I will result in I not containing the leaf of the data item. In this case, you must perform other operations to complete the deletion.
First, check the brothers with empty leaves. If one of his/her siblings has two items, the data items are re-allocated between his/her siblings, empty leaves, and leaves, as shown in 7. If the leaf brother does not have two items, move one item from the parent of the leaf to the brother (before, he has one item, so there is space for placing another item ), and delete the empty leaves to merge the leaves and the adjacent brothers, as shown in 7-B.

As mentioned above, moving one item down from node N may result in node n not including data items, and there is only one child. In this case, the delete algorithm is applied to N recursion. If a brother of N contains two items and three children, the item is reassigned between node N, brother and parent. In addition, give a brother's child N, as shown in 7-C.

If neither of the N's siblings has two items, merge N with the sibling, as shown in 7-D. In other words, move one item down from the parent and make the sibling accept N's one child (the former sibling only has one item and two children), and then delete the empty leaf. If n's parent has no items, the deletion process is applied recursively.

If you continue to merge, there will be no root item and only one child. In this case, you can simply delete the root. To perform this step, the height of the tree is reduced by 1, 7, and E.


Figure 7: (a) reallocation value; (B) merging leaves; (c) reallocation value and leaves; (d) merging internal nodes; (e) Deleting Root
The following is an advanced statement of the algorithm for deleting an item from the 2-3 tree:

[CPP]
View plaincopyprint?
  1. Deleteitem (in tttree: twothreetree, in searchkey: keytype)
  2. Throw (exception)
  3. // Deletes from the 2-3 tree tttree the item whose search key
  4. // Equals searchkey, throws exception if no such item exists
  5. {
  6. Attempt to locate item theitem whose search key equals
  7. To searchkey
  8. If (theitem is not in a leaf)
  9. {
  10. Swap item theitem with its inorder successor,
  11. Which will be in a leaf leafnode
  12. // The deletion always begins at a leaf
  13. Delete item theitem from leaf leafnode
  14. If (theleaf now has no items)
  15. Fix (theleaf );
  16. }
  17. Else
  18. Throws exception;
  19. }
  20. Fix (in N: treenode)
  21. // Completes the deletion when node N is empty by either
  22. // Removing the root, redistributing values or merging
  23. // Nodes. node: If n is internal, it has one child
  24. {
  25. If (n is the root)
  26. Remove the root
  27. Else
  28. {
  29. Let p be the parent of N
  30. If (some sibling of N has two items)
  31. {
  32. Distribute items appropriately among N
  33. , The sibling and P
  34. If (N is internal)
  35. Move the appropriate child from sibling
  36. To N
  37. }
  38. Else // merge the node
  39. {
  40. Choose an adjacent sibling s of N
  41. Bring the appropriate item down from P
  42. Into S
  43. If (N is internal)
  44. Move n's child to S
  45. Remove node n
  46. If (N is now empty)
  47. Fix (P );
  48. }
  49. }
  50. }
  51. From: http://blog.csdn.net/chenloug/article/details/6998527

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.