Proof of a conclusion in a binary tree chain list and the interpretation of the algorithm in the successor () function

Source: Internet
Author: User

First, the concept of "node X successor" is defined first. The concept of this sentence is that the successor is the entire binary tree, the key is only greater than the key node of X.

Verification: If X has two children, then the successor has no left child.

Proof: If X has two children, then its right subtree exists. In a binary tree list, the right subtree for any x is a set of nodes with a key greater than the key of X, which is {Y|y->key > X->key}. According to the definition of successor, if X->rightchild≠nil, then successor = Min ({y|y->key > X->key}). means that if node Y is X's successor and X has a right child, then y must be the leftmost node of the right subtree of x, so y must have no left child.

For an explanation of the algorithm in the successor () function:

1. If x has a right child, see the conclusion in the previous proof.
2. If x does not have a right child, then x must be the largest node of the key of the subtree with the head node of x, then to find successor, you must enlarge the subtree's extent and use the parent node of X as the head node of the new subtree, as P (x). There are three cases of P (x):

2.1 If p (x) = nil, the search ends, because this means that the key of X is the largest key in the entire binary tree, so successor = nil.
2.2 If P (x) is ≠nil and X is the left node of P (x), then successor = P (x). Because if there is a node y = successor and y≠p (x), then X->key < Y->key < P (x)->key, according to the rules of the binary tree list, y should meet Y∈{y|y->key > x >key} is the right subtree of x, but X is not the right child.

2.3 If P (x) ≠nil, and X is the right node of P (x), it is obvious that P (x)->key < X->key that x is still the largest node in the new subtree, so you still need to expand the size of the subtree, and the parent of the current head node as the head of the new subtree The judgement, which is recorded as P (y), is divided into three different cases:

2.3.1 if P (y) = nil, it is equivalent to 2.1.
2.3.2 P (Y) ≠nil, and P (x) is P (Y) the right node, it is apparent that P (y)->key < P (x)->key < X->key obviously P (y) is not successor, so repeat 2.3 Behavior to continue the search.
2.3.3 if P (y) is ≠nil and P (x) is the left node of P (Y), then P (y) = successor. Because at this point x = Max ({z|z->key <= P (y)->key}) ⇔p (y) = min ({k|k->key > X->key}) The conclusion in the previous proof is successor = Min ({k|k-& Gt;key > X->key}) can have P (y) = successor.

Proof of a conclusion in a binary tree chain list and the interpretation of the algorithm in the successor () function

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.