The2-3 treeIs also a search tree like the binary search tree, but this tree tries to solve the problem ofUnbalanced tree.
Imagine that you have a binary tree to store your data. The worst possible case for the binary tree is that all of the data is enteredIn order. Then the tree wowould look like this:
This tree has basically turned into a linked list. This is definitely a problem, for with a tree unbalanced like this, all of the advantages of the Binary Search Tree disappear: searching the tree isSlow and cumbersome, And there is muchWasted memoryBecause of the Empty left child pointers.
A waste of storage space.
The 2-3 tree tries to solve this by using a different structure and slightly different adding and removing procedure to help keep the tree more or less balanced. the biggest drawback with the 2-3 tree is that it requires more storage space than the normal binary search tree.
The 2-3 tree is called such because the maximum possible number of children each node can have is either 2 or 3. this makes the tree a bit more complex, so I will try to explain as much as possible.
One big difference with the 2-3 tree is that each node can have up to two data fields. You can see the three children extending from between the two data fields.
A 2-3 tree has the following nature: A node contains one or two key codes, and each internal node has two children (if it contains a key code ), or three children (including two key codes); All leaf nodes are on the same layer of the tree, so the tree is always highly balanced. The values of all subsequent nodes in the left subtree of each node in the 2-3 tree are smaller than those of the first key code of the parent node; in the middle subtree, the values of all subsequent nodes are greater than or equal to the value of the first key code of its parent node and smaller than the value of the second key code. If there is a right subtree, all successor nodes in the right subtree are greater than or equal to the second key code value of the parent node. 2-3 tree node definition: struct node {int lkey, rkey, numkeys; struct node * left, * Center, * Right ;}; (transferred from http://baike.baidu.com/link? Url = uLn9rjdrcao-itw7tbYdsExJCnmvVXaSxhjYrxSMq4azcDtc39EWYizn03ZUADfqCTq6MzjrASVaI7tRL5wI6K)
Thus, the tree is set up in the following manner:
- The node must always have a first field (Data 1In the digoal), but not necessarily a second field (Data 2). If both fields are present in a node, the first (or left) field of the node is always less than the second (or right) field of the node.
- Each node can have up to three child nodes, but if there is only one data field in the node, the node cannot have more than two children.
- The child nodes are set so that data in the first sub-tree is less than the first data field, the data of the second sub-tree is greater than the first data field and less than the second data field, and the data of the third sub-tree is greater than the second data field. if there is only one data field in the node, use the first and second children only.
- All leaf nodes appear onLastLevel.
Now, take a look at the implementation of a 2-3 tree:
class twoThreeTree { public: twoThreeTree(void); // Constructor ~twoThreeTree(void); // Destructor void add(int item); // Adds an item void search(int item); // Searches for an item private: twoThreeNode *root; // Pointer to root node // Private helper functions go here};class twoThreeNode { public: int firstData, secondData; // Two data fields // The three child nodes twoThreeNode *first, *second, *third; // This next one is quite useful. It aids // moving around the tree. It is a pointer // to the parent of the current node. twoThreeNode *parent;};
You can see that this tree, unlike the binary search tree or the heap tree, has no remove () function. ItIs possibleTo program a remove function, but it generally isn' t worth the time or the effort.
This tree will be a bit harder to implement than the binary search tree just because of the complexity of the node. Still, the add () function algorithm isn'tThatDifficult, and a step-by-step progression through the algorithm helps enormously.
First, to add an item, find the place in the tree for it. this is very much the same as in the binary search tree: just compare and move down the correct link until leaf node is reached.
Once at the leaf node, there are three basic cases for the add () function:
- The leaf node has only one data item:This case is very simple. Just Insert the new item after this item or shift the old item forward and insert the new item before the old one, depending on the situation.
- The leaf node is full and the parent node has only one data item:This case is also quite simple. Compare the three values: the two leaf node items and the new item. Choose the middle one and insert it in the parent node where appropriate.
If the leaf node is the left child, shift the old data in the parent node to the right and insert the middle value in the parent node before the old data.Make sure to shift the pointers to the children in the parent node!If the leaf is the right child, just insert the middle value to the right of the old value in the parent node. the two left-over values from the comparison become two leaf nodes, one as the second child, and one as the first or third child depending on the situation.
- Both the leaf node and the parent node are full:This situation is the most complex of all. in the same manner as Case 2, promote the Middle Value of the leaf node. then, continue doing the same thing with the parent node, and so on until the situation is resolved or the root node is reached. in this case, the middle value is promoted once again, cannot t a new root node is created from the middle value and the two other values become the Left and Right Subtrees of The New Root. at the leaf node at which we began, the new level allows us to split the three initial values into a three node sub-tree of the parent node, and so on.
Doing a few small 2-3 treesHandHelps to understand this algorithm. Remember to check and hold to the rules governing the 2-3 tree! It can get uugly if they are ignored, especially with the last one.
Using some recursive helper functions as well as that miraculous parent variable will help sort the pain of programming this tree.
From: http://www.cprogramming.com/tutorial/computersciencetheory/twothree.html
I found that the above mentioned is too long, look at Wikipedia at a Glance http://en.wikipedia.org/wiki/2%E2%80%933_tree
2-3 tree has two forms:
Search:
Before balancing two or three trees, let's assume that the database is already in a balanced state. Let's first look at the basic search operations.
The search for Two-3 trees is similar to that for the binary search tree. To determine whether a tree belongs to two-3 trees, we first compare it with the node. If it is equal, the search is successful; otherwise, recursive search is performed in the left-right subtree based on the comparison conditions. If the node found is empty, it is not found; otherwise, the result is returned. The search process is as follows:
Insert
Insert data to a 2-node Node
Inserting an element to a tree of 2 to 3 is the same as inserting an element to a binary search tree. First, you need to perform a search and then attach the node to an unfound node. The reason why 2-3 trees can guarantee the efficiency in the worst case is that they can still maintain a balance after being inserted. If the node not found after the search is a 2-node, it is very easy, we only need to put the new element in the 2-node to make it a 3-node. However, if the searched node ends with a 3-node, it may be a bit troublesome.
Insert to a 3-node Node
Inserting a new node to a 3-node may encounter many different situations. The following describes how to insert a new node from a tree that contains only a 3-node.
Contains only one 3-node Node
For example, assume that 2-3 trees contain only one 3-node. This node has two keys and has no space to insert the third key, the most natural way is to assume that this node can store three elements and temporarily turn it into a 4-node with four subnodes. Then, we promote the intermediate elements of the 4-node, the left node as its left node, and the right element as its right node. After the insertion is complete, it turns to a balance of 2-3 search trees, and the height of the tree changes from 0 to 1.
The node is 3-node.The parent node is 2-node.
Like the first case, we can also insert new elements into the 3-node to make it a temporary 4-node. Then, promote the intermediate elements in the node to the parent node, namely the 2-node, to make the parent node A 3-node, then, place the Left and Right nodes in the proper positions of the 3-node nodes. Operations include:
The node is 3-node.The parent node is also a 3-node
When the inserted node is a 3-node, we split the node and upgraded the intermediate element to the parent node. However, the parent node is a 3-node. After the insertion, the parent node is changed to a 4-node, and then the intermediate element is promoted to the parent node until a parent node is a 2-node and is changed to a 3-node, you do not need to continue splitting.
Root node split
When the root node to the byte point is a 3-node, if we want to insert a new element into the byte point, we will keep checking the points with the node, in the last step, the heel node is changed to a 4-node. At this time, you need to identify the heel node as two 2-node nodes, and Add 1 to the height of the tree, the procedure is as follows:
Local Conversion
Splitting a 4-node into 2-3 nodes involves six possible operations. The 4-node may be at the same node, or the Left or Right subnode of the 2-node. Or a 3-node left, center, or right subnode. All these changes are local and do not need to be checked or modified. Therefore, you only need to perform constant operations to achieve the balance between 2-3 trees.
Nature
These local operations maintain a balance of 2-3 trees. If the 4-node is changed to 2-3 nodes, the height of the tree does not change before and after the deformation. Only when the heel node is a 4-node can the height of the tree be increased after deformation. As shown in:
From: http://www.cnblogs.com/yangecnu/p/3624443.html
A ppt about 2-3 tree: http://www.docin.com/p-621379239.html
The question of "2-3 trees" in "B-tree". Hope your predecessors can guide me !!!
Question:
Start from the empty tree, input 20, 30, 50, 52, 60, 68, 70, and draw the process of creating 2-3 trees. And draw the B-Tree State after deleting 50 and 68 respectively.
The book says: Generally, the minimum value is M = 3 (degree, level). At this time, each internal node in the B-tree can have 2 or 3 children, therefore, this level-3 B-tree is called 2-3 trees.
The answer is:
The answer is:
1> [20]
2> [20, 30]
3> [30]
/\
[20] [50]
4> [30]
/\
[20] [50, 52]
5> [30, 52]
/| \
[20] [50] [60]
6> [30, 52]
/| \
[20] [50] [60, 68]
7> [30, 52, 68]
/| \
[20] [50] [60, 70]
8> [52]
/\
[30] [68]
/\/\
[20] [50] [60] [70]
From: http://www.myexception.cn/arithmetic/256189.html
More:
Http://philoscience.iteye.com/blog/1401962
Http://wenku.baidu.com/link? Url = sV9mlLAfUhinrY_rU2FQ9o_azfspWVM9t2ez0DSHSzElUag4otXt_EcIG_Nga3jgJUrnFzeUDovyQApZz8j6QHqSBquNDEZIMss3FbW-ayi