прога падает. Не знаете, в чем причина?
#include <iostream>
using namespace std;
struct tree_elem
{
int data;
tree_elem *left, *right;
tree_elem(int val = 0)
{
left = nullptr;
right = nullptr;
data = val;
}
void print() const
{
if (left)
left->print();
cout << data << ' ';
if (right)
right->print();
}
void delete_tree()
{
if (this == nullptr)
return;
(this->left)->delete_tree();
(this->right)->delete_tree();
delete this;
}
};
//----------------------------------------------------------->
struct binary_tree
{
int size;
tree_elem * root;
binary_tree(int=0);
~binary_tree();
void add(int);
void remove(int);
void delete_tree();
bool find(int) const;
void print() const;
};
//----------------------------------------------------------->
binary_tree::binary_tree(int first_elem)
{
root = new tree_elem(first_elem);
size = 1;
}
binary_tree::~binary_tree()
{
root->delete_tree();
}
//----------------------------------------------------------->
void binary_tree::add(int new_elem)
{
tree_elem *curr = root;
while (curr->data != new_elem)
{
if (new_elem < curr->data) {
if (curr->left == nullptr) {
curr->left = new tree_elem(new_elem);
size++;
}
curr = curr->left;
}
else if (new_elem > curr->data) {
if (curr->right == nullptr) {
curr->right = new tree_elem(new_elem);
size++;
}
curr = curr->right;
}
}
}
//----------------------------------------------------------->
bool binary_tree::find(int tofind) const
{
tree_elem *curr = root;
while (curr && curr->data != tofind) {
if (tofind < curr->data)
curr = curr->left;
else
curr = curr->right;
}
if (curr)
return true;
return false;
}
//----------------------------------------------------------->
void binary_tree::remove(int toremove)
{
tree_elem *curr = root, *parent;
while (curr && curr->data != toremove) {
parent = curr;
if (toremove < curr->data)
curr = curr->left;
else
curr = curr->right;
}
if (curr == nullptr)
return;
else if (curr->left == nullptr) {
if (parent->left == nullptr)
parent->left = curr->right;
if (parent->right == nullptr)
parent->right = curr->right;
delete curr;
size--;
}
else if (curr->right == nullptr) {
if (parent->left == nullptr)
parent->left = curr->left;
if (parent->right == nullptr)
parent->right = curr->left;
delete curr;
size--;
}
else if (curr->left != nullptr && curr->right != nullptr)
{
tree_elem *curr = parent->right;
tree_elem *removed_element = curr;
curr = curr->left;
while (curr->right)
curr = curr->right;
this->remove(curr->data);
removed_element->data = curr->data;
}
}
//----------------------------------------------------------->
void binary_tree::print() const {root->print();}
//----------------------------------------------------------->
int main()
{
binary_tree tree;
for (int i=0; i<20; ++i)
tree.add(rand() % 20);
tree.print();
cout << endl;
tree.remove(5);
tree.print();
for (int i=0; i<15; ++i)
if (tree.find(i))
cout << i << " - " << "found" << endl;
else
cout << i << " - " << "not found" << endl;
}
Оберните код в теги: 3 символа ` до и после кода (в случае одиночной конструкции достаточно 1 ` с обеих сторон). Спасибо!
Я знаю....
неужели ошибка в коде?))
Ну ты такой прозорливый, ужас!
Обсуждают сегодня