2-3-4 trees

Source: Internet
Author: User

2-3-4 is a type of B tree with a level of 4. It is a self-balancing data structure that can be searched, inserted, and deleted in O (lgn) time. Here N is the number of elements in the tree. 2-3-4 trees are equivalent to the red-black tree, that is, each red-black tree can be converted into a 2-3-4 tree. Each selection operation also corresponds to the split operation in the 2-3-4 tree.

The 2-4-4 tree is a data structure that meets the following requirements:

1) each node has 1, 2, or 3 keys, which are called 2-node, 3-node, and 4-node respectively.

2) the keys of each node divide the intervals. Taking 4-nde as an example, key1, key2, and key3 are respectively clipped between subtree1, subtree2, subtree2, subtree3, subtree3, and subtree4.

3) The subnode of 4-node cannot be 4-node.

As shown in:

Search:

1. Start with Root

2. Compare the value of the current node

2.1 If found, the current node is returned.

2.2 If not found, find out which subtree the search value belongs.

3. recursive search subtree

 

The key to insertion and deletion is to maintain the nature. 3) that is, the 4-node cannot be 4-node.

 

Insert key

1. Recursive key search

1.1 If the root is 4-node (ABC), a new root (B), A, and C will be created as its two subtree.

 

1.2 perform a downward search, if it is a 4-node, use the transformation split of example (note that the subnode of 4-node is not generated according to the assumption algorithm, so this operation can always be done)

1.3 if the corresponding key already exists, the algorithm ends and does not need to be inserted

2. Note that if the key does not exist, the recursive search of 1 must be stopped at the leaf node.

3. Insert at the current leaf node

3.1 if it is 2-node or 3-node, insert the current node and change it to 3-node or 4-node. The algorithm ends.

3.2 If it is 4-node, its parent node must not be 4-node (that is, 2-node or 3-node)

3.2.1 split 4-node using the same transform as 1.2

3.2.2 Insert the corresponding node (, must be 2-node)

 

It must be proved that the inbound and outbound algorithms from 2 to 3 must form a balance tree, that is, the length of any leaf starting from root is equal.

Mathematical induction:

1. The tree with only one node is of course balanced.

2. Assume that n elements are inserted, and the tree is still balanced. If a new element is inserted, the balance will not be damaged:

The algorithm will change the values of the tree to 1.1, 1.2, 3.1, and 3.2. Obviously, 1.2, 3.1, and 3.2 do not change the depth of the tree. Considering 1.1, it increases the depth of the tree's path by 1. The original tree is balanced. After the depth increases, it is still balanced.

 

Rough Estimation of the depth of 2-3-4 trees with n elements; the worst case is 2-node, the depth is logn, the best case is 4-node, the depth is logn/2, therefore:

LOgn/2 <depth ≤ logn (left-side brackets are not possible, because the subnode without 4-node is 4-node)

 

 

 

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.