On algorithm and data structure eight balance search tree 2-3 tree

Source: Internet
Author: User
Tags comparison data structures empty

The two-fork lookup tree (Binary) is described earlier, and he has no problem with finding and inserting in most cases, but he is less efficient in the worst case scenario. The data structure of the balanced lookup tree, described in this article and later in this article, ensures that LGN efficiency can be achieved in the worst-case scenario, and to achieve this we need to ensure that the tree remains balanced after the insert is complete, which is the balanced lookup tree (balanced search trees). In a tree with n nodes, we want the height of the tree to remain around LGN, so that we can ensure 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 is inserted. So here are some new data structures to ensure that in the worst case, the insertion and lookup efficiency are guaranteed to be done in logarithmic time complexity. This article first introduces the 2-3 lookup tree (2-3 search trees), which is followed by the introduction of red and black trees and B-trees.

Defined

Unlike two fork trees, 2-3 trees run each node to save 1 or two values. For an ordinary 2-node (2-node), he saves 1 keys and two points for himself. corresponding to the 3 node (3-node), the definition of two key,2-3 lookup trees is as follows:

1. Either empty or:

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

3. For 3 nodes, 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 the two key; the middle node is also a 2-3 node, the middle node key value is between two nodes key value and 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 sequence traverses the 2-3 lookup tree, the sequence can be sorted. In a fully balanced 2-3 lookup tree, the distance between the root node and each empty node is the same.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Find

Before we can balance the 2-3 trees, let's assume we're in equilibrium, and we'll look at the basic lookup operations first.

The 2-3 tree lookup and the two Fork lookup tree are similar, to determine whether a tree belongs to a 2-3 tree, we first compare it to the node, and if it is equal, the lookup succeeds; otherwise, the recursive lookup in the left-right subtree of the comparison, if the node is found empty, is not found, otherwise it is returned. The lookup process is as follows:

Insert

Insert into a 2-node node

Insert an element into a 2-3 tree and insert an element into the binary lookup tree, first look for it, and then hang the node on the node that is not found. The reason why 2-3 trees are able to ensure efficiency in the worst cases is that they remain balanced after they are plugged in. If a node not found after the lookup is a 2-node node, it is easy to just put the new element into the 2-node node to make it a 3-node node. But if the lookup node ends up in a 3-node node, it might be a bit of a hassle.

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.