Original: Step by step write algorithm (sort binary Tree Delete-2)
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
2.4 The left and right subtree of the deleted node is present, and there are two different situations
1) The left node is the largest node of the current left subtree, at which point only the left node can be used instead of the root node .
/** * ======> 6* / \ / * 6 5 15* / * 5 */
How do you write the code?
STATUS Delete_node_from_tree (tree_node** pptreenode, int data) {tree_node* ptreenode; tree_node* pleftmax;if (NULL = = Pptreenode | | NULL = = *pptreenode) return False;ptreenode = Find_data_in_tree_node (*pptreenode, data); if (NULL = = Ptreenode) return False;if (*pptreenode = = Ptreenode) {if (null = = Ptreenode->left_child && NULL = = ptreenode->right_child) {* Pptreenode = NULL;} else if (null! = Ptreenode->left_child && NULL = = ptreenode->right_child) {*pptreenode = Ptreenode->left _child;ptreenode->left_child->parent = NULL;} else if (NULL = = Ptreenode->left_child && null! = ptreenode->right_child) {*pptreenode = ptreenode-> Right_child;ptreenode->right_child->parent = NULL;} Else{pleftmax = Find_max_node (Ptreenode->left_child); if (Pleftmax = = ptreenode->left_child) {*ppTreeNode = ptreenode->left_child; (*pptreenode)->right_child = ptreenode->right_child; (*ppTreeNode)->right_ Child->parent = *pptreenode; (*pptreenode)->parent = NULL;}} Free (ptreenode); return TRUE;} return TRUE;}
The content added in the above code represents the situation we are introducing. To do this, we can design a test case. Insert 10, 6, 5, 15, and then delete 10.
static void Test6 () {tree_node* Ptreenode = Null;assert (true = = Insert_node_into_tree (&ptreenode, ten)); assert (true = = Insert_node_into_tree (&ptreenode, 6)); assert (true = = Insert_node_into_tree (&ptreenode, 5)); assert (true = = Insert_node_into_tree (&ptreenode, ()); assert (TRUE = = Delete_node_from_tree (&ptreenode)); assert (6 = = Ptreenode->data), assert (NULL = = ptreenode->parent), assert (Ptreenode->right_child->data = =), assert ( Ptreenode = ptreenode->right_child->parent); assert (NULL = = ptreenode->parent); free (Ptreenode->left_ Child), free (ptreenode->right_child), free (ptreenode);}
If the above test case passes, there is no problem with the code that we added.
2) The left node is not the largest node of the current left subtree, as shown below
/** * ======> 8* / \ / * 6 5 15* \ * 8 */
At this point, we should use the 10 to the left of the maximum node 8 instead of the deleted node 10.
STATUS Delete_node_from_tree (tree_node** pptreenode, int data) {tree_node* ptreenode; tree_node* pleftmax;if (NULL = = Pptreenode | | NULL = = *pptreenode) return False;ptreenode = Find_data_in_tree_node (*pptreenode, data); if (NULL = = Ptreenode) return False;if (*pptreenode = = Ptreenode) {if (null = = Ptreenode->left_child && NULL = = ptreenode->right_child) {* Pptreenode = NULL;} else if (null! = Ptreenode->left_child && NULL = = ptreenode->right_child) {*pptreenode = Ptreenode->left _child;ptreenode->left_child->parent = NULL;} else if (NULL = = Ptreenode->left_child && null! = ptreenode->right_child) {*pptreenode = ptreenode-> Right_child;ptreenode->right_child->parent = NULL;} Else{pleftmax = Find_max_node (Ptreenode->left_child); if (Pleftmax = = ptreenode->left_child) {*ppTreeNode = ptreenode->left_child; (*pptreenode)->right_child = ptreenode->right_child; (*ppTreeNode)->right_ Child->parent = *pptreenode; (*pptreenode)->parent = NULL;} Else{ptreenode->data = Pleftmax->data;pleftmax->parent->right_child = NULL;pTreeNode = PLeftMax;}} Free (ptreenode); return TRUE;} return TRUE;}
So, what should be the design of the test cases under this scenario? In fact, you just need to follow the above given to proceed. Insert Data 10, 6, 8, 15, and then delete data 10.
static void Test7 () {tree_node* Ptreenode = Null;assert (true = = Insert_node_into_tree (&ptreenode, ten)); assert (true = = Insert_node_into_tree (&ptreenode, 6)); assert (true = = Insert_node_into_tree (&ptreenode, 8)); assert (true = = Insert_node_into_tree (&ptreenode, ()); assert (TRUE = = Delete_node_from_tree (&ptreenode)); assert (8 = = Ptreenode->data); assert (null = = Ptreenode->parent); assert (null = = Ptreenode->left_child->right_child); ASSERT (NULL = = ptreenode->parent), free (ptreenode->left_child), free (ptreenode->right_child), Free ( Ptreenode);}
At this point, the deletion node is the root node of the case all discussed, then if the deletion of the node is a normal node, then how to solve it?
STATUS Delete_node_from_tree (tree_node** pptreenode, int data) {tree_node* ptreenode; tree_node* pleftmax;if (NULL = = Pptreenode | | NULL = = *pptreenode) return False;ptreenode = Find_data_in_tree_node (*pptreenode, data); if (NULL = = Ptreenode) return False;if (*pptreenode = = Ptreenode) {if (null = = Ptreenode->left_child && NULL = = ptreenode->right_child) {* Pptreenode = NULL;} else if (null! = Ptreenode->left_child && NULL = = ptreenode->right_child) {*pptreenode = Ptreenode->left _child;ptreenode->left_child->parent = NULL;} else if (NULL = = Ptreenode->left_child && null! = ptreenode->right_child) {*pptreenode = ptreenode-> Right_child;ptreenode->right_child->parent = NULL;} Else{pleftmax = Find_max_node (Ptreenode->left_child); if (Pleftmax = = ptreenode->left_child) {*ppTreeNode = ptreenode->left_child; (*pptreenode)->right_child = ptreenode->right_child; (*ppTreeNode)->right_ Child->parent = *pptreenode; (*pptreenode)->parent = NULL;} Else{ptreenode->data = Pleftmax->data;pleftmax->parent->right_child = pLeftMax->left_child; Pleftmax->left_child->parent = Pleftmax->parent;ptreenode = Pleftmax;}} Free (ptreenode); return TRUE;} Return _delete_node_from_tree (Ptreenode);}
We add _delete_node_from_tree to the last line of the current function, which is used to handle the deletion of normal nodes, and we will continue to describe them in the following blog post.
3, the deletion of ordinary nodes
adjourned
Step-by-step write algorithm (sort binary Tree Delete-2)