Balanced search tree search tree 2-3

2-3 tree

We are a binary search tree has introduced a binary search tree is likely the worst happens, all of the nodes into a chain. Figure

We certainly hope we can save balanced binary search tree, but to ensure a perfect balance of the cost of the tree is too dynamic insertion. We settled for learning a new data structure.

 

Related definitions:

2- node: contains a key (and corresponding values) and two links, links to the left in the tree key 2-3 is less than the node 2-3 links to the right of the tree are greater than the node key.

3- node: contains two keys (and corresponding values) and three links, links to the left in the tree key 2-3 is less than the node 2-3 in the link to the tree the node keys are located bonds between two points, the right tree links to 2-3 are greater than the node key.

Figure

A perfect 2-3 in all of the search tree from the root to the air links are equal

 

 

Seek

First, we will compare with a key in the root key, and if he is equal to either one, then hit; otherwise find a link to the corresponding region of the link based on the results of the comparison, and continues to look at its subtree pointed. If the link is empty, then search miss.

For finding the situation on the left, we find H, found that he h <m, so we continue to look at the left subtree of M, H between E and J, so we continue to look at neutron tree, hit

In the case of the right, we find B, find B <M ,, will continue to look at the left subtree M, we found B <E, in the left subtree continue searching, found in the B and intermediate A C, Find they neutron tree is empty, miss.

 

 

insert

We divided the discussion in the case of 4

 

Insert a new node key of 2-

We just need to replace the 2- a 3- node to node. As shown, insertion K

It contains a 3- to only one node of the tree to insert a new key.

Before considering the general case, we consider a case, a tree node is only a 3-, we now inserted inside the new key, it temporarily changes a 4- node, the fourth node can be a conveniently converted into 2- 2-3 tree contains three nodes, but only 1 tree height increases. Wherein a three nodes (with) a button, a node containing a minimum of (left link) three keys, a greatest comprising (Right links)

 

Insert a new node key is 3- 2- node to a parent node

我们先构建出一个临时的4-结点并将其分解,但是我们不会为中键创建一个新结点,而是将他移动到原来的父结点中。即将原来指向3-结点的链接替换为新父结点中的原中键左右两边的两条链接。,这种转化不影响2-32树的主要性质

向一个父结点为3-结点的3-结点插入新键

我们和刚才一样构造一颗临时的4-结点然后分解他,将他的中间插入到父结点中,但是父结点也是一个3-结点,因此我们继续构造一个临时的4-结点,继续进行相同的变换。以此类推,直到遇到一个2-结点或者到达3-结点的根

分解根结点

如果从插入结点到根结点的路径上全是3-结点,我们的根结点就变成了一个临时的4-结点,此时我们可以按照向一颗只含有一个3-结点的树中插入新键的方法处理,将树高加1.这次变换仍然保持了树的性质

 

 

全局性质

上面介绍的变换不会影响树的全局有序性和平衡性,任意空链接到根结点的距离都是相等的

 

Guess you like

Origin www.cnblogs.com/lls101/p/11240492.html