Phoenix_x

Untitled

Apr 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Element
  7. {
  8.     int key;
  9.     Element *left;
  10.     Element *right;
  11. } *mainTree = NULL, *evenNumbers = NULL, *oddNumbers = NULL;
  12.  
  13. void Add(int number, Element* &t) //добавяне на възли
  14. {
  15.     if (t == NULL)
  16.     {
  17.         t = new Element;
  18.         t->key = number;
  19.         t->left = NULL;
  20.         t->right = NULL;
  21.     }
  22.  
  23.     else
  24.     {
  25.         if (t->key < number)
  26.         {
  27.             Add(number, t->right);
  28.         }
  29.  
  30.         else
  31.         {
  32.             Add(number, t->left);
  33.         }
  34.     }
  35. }
  36.  
  37. int Height(Element *t)
  38. {
  39.     if (t == NULL)
  40.         return -1;
  41.     int u = Height(t->left);
  42.     int v = Height(t->right);
  43.  
  44.     if (u>v)
  45.         return u + 1;
  46.     else
  47.         return v + 1;
  48. }
  49.  
  50. void Show(Element *t)
  51. {
  52.     if (t)
  53.     {
  54.         Show(t->left);
  55.         Show(t->right);
  56.         cout << t->key << "\t";
  57.     }
  58. }
  59.  
  60. void DividingATree(Element *t)
  61. {
  62.     if (t)
  63.     {
  64.         if (t->key % 2 == 1)
  65.         {
  66.             Add(t->key, oddNumbers);
  67.         }
  68.  
  69.         else
  70.         {
  71.             Add(t->key, evenNumbers);
  72.         }
  73.  
  74.         DividingATree(t->left);
  75.         DividingATree(t->right);
  76.     }
  77. }
  78.  
  79. void main()
  80. {
  81.     cout << "Enter only integer numbers, please. The program will print: the main tree, even numbers and odd numbers." << endl;
  82.     cout << "Enter oh to exit." << endl << endl;
  83.  
  84.     int number = 1;
  85.  
  86.     while (number != 0)
  87.     {
  88.         cout << "Enter an integer number: ";
  89.         cin >> number;
  90.  
  91.         if (number != 0 && number > 0)
  92.         {
  93.             Add(number, mainTree);
  94.         }
  95.     }
  96.  
  97.     cout << endl << "The main tree looks like this:" << endl;
  98.  
  99.     Show(mainTree);
  100.     DividingATree(mainTree);
  101.  
  102.     cout << endl << "Even numbers:" << endl;
  103.  
  104.     Show(evenNumbers);
  105.  
  106.     cout << endl << "Odd numbers:" << endl;
  107.  
  108.     Show(oddNumbers);
  109.  
  110.     cout << endl;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment