I have written a binary search tree before, and the most troublesome method is to delete elements. There are eight cases in total, mainly to identify whether it is a root node or a non-root node,
Write too annoying, see: http://blog.csdn.net/todd911/article/details/8471566
Today, I want to simplify this method. Before that, I wrote a simple single linked list insert operation, which simplifies the differences between root nodes and non-root nodes by Using Dual pointers,
For details, see.
The improved code is as follows:
Void deletenode (treenode ** node, int value) {treenode * Current; while (current = * node )! = NULL & Current-> value! = Value) {If (current-> value) {node = vertex T-> left;} else if (current-> value <value) {node = existing T-> right ;}// if current is null, if (current = NULL) is not found) {printf ("this node want to delete is not exit! \ N ");} else {// If a node is found, the node status is determined. // No child node exists, delete if (current-> left = NULL & Current-> right = NULL) {free (current); * node = NULL; current = NULL ;} else // Insert the right child to the left child tree first, and then point the pointer pointing to current to the left child of current to delete currentif (current-> left! = NULL & Current-> right! = NULL) {insertnode (current-> left, current-> right); * node = Current-> left; free (current); current = NULL ;} else // if there are no left children and right children, the pointer pointing to current is directed to current's right child, delete currentif (current-> left = NULL & Current-> right! = NULL) {* node = Current-> right; free (current); current = NULL;} else // No right child, left child, delete currentif (current-> left! = NULL & Current-> right = NULL) {* node = Current-> left; free (current); current = NULL ;}}}
One problem with this method is that all the deleted nodes have left and right children, and the right child is first inserted into the left child tree, then, point the pointer pointing to current to the left child of current,
The trunk length of a binary tree is unbalanced.
The detailed steps are as follows. node 12 is to be deleted:
It can be seen that the structure of the final tree is severely damaged, which may reduce the search efficiency.
Next, let's make some improvements. If there are left and right children on the node to be deleted, we will get the largest value from the left child to replace the node to be deleted. The detailed steps are as follows, node 12 is to be deleted:
The tree structure is not damaged. We recommend that you use this method.
The complete sample code is attached below:
#include <stdio.h>#include <stdlib.h>#define SIZE 10typedef struct tagNode{ int value; struct tagNode* left; struct tagNode* right;}treeNode;void displayArray(int array[],int size){ printf("the array is:"); int i; for(i=0;i<size;i++){ printf("%d ",array[i]); } printf("\n");}void displayTree(treeNode* node){ if(node == NULL) return; if(node->left != NULL){ displayTree(node->left); } printf("%d ",node->value); if(node->right != NULL){ displayTree(node->right); }}void insertNode(treeNode* node, treeNode* iNode){ if(iNode->value >= node->value && node->right != NULL){ insertNode(node->right, iNode); return; } if(iNode->value < node->value && node->left != NULL){ insertNode(node->left, iNode); return; } if(iNode->value >= node->value && node->right == NULL){ node->right = iNode; } if(iNode->value < node->value && node->left == NULL){ node->left = iNode; }}void deleteNode(treeNode** node, int value){ treeNode* current; while( (current = *node) != NULL && current->value != value ){ if(current->value > value){ node = ¤t->left; }else if(current->value < value){ node = ¤t->right; } } if(current == NULL){ printf("this node want to delete is not exit!\n"); }else{ if(current->left == NULL && current->right == NULL){ free(current); *node = NULL; current = NULL; }else if(current->left != NULL && current->right != NULL){ insertNode(current->left, current->right); *node = current->left; free(current); current = NULL; }else if(current->left == NULL && current->right != NULL){ *node = current->right; free(current); current = NULL; }else if(current->left != NULL && current->right == NULL){ *node = current->left; free(current); current = NULL; } }}void getMaxValueNode(treeNode **link){ while( *link != NULL && (*link)->right != NULL){ *link = (*link)->right; };}void deleteNode2(treeNode** node, int value){ treeNode* current; while( (current = *node) != NULL && current->value != value ){ if(current->value > value){ node = ¤t->left; }else if(current->value < value){ node = ¤t->right; } } if(current == NULL){ printf("this node want to delete is not exit!\n"); }else{ if(current->left == NULL && current->right == NULL){ free(current); *node = NULL; current = NULL; }else if(current->left != NULL && current->right != NULL){ treeNode** link = ¤t->left; getMaxValueNode(link); if(*link != NULL){ treeNode *maxValueNode = *link; *link = (*link)->left; maxValueNode->left = current->left; maxValueNode->right = current->right; *node = maxValueNode; free(current); current = NULL; } }else if(current->left == NULL && current->right != NULL){ *node = current->right; free(current); current = NULL; }else if(current->left != NULL && current->right == NULL){ *node = current->left; free(current); current = NULL; } }}void createTree(treeNode** root, int array[], int size){ int i; *root = (treeNode*)malloc(sizeof(treeNode)); (*root)->value = array[0]; (*root)->left = NULL; (*root)->right = NULL; for(i=1;i<size;i++){ treeNode* child = (treeNode*)malloc(sizeof(treeNode)); child->value = array[i]; child->left = NULL; child->right = NULL; insertNode(*root, child); }}void deleteTree(treeNode* node){ if(node == NULL) return; if(node->left != NULL){ deleteTree(node->left); } if(node->right != NULL){ deleteTree(node->right); } if(node->left == NULL && node->right == NULL){ free(node); node = NULL; }}int main(int argc, char* argv[]){ int array[SIZE] = {60,4,45,78,345,23,12,3,6,21}; displayArray(array,SIZE); treeNode *root = NULL; createTree(&root, array, SIZE); printf("the tree is(left->middle->right):"); displayTree(root); printf("\n"); int value = 12; printf("delete value:%d\n",value); deleteNode(&root,value); printf("the tree is(left->middle->right):"); displayTree(root); printf("\n"); value = 78; printf("delete value:%d\n",value); deleteNode2(&root,value); printf("the tree is(left->middle->right):"); displayTree(root); printf("\n"); deleteTree(root); return 0;}