Тема: Видалення елементу.
Не розумію в чому проблема. Якщо хтось знає допоможіть будь ласка.
В коді написав де саме помилка.
void deleteleaf(leaf** root) {
cout << "Введіть індекс листя, яке ви хочите видалити: ";
int num;
cin >> num;
leaf* deleteroot = (*root);
leaf* oldroot = (*root);
leaf* lefttree= (*root)->left;
leaf* righttree = (*root)->right;
leaf* find = (*root)->left;
if ((*root)->index == num) {
if ((*root)->left == nullptr && (*root)->right == nullptr) {
delete (*root);
}
if ((*root)->right != nullptr && (*root)->left == nullptr) {
(*root) = righttree;
delete oldroot;
}
if ((*root)->left != nullptr && (*root)->right== nullptr) {
(*root) = lefttree;
delete oldroot;
}
if ((*root)->right != nullptr && (*root)->left != nullptr) {
if (find->right == nullptr) {
oldroot = find;
oldroot->right = righttree;
delete deleteroot;
}
else {
if (find->right != nullptr) {
while (find->right != nullptr) {
find = find->right;
}
if (find->left == nullptr) { ----> тут вибиває помилку Вызвано исключение: нарушение доступа
для чтения.
lefttree было 0xDDDDDDDD.
oldroot->index = find->index;
delete find;
if (lefttree->right) {
while (lefttree->right) {
lefttree = lefttree->right;
}
lefttree->right = nullptr;
}
}
else {
if (find->left != nullptr) {
leaf* save = find->left;
oldroot->index = find->index;
delete find;
while (lefttree->right) {
lefttree = lefttree->right;
}
lefttree->right = save;
}
}
}
}
}
}
if ((*root)->index != num) {
leaf* begin = nullptr;
while (oldroot) {
begin = oldroot;
if (oldroot->index > num && oldroot->left !=nullptr) {
oldroot = oldroot->left;
if (oldroot->index == num) {
break;
}
}
else {
cout << "Індексу " << num << " не знайдено у дереві." << endl;
return;
}
if (oldroot->index < num && oldroot->right != nullptr) {
oldroot = oldroot->right;
if (oldroot->index == num) {
break;
}
}
else {
cout << "Індексу " << num << " не знайдено у дереві." << endl;
return;
}
}
if (oldroot->left == nullptr) {
begin->right = oldroot->right;
delete oldroot;
}
else {
if (oldroot->left != nullptr) {
leaf* seveoldrootright = oldroot->right;
leaf* clean = oldroot;
oldroot = oldroot->left;
if (oldroot->right == nullptr) {
begin->right = oldroot;
begin->right->right = seveoldrootright;
delete clean;
}
else {
if (oldroot->right != nullptr) {
while (oldroot->right != nullptr) {
oldroot = oldroot->right;
}
leaf* clean = oldroot;
if (clean->left == nullptr) {
begin->right->index = oldroot->index;
delete clean;
}
else {
if (clean->left != nullptr) {
leaf* savecleanleft = clean->left;
delete clean;
leaf* search = righttree->left;
while (search->right) {
search = search->right;
}
search->right = savecleanleft;
}
}
}
}
}
}
}
}