2-3-4 Tree of Algorithms

Source: Internet
Author: User

The 2-3-4 tree is a B-Tree of order 4. It is a self-balancing data structure that can be found, inserted, and deleted in O (LGN) time, where n is the number of elements in the tree. 2-3-4 trees and red black trees are equivalent, that is, each red and black tree can be converted into a 2-3-4 tree, and each selection operation is also corresponding to the split operation in the 2-3-4 tree.

The 2-3-4 tree is a data structure that satisfies the following properties:

1) Each node has 1, 2, or 3 keys per node, respectively called 2-node,3-node,4-node.

2) The keys of each node divide the interval, taking 4-nde as an example, Key1, Key2, Key3 are sandwiched between subtree1, Subtree2 and Subtree2, Subtree3 and Subtree3, subtree4 respectively.

3) 4-node's child nodes cannot be 4-node.

As shown in the following:

    

Search:

1. Starting from Root

2. Compare the values of the current node

2.1 If found, returns the current node

2.2 If it is not found, find out which sub-tree the value to search for belongs to

3. Recursive search subtree

the key to insert and delete is to maintain the properties of 3), i.e. the 4-node node cannot be 4-node

Insert key

1. Recursive search key

1.1 If Root is 4-node (ABC), a new root (B) is built, and A,c becomes its two subtree

1.2 down search, for each node passing through, if it is 4-node then use such as the transform spin-off (note that according to the assumption algorithm does not produce 4-node child nodes are 4-node, so this operation is always possible)

1.3 If the corresponding key already exists, the algorithm ends and no insertion is required

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

3. Insert in the current leaf node

3.1 If it is 2-node or 3-node, insert the current node and turn it into 3-node or 4-node, the algorithm ends

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

3.2.1 uses the same transform as 1.2 to split the 4-node

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

To prove that the 2-3-4 above the access algorithm must form a balance tree, that is, from root to the next leaf length is equal.

Using the mathematical induction method:

1. Only one node of the tree is of course balanced

2. Assuming that n elements are inserted, the tree is balanced and now inserts a new element to prove that the balance is not broken:

The algorithm will change the tree to be 1.1, 1.2, 3.1, 3.2. Obviously 1.2, 3.1, 3.2 will not change the depth of the tree, consider 1.1, it makes the path depth of the tree 1, the original tree is balanced, the depth of the increase is of course balanced.

A rough estimate of the depth of the 2-3-4 tree with n elements; the worst case is all 2-node, then the depth is Logn, the best case is 4-node, the depth is LOGN/2, so:

lOGN/2 < Depth≤logn (the left parenthesis is not possible, because a child node that does not exist 4-node is 4-node).

To learn more about the code implementation, please go to http://www.cnblogs.com/guoyiqi/archive/2011/06/08/2129310.html. I'm just mentioning some conceptual stuff here.

2-3-4 Tree of Algorithms

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.