Тема: Видалення елементу з бінарного дерева.
Пробував видалити елемент, але щось нічого не виходить. Допоможіть будь ласка. Компілятор помилку не вибиває.
void deleteleaf(leaf** root) {
cout << "Введіть індекс елемента, якого Ви хочете видалити: ";
int num;
cin >> num;
leaf* clean = (*root);
leaf* saveroot = (*root);
leaf* rootleft = ((*root)->left);
leaf* rootright = ((*root)->right);
if (clean->index == num) {
if (clean->right != nullptr) {
clean = clean->right;
while (clean->left != nullptr) {
clean = clean->left;
}
leaf* newright = clean->right;
(*root) = clean;
(*root)->left = rootleft;
(*root)->right = rootright;
delete clean;
leaf* find = rootright;
while (find->left) {
find = find->left;
}
find->left = newright;
return;
}
else {
if (clean->index < num && clean->index>num) {
while (clean->left != nullptr && clean->right != nullptr) {
if (clean->index < num && clean->right != nullptr) {
clean = clean->right;
if (clean->index == num) {
break;
}
}
else {
if (clean->index > num && clean->left != nullptr) {
clean = clean->left;
if (clean->index == num) {
break;
}
}
}
}if (clean->index != num) {
cout << "Індексу " << num << " не знайдено у правій частині дерева.";
return;
}
else {
if (clean->index == num) {
leaf* deleteit = clean;
leaf* save = clean;
leaf* cleanright;
leaf* cleanleft = clean->left;
if (clean->right != nullptr) {
cleanright = clean->right;
}
else {
cleanright = nullptr;
}
if (clean->left != nullptr) {
clean = clean->left;
if (clean->right == nullptr) {
deleteit = clean;
deleteit->right = cleanright;
delete save;
return;
}
else {
while (clean->right != nullptr) {
clean = clean->right;
}
deleteit = clean;
deleteit->right = cleanright;
deleteit->left = cleanleft;
delete clean;
return;
}
}
}
}
}
}
}
}