Now there is a binary tree to find the tree as follows:
If we need to delete a node, and after the deletion, it still satisfies the binary lookup tree's data ordering strategy . In this case, the delete operation can be divided into three cases . As follows
Scenario 1: node has no left subtree
Figure: A two-fork tree without a left subtree
If the node is deleted in this case, the location of the node can be divided into three ways. As follows
Delete The root node: Delete the root node is also delete node 5, at this point only the root node pointer to its right subtree node. Figure below
Delete leaf node: If the deleted node is a leaf node or node 9, simply point the parent node to NULL, as shown below
Three intermediate nodes: If the deleted node is the middle node or node 6, just point the parent node to the right child nodes. Figure below
Scenario 2: node has no right subtree
Figure: A two-pronged tree without a right sub-tree
If the node is deleted in this case, the location of the node can be divided into three ways. It is the same as the three cases where there are no left subtrees, just the difference between the left and right pointers. There is not much to repeat here. Let's understand it for ourselves.
Case 3: node with left subtree also has right sub-tree
Figure: If the binary tree is the case, its processing will not be different because of the location of the nodes, but the processing process is more complex.
Observing the above two-fork tree, if you need to delete the root node 5, if we can find a node that is the node 3 and 7, and then replace it with the root of the location of the point. This way the whole binary tree does not need to be too right to move to complete the operation of node deletion. For example: Node 4 is in line with the above requirements, until the deletion of node 5 after its structure as shown
We can find that the actual deletion of the node is the original node 4, and then the original root node of the content 5 is replaced by 4, the deletion is completed, the question is how to find the matching node 4, in fact, we observe the characteristics of binary search tree:
the characteristics are as follows: each node has a different value, that is, each node in the tree has a different value. The data for each node is greater than the data for the left subtree node (if any), but less than the node of the right subtree (if any). The left and right two parts of the subtree, but also a lesson binary search tree.
If you want to delete the node after the binary tree is still a binary search tree, you can find that meet the requirements of the node only two, that is 4 and 6. They are from the root node 5 of the left node 3 has been from the right subtree to the leaf node and the right node 71 straight to the left sub-tree walk to the leaf node.
Now we use the node from the left Dial hand node to find the replacement, if the deletion of the node is 5, from the node 5 of the left Dial hand node 3 to the right subtree to find, until the leaf node is reached, found the node 4. The node is then deleted, with the same method and condition, and finally replacing the original node 50% as 4.
If the deletion is node 3, at this time left dial hand Node 2 does not have a right child node, so the condition is the node 2, the deletion of the operation is to delete a node without a right subtree 2, and then replace the value of the original node 3 becomes 2. The complete operation is as follows
As for the method of finding and replacing the deleted node value from the right sub-node, and looking for a similar ... from the left node.
Okay, look directly at the code.
/*********************************************************-Copyright (C): 2016-file name:deletetree.c-author
:-Zhaoxinan-date:2016 August 05 Friday 16:15 45 seconds-Description: Use recursion to create a binary tree and then enter a node value Use the two-fork lookup method to find the node, if found, call the delete function to delete the node, the final output after the deleted result * ****************************************************** */#include <stdio.h> #include <stdlib.h> struct tree//tree structure declaration {int data; Node data struct tree *left; Pointer to Zuozi struct tree *right;
Pointer to right sub-tree}; typedef struct Tree TreeNode; A new type of tree structure typedef TreeNode *btree;
Declare tree node pointer type/* Recursively create two fork tree */btree createtree (int *data, int pos) {Btree newnode;
recursive termination condition if (data[pos] = = 0 | | pos > +) {return NULL;
} else {//Assign memory to new node NewNode = (btree) malloc (sizeof (TreeNode));
Create new node Content newnode->data = Data[pos];
To create a recursive call to the left subtree Newnode->left = Createtree (data, 2*pos);
Create a recursive call to the right subtree Newnode->right = createtree (data, 2*pos+1);
return newnode;
}/* Two fork Tree lookup */btree Btreefind (btree ptr, int value, int *pos) {btree backfather; Backfather = ptr; Sets the parent node pointer initial value *pos = 0; Set the position initial value while (ptr! = NULL) {if (Ptr->data = = value) {return backfather;
Found the return parent node} else {backfather = ptr; if (Ptr->data > value) {ptr = ptr->left;
Left dial hand Tree *pos =-1; } else {ptr = ptr->right;
Right subtree *pos = 1;
}}} return NULL; }/* Two fork tree node Delete */Btree deletenode (btree root, int value) {Btree backfather; Parent node pointer btree ptr; Delete node pointer btree next; Sub-node pointer int pos; Delete location Backfather = Btreefind (root, value, &pos);
if (Backfather = = NULL)//not found {return root;
}//Delete location switch (POS) {case-1: {//Left dial hand node ptr = backfather->left;
Break
} Case 1: {//Right child node PTR = backfather->right;
Break
} case 0: {//with node PTR = Backfather;
Break
}}/* First case, there is no left subtree */if (Ptr->left = = NULL) {if (backfather! = PTR)//Determine if the root node {
is not the root node, then the delete operation is the right child node of the parent node equal to the right child node of the current node//is skipping the current node Backfather->right = ptr->right;
} else {//is the root node, then the root node points directly to the right node root = root->right;
} free (PTR);
ptr = NULL;
return root; }/* In the second case, there is no right subtree */if (ptr->right = = NULL) {if (Backfather!= PTR)//To determine if the root node {//is not the root node, then the delete operation is the right child node of the parent node equals the right child node pointing to the current node//is skipping the current node Backfather
->left = ptr->left;
} else {//is the root node, then the root node points directly to the right node root = root->left;
} free (PTR);
ptr = NULL;
return root; }/* Third case, there are Zuozi also have right subtree */backfather = ptr; Parent node points to current node next = ptr->left;
Set child node while (next->right! = NULL) {backfather = next;
Next = next->right; } Ptr->data = next->data;
Replace data if (Backfather->left = = next) {Backfather->left = next->left;
} else {backfather->right = next->right;
} free (next);
return root;
}/* Two fork Tree's pre-sequence traversal output */void Preolder (Btree ptr) {if (PTR) {printf ("%2d", ptr->data);
Preolder (Ptr->left);
Preolder (Ptr->right); }}/* Two fork tree in the middle sequence traversal output */void Inolder (Btree ptr) {if (PTR)
{Inolder (ptr->left);
printf ("%2d", ptr->data);
Inolder (Ptr->right);
}/* Two forks the post-traversal output */void Postolder (Btree ptr) {if (PTR) {postolder (ptr->left);
Postolder (Ptr->right);
printf ("%2d", ptr->data);
}} int main () {btree root = NULL;
int value;
int data[16] = {00,5,4,6,2,0,0,8,1,3,0,0,0,0,7,9};
Recursive creation two fork tree root = Createbtree (data, 1);
printf ("\ n-------pre-sequence traversal-------\ n");
Preolder (root);
printf ("\ n-------Middle sequence traversal-------\ n");
Inolder (root);
printf ("\ n-------post-traversal-------\ n");
Postolder (root);
printf ("\ n Please enter the node you want to delete (1-9):");
scanf ("%d", &value);
Root = Deletenode (root, value);
printf ("The node content of the tree after deletion is \ n");
printf ("\ n-------pre-sequence traversal-------\ n");
Preolder (root);
printf ("\ n-------Middle sequence traversal-------\ n");
Inolder (root);
printf ("\ n-------post-traversal-------\ n");
Postolder (root);
printf ("\ n");
return 0;
}
Primitive binary Tree
Delete Node 2
Deleting a node 5
Delete node 6