The example of this article describes the method of using Java to use mergers and deletion to delete the node in the binary tree. Share it for everyone for your reference. The specific analysis is as follows:
The idea of implementation is very simple:
First: Find nodes to be deleted
SECOND: If the deleted node is not there is no right subtree, then the left sub -tree chain to the parent node
Third: If the deleted nodes are not left, the left sub -tree chain to the parent node
FORTH: If the deleted nodes are left and right, then you can merge the sub -tree after the delete node: There are two ways to use the right node of the left sub -tree that delete nodes, point to the right sub -tree of the delete node, and the other It is to point the left sub -tree of the deleted node with the leftmost node of the number of words of the deleted node.
Java implementation is as follows:
Public void deletebyerring (int El) {intbstnode TMP, node, p = root, prev = null;/*Find the node to be deleted*/While (p! = null &&KEY! = El) Rev = p; if (if P.Key <EL) p = P.Right; Else P = P.Left;}/*Find ED*/Node = P; If (P! = NULL && P.Key == EL) {if.richt == null) // NODE has no right children (if any) is attached to node.left; // iTS Parent Else if (node.left == null) // node has no left left. child ites right Child (if any) is attached to node = node.richt // its paintelse {tmp = node.left; about (tmp.rid! = null) tmp = tmp.richt; // find the rightmost n ODE of the left subtreetem. Right = node.right; // Establish the link between the rightmost node of the left subtree and the right subtreenode = node.left;} if (p == root) {root) = node;} else if (prev.left == p) {prev.left = node;} else prev.richt = node} else if (root! = Null) {system.out.println ("The node is not in the tree"); TLN ("The Tree is Empty");}It is hoped that this article is helpful to everyone's Java program design.