[To] talk about algorithms and data structures: eight-balance search tree 2-3 trees

Source: Internet
Author: User

The two-fork search tree is described earlier, and he has no problem with the efficiency of finding and inserting in most cases, but he is less efficient in the worst case. The data structure of the balanced lookup tree introduced in this article and later in this article ensures that the LGN efficiency can be achieved in the worst case, and we need to make sure that the tree remains in equilibrium after the insert is complete, which is the Balanced search tree. In a tree with n nodes, we want the height of the tree to be maintained at LGN so that we can find the desired value only if we need to lgn the comparison operation. Unfortunately, it is too expensive to maintain the balance of the tree after each element insertion. So here are some new data structures to ensure that in the worst case, insertion and lookup efficiency are guaranteed to be done in logarithmic time complexity. This article first introduces the 2-3 find tree (2-3 search trees), which will be followed by the introduction of red and black trees and B-trees.

Defined

Unlike a two-fork tree, 2-3 trees run each node to save 1 or two values. For the normal 2 node (2-node), he saves 1 keys and left and right two points. corresponding to 3 nodes (3-node), save two key,2-3 find tree is defined as follows:

1. Either empty, or:

2. For 2 nodes, the node holds a key and corresponding value, and two points to the left and right nodes, and a 2-3 node, all the values are more effective than key, there is a node is a 2-3 node, all the value is larger than key.

3. For the 3 node, the node holds two keys and corresponding value, and three points to the left and right nodes. The left node is also a 2-3 node, all values are smaller than the smallest key in two key, the middle node is also a 2-3 node, the key value of the middle node is between two and the node key value, the right node is also a 2-3 node, All key values for a node are larger than the largest key in the two key.

If the middle order traverses the 2-3 lookup tree, the ordered sequence can be obtained. In a fully balanced 2-3 lookup tree, the root node is the same distance from each of the empty nodes.

Find

Before we proceed to the balance of the 2-3 tree, we first assume that we are in equilibrium, and we look at the basic lookup operation first.

2-3 Tree Lookup is similar to a two-fork lookup tree, to determine whether a tree belongs to a 2-3 tree, we first compare it with the node, and if it is equal, the lookup succeeds; otherwise, based on the condition of the comparison, it is found recursively in its left-right subtree, if the found node is empty, it is returned. The lookup process is as follows:

Insert into a 2-node node

Inserting an element into a 2-3 tree and inserting an element into a binary lookup tree, you first look for it, and then you hang the node on the node that is not found. The reason why 2-3 trees can guarantee efficiency in the worst case is that they remain balanced after insertion. If a node that is not found after the lookup is a 2-node node, then it is easy to simply put the new element into the 2-node node and make it a 3-node node. But if the found node ends up in a 3-node node, it can be a bit of a hassle.

Insert into a 3-node node

Inserting a new node into a 3-node node may encounter many different situations, starting with a simple tree that contains only one 3-node node.

contains only a single 3-node node

For example, suppose that the 2-3 tree contains only one 3-node node, which has two keys and no space to insert a third key, the most natural way is to assume that the node can hold three elements, temporarily make it a 4-node node, and he contains four child nodes. We then promote the middle element of this 4-node node, the left node as its left node, and the right element as its right node. Insert complete, change to balance 2-3 find tree, tree height changed from 0 to 1.

node is 3-node , the parent node is 2-node

As in the first case, we can also insert a new element into the 3-node node, make it a temporary 4-node node, and then promote the intermediate element in that node to the parent node, the 2-node node, so that its parent node becomes a 3-node node. The left and right nodes are then hung in the proper place on the 3-node node, respectively. Operations such as:

node is 3-node , the parent node is also 3-node

When we insert the node is 3-node, we split the node, the intermediate element to the parent node, but at this point the parent node is a 3-node node, after the insertion, the parent node becomes the 4-node node, and then continues to promote the intermediate element to its parent node. Until you encounter a parent node that is a 2-node node and then turn it into 3-node, you do not need to continue splitting.

Root node splitting

When the root node to the byte point is the 3-node node, this is if we want to insert a new element in the byte point, will always check the node, and in the last step, with the node into a 4-node node, this time, it will need to be divided into two 2-node nodes, The height of the tree is added to 1, this operation is as follows:

Local conversion

Splitting a 4-node into 2-3node involves 6 possible operations. This 4-node may be in the same node as the left or right child node of the 2-node. Or a 3-node left, middle, and right child node. All of these changes are local, and there is no need to check or modify other parts of the node. So it takes only a few operations to complete the 2-3-tree balance.

Properties

These local operations maintain a 2-3-tree balance. For 4-node nodes, the height of the tree is not changed before and after the deformation of the 2-3 node. Only if the node is a 4-node node, the height of the tree is added one after the deformation. As shown in the following:

Analysis

For a fully balanced 2-3 lookup tree, the distance from each root node to the leaf node is the same:

2-3 Tree Search efficiency is closely related to the height of the tree.

    • In the worst case, that is, all nodes are 2-node nodes, and the lookup efficiency is LGN
    • In the best case, all nodes are 3-node nodes, the lookup efficiency is log3n approximately equal to 0.631lgN

Distance, for 1 million nodes of 2-3 trees, the height of the tree is 12-20, for 1 billion nodes of 2-3 trees, the height of the tree is between 18-30.

For inserts, it takes only a few operations to complete, because he only needs to modify the nodes associated with that node, and does not need to check other nodes, so efficiency and lookup are similar. Here is the efficiency of the 2-3 lookup tree:

Realize

The direct implementation of the 2-3 tree is more complex because:

    1. Need to handle different node types, very cumbersome
    2. A multiple comparison operation is required to move the node down
    3. Need to move up to split the 4-node node
    4. There are a number of scenarios for splitting 4-node nodes

2-3 Find trees are more complex to implement, and in some cases the balancing operation after insertion may result in reduced efficiency. The improved red-black tree based on the 2-3 find tree is not only more efficient, but also easier to implement than the 2-3 lookup tree.

But the 2-3 search tree as a more important concept and idea is very important to the red and black trees and B-trees to be talked about later. Hopefully this article will help you understand the 2-3 find tree.

[to] talk about algorithms and data structures: eight-balance find tree 2-3 trees

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.