Data Structures and Algorithms 1-2-3 search trees red-black tree

12-3 search tree

1.1 concept

name Number of key node
Node 2 (2-node) 1 2
Node 2 (2-node) 2 3
  1. Either it is empty , either:
  2. For two nodes , the node stores a key and a corresponding value , and two point nodes around the node, the left node is a 2-3 nodes , than all of the values of key small , the right node is a 2-3 nodes , all value than the larger Key .
  3. For the third node , the node maintains two key and the corresponding value , and the three fingers of the right to the left node. Left node is a 2-3 nodes , all values than in the two key smallest key even smaller ; the intermediate node is a 2-3 node , the intermediate node key value between the two with the key value of the node ; right node is a 2-3 nodes all key values, two key nodes than the bigger the biggest key
    Here Insert Picture Description
    if preorder 2-3 search tree, you can get sorted sequence. 2-3 in a fully balanced search tree, a root node to the distance of each node are the same blank.

1.2 Requirements

1.3 Operation

1.3.1 insert

  1. Into a 2-node tree node is inserted into the insert elements 2-3 and BST insert element, the first lookup, then the nodes linked to the node is not found. If the Find node is found in a 2-node node , simply a new element into the 2-node node inside so that it becomes a 3-node node can
    Here Insert Picture Description

  2. A 3-node to node is inserted into a 3-node node insert a new node may experience a variety of different situations.

  3. There are three cases
    1: a 3-node contains only nodes
    2: 3-node is a node, the parent node is a Node-2
    1: When the inserted node is 3-node time , the split node, the intermediate element up to parent node ,
    2: but this time the parent node is a 3-node node, after insertion into a parent node 4-node, and then continues to the intermediate element up to the parent node ,
    3: is a parent node until it encounters a node 2-node, and then to turn it into 3-node, need not split continued.

3: 3-node is a node, the parent node is 3-node
. 1: when the node is inserted into the 3-node, when the node to enhance the resolution, the intermediate element to the parent node,
2: In this case it is a parent node 3 -node node, after insertion into a parent node 4-node,
3: intermediate element and then continue up to its parent node until it encounters a 2-node is a parent node, then it becomes a 3-node no need to continue to split.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Where there are special circumstances
1: root division

When the root node to the child nodes are 3-node node When this is if you want to insert a new element in the byte point of time, it would have been assigned to check with the node, in the last step of the time, with the node into a 4 -node node , this time, you need to follow the node check is divided into two 2-node node , the tree height plus 1 .

Here Insert Picture Description

1.3.1 Delete

2 red-black tree

2.1 Concepts

  1. BST is a red-black tree with a red and black links
  2. Node is red or black .
  3. Root and leaf nodes (NIL node) are black .
  4. Each red node two child nodes are black . (All paths from the root to each leaf can not have two consecutive red nodes )
  5. From any node to which each of the leaves of all the paths include the same number of black nodes.

2.2 Background

2-3 search tree to ensure that after insertion of the element to maintain balance of the tree, i.e. all the 2-node are child nodes in the worst case, the height of the tree is lgN, thus ensuring the complexity of the worst-case time . But the 2-3 tree is more complicated to implement. Red-black tree is a simple tree data structure implementation 2-3.

Published 27 original articles · won praise 4 · Views 1342

Guess you like

Origin blog.csdn.net/weixin_45639955/article/details/104280200