2-4-4 trees

Source: Internet
Author: User

2-3-4 treesIn computer science, it is a B-tree of order 4.

Like Tree B, tree 2-3-4 is a self-balancing data structure that can be used as a dictionary. It can beN) Search, insert, and delete data within the specified time.NIs the number of elements in the tree.

2-3-4 trees are relatively difficult to implement in most programming languages, because operations on trees involve a large number of special cases. The implementation of the Red-black tree is simpler, so it can be used instead.

Http://en.wikipedia.org/wiki/2%E2%80%933%E2%80%934_tree)

In a binary tree, each node has one data item and can have up to two children. If we allow more data items and children per node, the result is a multiway tree.

The 2, 3, and 4 in the name 2-3-4 tree refer to how many links to child nodes can potentially be contained in a given node. for non-leaf nodes, three arrangements are possible:
1. A node with one data item always has two children
2. A node with two data items always has three children
3. A node with three data items always has four children

In a 2-3-4 tree, on the other hand, nodes with a single link are not permitted. A node with one data item must always have two links, unless it's a leaf, in which case it has no links.
(Because less than 4 is in a node and more than 4 is split)
(In a binary tree, a node can have a maximum of two subnode links. Of course, it can have only one link, pointing to its left subnode or right subnode. Its other link can be a null value. However, only one link is not allowed in 2-3-4 trees.. A node with one data item must always have two links, unless it is a leaf node and there is no link in that case.
A node with two links is called a 2-node, and a 3-node is called a 3-node. A 4-node is called a 4-node, but it is not a 1-node)

Insertion
New data items are always inserted in leaves, which are on the bottom row of the tree. if items were inserted in nodes with children, then the number of children wowould need to be changed to maintain the structure of the tree, which stipulates that there shoshould be one more child than data items in a node.
(New data items are always inserted into leaf nodes at the bottom of the tree. If it is inserted into a node with a subnode, the number of the subnode will change to maintain the tree structure. This ensures that the subnode of the node is 1 more than the data item .)

The process begins by searching for the appropriate leaf node. if no full nodes are encountered during the search, insertion is easy. when the appropriate leaf node is reached, the new data item is simply inserted into it.

Insertion may involve moving one or two other items in a node so the keys will be
In the correct order after the new item is inserted. InThis example the 23 had to be
Shifted right to make room for the 18.

(3)
Node splits
Insertion becomes more complicated if a full node is encountered on the path down to the insertion point. when this happens, the node must be split. it's this splitting process that keeps the tree balanced. the kind of 2-3-4 tree we're re discussing here is often called a top-down 2-3-4 tree because nodes are split on the way down to the insertion point.
(If you look down at the path where you want to insert the node, the insertion will become complicated. In this case, the node must be split ). This split process ensures the tree balance. The 2-3-4 tree discussed here is a top-down 2-3-4 tree, because the node is split when the insertion point is located down .)

Split when the node is full,

Let's name the data items in the node that's about to be split A, B, and C. Here's
What happens in a split. (We assume the node being split is not the root; we'll
Examine splitting the root later .)
• A new, empty node is created. It's a sibling of the node being split, and is
Placed to its right.
• Data item C is moved into the new node.

• Data item B is moved into the parent of the node being split.
• Data item A remains where it is.
• The rightmost two children are disconnected from the node being split and
Connected to the new node.
An example of a node split is shown in Figure 10.5. Another way of describing
Node split is to say that a 4-node has been transformed into two 2-nodes.

Figure 10.5 splitting a node.
Notice that the effect of the node split is to move data up and to the right. It is this
Rearrangement that keeps the tree balanced.
Here the insertion required only one node split, but more than one full node may be
Encountered on the path to the insertion point. When this is the case, there will be
Multiple splits.

 

Splitting the root
When a full root is encountered at the beginning of the search for the insertion
Point, the resulting split is slightly more complicated:

Search for the insertion
Point, the resulting split is slightly more complicated:
• A new root is created. It becomes the parent of the node being split.
• A second new node is created. It becomes a sibling of the node being split.
• Data item C is moved into the new sibling.
• Data item B is moved into the new root.
• Data item A remains where it is.
• The two rightmost children of the node being split are disconnected from it
And connected to the New Right-hand node.

Figure 10.6 shows the root being split. This process creates a new root that's at
Higher level than the old one. Thus, the overall height of the tree is increased
One. Another way to describe splitting the root is to say that a 4-node is split
Three 2-nodes.

Figure 10.6 splitting the root.
Following a node split, the search for the insertion point continues down the tree. In
Figure 10.6, the data item with a key of 41 is inserted into the appropriate leaf.

 

 

Splitting on the way down
Notice that, because all full nodes are split on the way down, a split can't cause an effect that ripples back up through the tree. the parent of any node that's being split is guaranteed not to be full, and can therefore accept data item B without itself needing to be split. of course, if this parent already had two children when its child was split, it will become full. however, that just means that it will be split when the next search encounters it.
(Note: because all full nodes are split in the next line, it is impossible for the split to return to and to the nodes on the tree. The parent node of any node to be split is certainly not full. Therefore, data item B can be inserted without splitting the node. Of course, if the parent node has two child nodes when it is split, it will become full. However, this only means that the split is required for the next search .)

 

Http://blog.sina.com.cn/s/blog_441997d20100ehar.html

Http://blog.chinaunix.net/uid-23629988-id-3152495.html? Page = 2

Http://blog.csdn.net/v_JULY_v/article/details/6531399

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.