У меня есть код для вставки данных в балансировочное двоичное дерево, поэтому, например, если я введу эти входные данные:
20, 10, 30, 5, 15, 25, 4
Я ожидаю, что дерево после ввода будет выглядеть так:
20
/ \
10 30
/ \ / \
5 15 25 4
Итак, при удалении все работает нормально, кроме удаления 4
4 относится к случаю 1 в функции удаления,
Вопрос в том, не могу понять, почему удаление 4 не делает работает, но когда я удаляю 5, 15, 25, это работает?
Я получил функцию удаления из https://www.youtube.com/watch?v=gcULXE7ViZw
Это для бинарного дерева поиска, но я думал, что это не вызовет никаких проблем, даже если оно используется в двоичном дереве
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
struct node{
int data, balance;
struct node *left, *right;
};
int insert(struct node **root, struct node **curr, int data){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode -> data = data;
newNode -> left = NULL;
newNode -> right = NULL;
newNode -> balance = 0;
if((*root) == NULL){
(*root) = (*curr) = newNode;
(*root) -> left = NULL;
(*root) -> right = NULL;
return 0;
} else {
if((*curr)->left == NULL && (*curr)->balance == 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) -> left = newNode;
return 0;
} else if ((*curr)->right == NULL && (*curr)->balance == -1){
(*curr) -> balance = (*curr) -> balance + 1;
(*curr) -> right = newNode;
return 0;
} else if ((*curr)->balance == 0 && (*curr)->left->balance == 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) = (*curr)->left;
return insert(root,curr,data);
} else if ((*curr)->balance < 0 && (*curr)->left->balance < 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) = (*curr) -> left;
return insert(root,curr,data);
} else if ((*curr)->balance < 0 && (*curr)->left->balance == 0){
(*curr) -> balance = (*curr) -> balance + 1;
(*curr) = (*curr)->right;
return insert(root, curr, data);
}
}
}
void preorder(struct node *root){
if(root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node *root){
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
void inorder(struct node *root){
if(root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void search(struct node *root, int *key, int *found){
if(root == NULL) return;
search(root->left, key, found);
if(root->data == *key){
*found = 1;
return ;
}
search(root->right, key, found);
}
struct node *findMin(struct node *root){
while(root->left != NULL) root = root->left;
return root;
}
struct node *Delete(struct node *root, int data){
if(root == NULL) return root;
else if(data < root->data) root->left = Delete(root->left, data);
else if(data > root->data) root->right = Delete(root->right, data);
else {
//Case 1: no child / leaf node
if(root->left == NULL && root->right == NULL){
free(root);
root = NULL;
}
//Case 2: one child, left or right
else if(root->left == NULL){
struct node *temp = root;
root = root->right;
free(temp);
} else if (root->right == NULL){
struct node *temp = root;
root = root->left;
free(temp);
}
//Case 3: two children
else{
struct node *temp = findMin(root->right);
root->data = temp->data;
root->right = Delete(root->right, temp->data);
}
}
return root;
}
int main(){
struct node *root, *curr;
int choice, data, key, found, delKey;
root = curr = NULL;
while(1){
found = 0;
printf("Balanced Binary Tree Menu\n");
printf("1. Insert Data\n");
printf("2. View on pre order\n");
printf("3. View on post order\n");
printf("4. View on in order\n");
printf("5. Search\n");
printf("6. Delete\n");
printf("7. Exit\n");
printf("Pilihan: ");scanf("%d", &choice);fflush(stdin);
if(choice == 1){
printf("Enter data : "); scanf("%d", &data);
curr = root;
insert(&root, &curr, data);
} else if (choice == 2){
preorder(root);
system("pause");
} else if (choice == 3){
postorder(root);
system("pause");
} else if (choice == 4){
inorder(root);
system("pause");
} else if (choice == 5){
printf("Search: "); scanf("%d", &key);
search(root, &key, &found);
if(found == 1){
printf("Data found !\n");
} else {
printf("Data not found !\n");
}
system("pause");
} else if (choice == 6){
curr = root;
printf("Data : ");
preorder(root);
printf("\n\n");
printf("Enter data to be deleted: "); scanf("%d", &delKey);
Delete(curr, delKey);
printf("Data after deletion : ");
preorder(root);
system("pause");
} else if (choice == 7){
return 1;
}
system("cls");
}
return 0;
}