2-3 Tree insertion and deletion operations

1. Definition

The 2-3 search tree is a kind of balanced search tree, that is to say, the height difference between the left and right subtrees does not exceed 1 , which is defined as follows:

  1. Either empty, or:

  2. For 2 nodes, the node saves a key and corresponding value, and two nodes pointing to the left and right nodes. The left node is also a 2-3 node, and all values ​​are smaller than the key. The right node is also a 2-3 node. All The value is larger than the key

  3. For 3 nodes, the node holds two keys and corresponding values, and three nodes pointing to the left, middle and right. The left node is also a 2-3 node, and all values ​​are smaller than the smallest key among the two keys; the middle node is also a 2-3 node, and the key value of the middle node is between the two and the node key value; The right node is also a 2-3 node, and all key values ​​of the node are greater than the largest key of the two keys

If you traverse the 2-3 search tree in order, you can get the sorted sequence. In a perfectly balanced 2-3 search tree, the root node is the same distance from every empty node.

write picture description here

2. Find

write picture description here

3. Insert

Before inserting, do a miss lookup on the 2-3 tree

1. Insert a node into the 2 node

If the miss lookup ends with a 2-node, simply replace the 2-node with a 3-node and save the key to be inserted in it.
write picture description here

2. Insert nodes into 3 nodes

① The parent node is 2 nodes

A new element can be inserted into the 3-node node, making it a temporary 4-node node, and then the intermediate element in the node can be promoted to the parent node, the 2-node node, making its parent node a 3-node node, and then hang the left and right nodes in the appropriate position of the 3-node node. The operation is as follows: .

write picture description here

②The parent node is 3 nodes

When the node we insert is a 3-node, we split the node and promote the intermediate element to the parent node, but at this time the parent node is a 3-node node, after insertion, the parent node becomes a 4-node node , and then continue to promote the middle element to its parent node until it encounters a parent node that is a 2-node node, then it becomes a 3-node node, no further splitting is required.

write picture description here

③The parent node to the root node are all 3 nodes

When the parent node to the root node are all 3 nodes, this is if we want to insert a new element at the byte point, it will always check to the follower node. In the last step, the follower node becomes a 4 node. , at this time, it is necessary to divide the follow node into two 2 nodes, and the height of the tree is increased by 1. The operation process is as follows:

write picture description here

3. Transform

For the deformation of 4 nodes to 2-3 nodes, the height of the tree does not change before and after the deformation. Only when the root node is 4 nodes, the height of the tree after deformation is increased by one. Transformations do not affect the global order and balance of the tree. As shown below:

write picture description here

3. Delete

1. Delete non-leaf nodes

Use the direct successor node key under in-order traversal to overwrite the current node key, and then delete the successor node key used to overwrite

write picture description here

2. Delete leaf nodes

①Delete node is not 2 nodes, delete it directly

write picture description here

②Deleted node is 2 nodes

a: The parent node of the current node is 2 nodes and the sibling node is 3 nodes, move the parent node to the current position, and then move the key of the sibling node closest to the current position to the parent node

write picture description here

b: The parent node of the current node is 2 nodes, and the sibling node is also 2 nodes. First, directly drive to the sibling node through the in-order traversal of the moving sibling node, so that the sibling node becomes 3 nodes; then perform the operation of a

write picture description here

c: The parent node of the current node is 3 nodes, split the parent node to make it a 2 node, and then merge the closest split key in the parent node with the middle child, and use the merged node as the current node

write picture description here

d: The 2-3 tree is a full binary tree. The 2-3 tree layer tree is reduced, and the sibling nodes are merged into the parent node. At the same time, all sibling nodes of the parent node are merged into the parent node of the parent node. If generated 4 nodes, and then decompose 4 nodes.

write picture description here

Reference:
1. Robert Sedgewick. Fourth Edition of Algorithms [M]. Beijing: People's Posts and Telecommunications Press, 2012.10
2. Dahua Data Structure

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326045952&siteId=291194637