Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct tree
- {
- int D;
- tree* l;
- tree* r;
- };
- void add(tree*& top, int element)
- {
- if (top == NULL)
- {
- top = new tree();
- top->D = element;
- top->l = NULL;
- top->r = NULL;
- }
- else if (element < top->D) add(top->l, element);
- else add(top->r, element);
- }
- void print_pramoi(tree* top)
- {
- if (top != NULL)
- {
- cout << " " << top->D;
- print_pramoi(top->l);
- print_pramoi(top->r);
- }
- }
- void print_simetr(tree* top)
- {
- if (top != NULL)
- {
- print_simetr(top->l);
- cout << " " << top->D;
- print_simetr(top->r);
- }
- }
- void print_obr(tree* top)
- {
- if (top != NULL)
- {
- print_obr(top->l);
- print_obr(top->r);
- cout << " " << top->D;
- }
- }
- void delet(tree* top)
- {
- if (top != NULL)
- {
- delet(top->l);
- delet(top->r);
- delete top;
- top = NULL;
- }
- }
- void part_A(tree* top,int &kolvo)
- {
- if (top != NULL )
- {
- part_A(top->l,kolvo);
- if (top->l != NULL && top->r == NULL)
- if (top->l->l == NULL && top->l->r == NULL)
- kolvo++;
- part_A(top->r,kolvo);
- }
- }
- tree *found_before_max_in_Right_tree(tree*top)
- {
- while (top->r->r != NULL)
- top = top->r;
- return top;
- }
- void part_B_change_elements(tree*&top_main)
- {
- if(top_main->r==NULL){
- cout<<"Right podderevo is empty! Replace is unreal";
- exit(1);
- }
- if(top_main==NULL){
- cout<<"Tree is empty!";
- exit(1);
- }
- if(top_main->r->r==NULL)
- {
- tree* max=top_main->r;
- tree* max_l=max->l;
- tree* top=top_main;
- tree* top_l=top_main->l;
- top_main=max;
- max->l=top_l;
- max->r=top;
- top->l=max_l;
- top->r=NULL;
- return;
- }
- tree* top=top_main;
- tree* before_max=found_before_max_in_Right_tree(top);
- tree* max=before_max->r;
- tree* top_l=top_main->l;
- tree* top_r=top_main->r;
- tree* max_l=max->l;
- tree* max_r=max->r;
- top=top_main;
- top_main=max;
- max->l=top_l;
- max->r=top_r;
- before_max->r=top;
- top->r=max_r;
- top->l=max_l;
- }
- int main()
- {
- tree* top = NULL;
- cout << "Write number of elements: "; int n; cin >> n;
- int element;
- for (int i = 0; i < n; i++)
- {
- cin >> element;
- add(top, element);
- }
- print_simetr(top); cout << endl;
- cout << "Part A: ";
- int kolvo;
- part_A(top,kolvo); cout << kolvo << endl;
- part_B_change_elements(top);
- print_simetr(top);
- cout << endl;
- delet(top);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement