Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- //создаем стрктуру дерева
- struct tree{
- int info;
- tree *right, *left;
- };
- //фукнция дерева
- void add_node(tree *node, int n)
- {
- if(n<node->info)
- {
- if(node->left==NULL)
- {
- tree *r = new tree;
- r->info = n;
- node->left = r;
- }
- else{add_node(node->left, n);}
- }
- else
- {
- if(node->right==NULL)
- {
- tree *r = new tree;
- r->info = n;
- node->right = r;
- }
- else{add_node(node->right, n);}
- }
- }
- void LNR(tree *node)
- {
- if(node==NULL){return;}
- else
- {
- LNR(node->left);
- cout << node->info << " ";
- LNR(node->right);
- }
- }
- int main()
- {
- int n;
- cout << "Введите длину массива: "; cin>>n;
- int *a = new int[n];
- for(int i = 0; i < n; i++)
- {
- cin >> a[i];
- }
- //объявляем корень дерева
- tree *root = new tree;
- int x;
- int i = 0;
- //проверка корневого элемента на четность
- while(i!=n)
- {
- if(a[i]%2==0){x=a[i]; break;}
- i++;
- if(i==n){cout<<"В исходном массиве отсутствуют четные числа"; return 0;}
- }
- root->left = NULL; root->right = NULL;
- root->info = x;
- i++;
- //проверка остальной части элементов массива на четность
- while(i<n)
- {
- x = a[i];
- if(x%2==0){add_node(root, x);}
- i++;
- }
- //вывод в отсортированном виде
- LNR(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement