Binary tree to find predecessor and successor

Note: N represents the current node, P represents the parent node, L represents the left child, and R represents the right child. Empty nodes are nil (or null). The following 1, 2, 3, and 4 are all based on the previous ones, 2 is based on the absence of 1, 3 is based on the absence of both 1 and 2, and so on.

Preorder traversal

Find a pioneer

1. It is the left child of the parent node, and the predecessor is the parent node. As shown in Figure 1
2. It is the right child of the parent node, and the parent node has no left child, and the predecessor is the parent node. As shown in Figure 3
3. It is the right child of the parent node, and the parent node has a left child. The previous parent node is the last node traversed in the previous order. As shown in Figure 2
4. None of the above can be found, the predecessor is nil

Looking for a successor

1. If there is a left child, the successor is the left child, as shown in Figure 4.
2. If there is no left child, and there is a right child, the successor is the right child, as shown in Figure 5.
3. If there is no child node, you need to search according to the following conditions until you find it. .As shown in Figure 6, the dotted line represents that there may be subtrees below. As shown in Figure 6

while(n->p != nil && (n->p->rChild == nil || n->p->rChild == n)){
    n = n->p
}
if(n->p == nil){
    next = nil
}else{
    next = n->p->rChild//如果图中的红点不存在,也可能为空
}

4. If none of the above can be found, it will be nil.

mind Mapping:

inorder traversal

Find a pioneer

1. If there is a left child, the predecessor is the rightmost node. As shown in Figure 7
2. For the right child of the parent node, the predecessor is the parent node. Figure 8
3. For the left child of the parent node, search upwards recursively until the left child of the parent node is not itself. As shown in Figure 9

while(n->p != nil && n->p->lChild == n){
    n = n->p
}
if(n->p == nil){
    pre = nil
}else{
    pre = n->p
}

4. If none of the above can be found, it will be nil. 

 

Looking for a successor

1. There is a right child, and the successor is the right child. As shown in Figure 10

2. The left child of the parent node, the successor is the parent node. As shown in Figure 11.

3. For the right child of the parent node, search upward until the right child of the parent node is not itself. As shown in Figure 12

while(n->p != nil && n->p->rChild == n){
    n = n->p
}
if(n->p == nil){
    next = nil
}else{
    next = n->p
}

4. If none of the above can be found, it will be nil.

mind Mapping

Postorder traversal

Find a pioneer

1. There is a right child node, and the predecessor is the right child.

2. There is a left child but no right child. The precursor is the left child.

3. There is no left or right child, it is the left child of the parent node, search upward until the left child of the parent child is not empty, and the right child of the parent child is itself, then the predecessor is the left child

4. If there are no left and right children, it is the right child of the parent node. If the parent node has a left child, the predecessor is the left child of the parent node.

5. There are no left and right children. It is the right child of the parent node. The parent node has no left child. Search upward until the left child of the parent child is not empty. The predecessor is the left child of the parent node (same as 3).

6. None of the above can be found, it is nil

Looking for a successor

1. It is the left child of the parent node. If the parent node has a right child, it is the first node traversed by the right subtree of the parent node.

2. The left child of the parent node. If the parent node has no right child, it is the parent node.

3. The right child of the parent node is the parent node

Guess you like

Origin blog.csdn.net/weixin_37909391/article/details/112760224