回复 19楼 GBH1
// 哪些变量定义语义不清,我给改过来。// 删除操作函数已经修改好,简单测试了一下,没发现问题了,欢迎大家继续测试
// 因时间问题,代码仍未精简、完善, 见谅!
// 测试时可以输入字母a、b、c、d...代替输入月份
程序代码:
void avlDelete(treePointer *parent, element x, int *unbalanced) { treePointer temp; treePointer* delp; if(!*parent){ *unbalanced = FALSE; printf("未查找到要删除的节点!\n"); return ; } if(strcmp(x.key, (*parent)->data.key) == 0){ if(((*parent)->leftChild == NULL) && ((*parent)->rightChild == NULL)) { //情况1:无孩子 free(*parent); *parent = NULL; *unbalanced = TRUE; }else if(((*parent)->leftChild == NULL) && ((*parent)->rightChild != NULL)) { //情况2:无左孩子 temp = *parent; *parent = (*parent)->rightChild; free(temp); *unbalanced = TRUE; }else if(((*parent)->leftChild != NULL) && ((*parent)->rightChild == NULL)) { //情况3:无右孩子 temp = *parent; *parent = (*parent)->leftChild; free(temp); *unbalanced = TRUE; }else{ //情况4:有俩孩子 if((*parent)->leftChild->rightChild == NULL){//左孩无右孙 temp = *parent; *parent = (*parent)->leftChild; (*parent)->rightChild = temp->rightChild; free(temp); if((*parent)->bf == 0){ (*parent)->bf = -1; *unbalanced == FALSE; }else if((*parent)->bf == 1){ (*parent)->bf = 0; *unbalanced = FALSE; }else{ rightRotation(parent, unbalanced); } }else{ //左孩有右孙 leftMaxNode(parent, &(*parent)->leftChild, unbalanced); if(*unbalanced){ if((*parent)->bf == -1){ rightRotation(parent, unbalanced); }else if((*parent)->bf == 0){ (*parent)->bf = -1; *unbalanced = FALSE; }else if((*parent)->bf == 1){ (*parent)->bf = 0; *unbalanced = FALSE; } } } } }else if(strcmp(x.key, (*parent)->data.key) < 0){ avlDelete(&(*parent)->leftChild, x, unbalanced); if(*unbalanced) if((*parent)->bf == 1){ (*parent)->bf = 0; }else if((*parent)->bf == 0){ (*parent)->bf = -1; *unbalanced = FALSE; }else if((*parent)->bf == -1){ rightRotation(parent, unbalanced); } }else if(strcmp(x.key, (*parent)->data.key) > 0){ avlDelete(&(*parent)->rightChild, x, unbalanced); if(*unbalanced) if((*parent)->bf == -1){ (*parent)->bf = 0; }else if((*parent)->bf == 0){ (*parent)->bf = 1; *unbalanced = FALSE; }else if((*parent)->bf == 1){ leftRotation(parent, unbalanced); } } } void leftMaxNode(treePointer *parent, treePointer *maxNode, int *unbalanced) { /* 与左子树最大元交换,并删除结点 */ if((*maxNode)->rightChild->rightChild == NULL){ treePointer temp = *parent; *parent = (*maxNode)->rightChild; (*maxNode)->rightChild = (*maxNode)->rightChild->leftChild; (*parent)->leftChild = temp->leftChild; (*parent)->rightChild = temp->rightChild; (*parent)->bf = temp->bf; free(temp); temp = NULL;/*不知道为什么,这里必须给temp赋空值,虽然后再不用temp了*/ if((*maxNode)->bf == 0){ (*maxNode)->bf = -1; *unbalanced = FALSE; }else if((*maxNode)->bf == -1){ (*maxNode)->bf == 0; *unbalanced = TRUE; }else if((*maxNode)->bf == 1){ leftRotation(maxNode, unbalanced); (*parent)->leftChild = *maxNode; /*这行代码容易被忽略 */ } }else{ leftMaxNode(parent, &(*maxNode)->rightChild, unbalanced); if(*unbalanced){ if((*maxNode)->bf == -1){ (*maxNode)->bf = 0; *unbalanced = FALSE; }else if((*maxNode)->bf == 0){ (*maxNode)->bf = 1; *unbalanced = FALSE; }else /* (*maxNode)->bf == 1 */ leftRotation(maxNode, unbalanced); } } }
[此贴子已经被作者于2017-8-2 09:06编辑过]
一切都在学习、尝试、摸索中