The precursor and subsequent detailed derivation of binary search tree

Source: Internet
Author: User

Successors and precursors

definition: the successor of a node is the node of the minimum keyword greater than x.key.

The precursor of a node is the node of the largest keyword less than x.key.

idea: to find a node of the precursor or successor, is nothing more than in three areas to find.

First analyze the precursor:

Meet two conditions, one is less than the current key value, then only the LP and LS area can be found.

The two requirements are the largest of these values. We know that for LP, X, LS, and Rs all belong to his right subtree, so x, LS, and RS are larger than that.

So obviously, the precursor is the maximum value in LS, the precursor = the maximum value in the left subtree . the condition is that there is a left subtree.

That there is no left subtree only left parents of the situation?

That can only be found on LP, LP also has two parts, the first part is the LP LS,LP LS, although satisfies the condition of less than X, but LP's LS in all elements are less than LP, so at least also LP.

Also, LP may have left parents or right parents, apparently, the right parent is larger than all of his left subtrees, including X, the condition one is not satisfied, obviously not. The left parent is less than LP, so it competes with LP.

So the final conclusion is that in the case of only left parents, there is no left subtree, precursor = left parent's value.

That doesn't exist Zuozi and left parents?

That leaves right and right parents, obviously, right subtree certainly not, all of its elements are greater than X. That can only be found in the right parents, after all, although the right parents are greater than it, but the right parents also have left/right parents and right sub-tree.

Right parents right parents, and right sub-tree are not, are more than the right parents themselves, more than X. That can only be found on the left parents of the right parents, for the left parents, his right subtree is all greater than him, that is, including X's right parents and X, so, at this time to find the left parents is our precursor.

Therefore, there is no case of Zuozi and left parents, precursor = right parents left parents (if the right parents do not exist left parents, will continue to go up, until the left parents).

The analysis is complete. Here is the code implementation.

Because the nodes of our two-fork tree have only left and right pointers, this feeling has to be done by hand or stack. Let's write one on the stack (time complexity is O (N))

Bintree predecessor (Bintree x,bintree BST) {if(x->left!=NULL)returnFindmax (x->Left ); Else{Stack S; S=Creatstack ();  while(bst!=X) {Push (S,BST); if(X->data > bst->Data) BST= bst->Right ; Else if(X->data < bst->Data) BST= bst->Left ;        } Bintree Par,son; Son=X;  while(Par = Pop (S))->right!=son) Son = Par;//equivalent to case2&3 until the left mother is found.         returnPar; }}

The difference between a recursive and a stack is interrupted:

Then the subsequent analysis: (analogy precursor, if the precursor to understand can not see, basically the same analytical thinking)

Meet two conditions, one is greater than the current key value, then only the RP and Rs area can be found.

The second requirement is the smallest of these values. We know that for the RP, X, ls, rs all belong to his left subtree, then x, LS and RS are smaller than it.

So it is clear that the precursor is the minimum value in RS, which is the minimum value in the successor = right subtree. The condition is: there is a right subtree.

That doesn't exist right subtree only right parent's situation?

That can only be found on the RP, RP also has two parts, the first part is the RS,RP of the LP, although the RS satisfies the conditions greater than X, but the RP RS all elements are greater than LP, so to find the successor, at least the RP.

Also, the RP may have left parents or right parents, apparently, the left parent is smaller than all of his right subtrees, including X, condition one is not satisfied, obviously not. The right parent is larger than the RP, so it competes with the RP.

So the final conclusion is that in the case of only the right parent, there is no right subtree, and the successor = the value of the right parent.

That doesn't exist right subtree and right parent's situation?

That leaves only Zuozi and left parents, obviously, Zuozi certainly not, all of its elements are less than x. That can only be found in the left parents, after all, although the left parents are smaller than it, but the right parents also have its own left/right parents and left subtree.

Left parents left parents, and left subtree are not, are smaller than the left parents themselves, less than X. That can only be found on the left parents of the right parents, for its right parents, his Zuozi are all smaller than him, that is, including X's left parents and X, so, at this time to find the right parents is our successor.

Therefore, there is no right subtree and right parents, successor = left parents of the right parents (if left parents do not exist right parents, will continue to traverse until the right parents appear).

The analysis is complete. The following is a code implementation, which is also implemented with a stack.

Bintree successor (Bintree X,bintree BST) {if(x->Right )returnFindmin (x->Right ); Else{Stack S; S=Creatstack ();  while(bst!=X) {Push (S,BST); if(X->data > bst->Data) BST= bst->Right ; Else if(X->data < bst->Data) BST= bst->Left ;        } Bintree Par,son; Son=X;  while(Par = Pop (S))->left!=son) Son =Par; returnPar; }}

is basically the same.

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.