Помогите пожалуйста. Есть бинарное дерево, нужно сделать процедуру балансировки.
Код:
template <class T, class I> class node
{
private:
T x; //ключ
I info;//информация
node* LL; //left link
node* RL; //right link
public:
node(){ x = 0; LL = 0; RL = 0; };
~node()
{
if (LL) LL->~node();
if (RL) RL->~node();
if (LL) { delete LL; LL = 0; }
if (RL) { delete RL; RL = 0; }
}
void putx(T new_x, I new_info){ this->x = new_x; this->info = new_info; }
void null_leftlink(){ this->LL = 0; }
void null_rightlink(){ this->RL = 0; }
void add(T new_x, I new_info)
{
if (LL && (new_x < x)) LL->add(new_x, new_info);
if (RL && (new_x > x)) RL->add(new_x, new_info);
if (!LL && (new_x < x))
{
node* N = new node;
N->x = new_x;
N->info = new_info;
N->LL = 0;
N->RL = 0;
LL = N;
}
if (!RL && (new_x > x))
{
node* N = new node;
N->x = new_x;
N->info = new_info;
N->LL = 0;
N->RL = 0;
RL = N;
}
}
void print(int tab)
{
if (RL) RL->print(tab + 1);
for (int i = 1; i != tab; i++)cout << " "; cout << this->x << "-" << this->info << endl;
if (LL) LL->print(tab + 1);
}
void del_all()
{
if (LL) LL->del_all();
if (RL) RL->del_all();
if (LL) { delete LL; LL = 0; }
if (RL) { delete RL; RL = 0; }
}
void del(T x_to_del)
{
if (LL) LL->del();
if (RL) RL->del();
//не дописано еще.
}
I get(T getx)
{
if (getx == x) return info;
if (LL && (getx < x)) return LL->get(getx);
if (RL && (getx > x)) return RL->get(getx);
}
};
template <class T, class I> class tree
{
private:
node <T, I>* link;
public:
tree(){ link = 0; };
~tree(){ if (link)link->~node<T, I>(); delete link; link = 0; };
void add(T new_x, I new_info)
{
if (link) link->add(new_x, new_info);
else
{
node<T, I>* N = new node<T, I>;
N->putx(new_x, new_info);
N->null_leftlink();
N->null_rightlink();
link = N;
}
};
void print(){ if (link)link->print(1); else cout << "No tree existing\n"; }
void del_all(){ if (link) link->del_all(); delete link; link = 0; }
void del(int x){ if (link)link->del(x); };
I get(T x){ if (link) return link->get(x); else cout << "No elements existing\n"; }
};
int main()
{
system("cls");//clear screen
tree <int, char> *T = new tree <int, char>; // создание дерева
//menu
int choos = 0;
const int exit = 6;
while (choos != exit)
{
cout << "Hello! This is a 'tree' example\n"
"There is a menu for you to choos:\n"
" 1-add;\n"
" 2-use destructor;\n"
" 3-print;\n"
" 4-delete all;\n"
" 5-get element;\n"
" 6-exit;\n"
"enter-> ";
cin >> choos;
switch (choos){
case 1: {
int key; //ключ.
//char val; //значение
char val[10];
cout << "enter key: ";
cin >> key;
cout << "enter int value: "; cin >> val;
T->add(key, val[10]);
break;
}
case 2: T->~tree(); break;
case 3: T->print(); break;
case 4: T->del_all(); break;
case 5:{
cout << "what is key of element? ";
int key = 0;
cin >> key;
cout << "there it is: " << T->get(key) << endl;
break; }
default: if (choos != exit)cout << "wrong int value " << choos << endl;
}
}
}
Дана188, вы из принципа не оформляете код по правилам? Напоминаю - клавиша # вверху. Посмотрел несколько ваших тем - почти везде модераторы за вас это делают. Пора бы и научится
Модератор