About the binary tree deletion algorithm in introduction to Algorithms

Source: Internet
Author: User

First, let's briefly introduce the binary search tree. The binary search tree means that any node with the left subtree is no greater than the root node, any node of the right subtree is not smaller than the binary tree of the root node. The algorithm for deleting a node is discussed in the book in three cases.

First, the node to be deletedZWithout a son, this situation is very simple,ZWhich son of his father node makes the pointer to that son empty,ZThe pointer to the parent node is null.

Second, the node to be deletedZThere is only one son, soZThe child tree of the root node is either not greaterZOr none of them are smallerZAt this time, letZSon pointingZPointerZFather node,ZWhich is the son of his father's node? Let the pointer pointing to that son pointZSon, handle it againZThe pointer.

Finally, the deleted NodeZThere are two sons. This is the most complicated situation. The method provided in the book is to firstZSuccessorYSubstitutionZAnd then modifyYPointer of the parent node to pointYThe only son orNULL(In this caseZWill certainly have at most one son ).ZSuccessorYSubstitutionZThe processing isKey [z] = key [y]In this way, you do not need to change the originalZThe node points to other node pointers, but I think this is inappropriate in some cases, such as when the satellite data volume in the node is large, this simply increases the time and memory overhead, and cannot be rewritten when the satellite data in the node is read-only.ZSo I think the most appropriate way is to change the satellite data.YTo pointZFather node, son node.

The above Algorithms are the Algorithms proposed on the 2nd page of the original book Introduction to Algorithms. I checked the Third Edition of Introduction to Algorithms and proposed an improved algorithm.Transplant (T, u, v)Function.VReplace the treeUThe tree is the root node.TransplantFunction,DeleteThe algorithm has an easier implementation method:

  1. IfZIf you have at most one son, use that subtree (which can beNull) To replaceZSubtree;
  2. IfZThere are two sons, first useZSuccessorYInsteadYIs the child tree of the root node, and then changes the pointerYReplaceZ.
  3. To sum up the two methods, the truth is the same, the form is different, and the method below is more rigorous.

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.