Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7.     char element;
  8.     node *less, *more;
  9. };
  10.  
  11. node* create() {
  12.     node *tree;
  13.     tree = new node;
  14.     tree->more = NULL;
  15.     tree->less = NULL;
  16.     return tree;
  17. }
  18.  
  19. void Add(char  el, node *tree) {
  20.     if (tree == NULL) {
  21.         cout << "NULL - processing." << endl;
  22.         tree = create();
  23.         tree->element = el;
  24.         cout << tree->element << "  " << "Ready." << endl;
  25.     }
  26.     else {
  27.         cout << "Not NULL."<<endl;
  28.         if (el>tree->element) Add(el, tree->more);
  29.         else Add(el, tree->less);
  30.         cout << el;
  31.     }
  32.  
  33. }
  34.  
  35. void Print(node *tree, int u)
  36. {
  37.     if (tree == NULL) {
  38.         cout << "Pusto";
  39.         return; //Если дерево пустое - выходим
  40.     }
  41.     else
  42.     {  
  43.         cout << "Ne Pusto!";
  44.         Print(tree->less, ++u);//С помощью рекурсивного посещаем левое поддерево
  45.         for (int i = 0; i<u; ++i) cout << "|";
  46.         cout << tree->element << endl; //И показываем элемент
  47.         u--;
  48.     }
  49.     Print(tree->more, ++u); //С помощью рекурсии посещаем правое поддерево
  50. }
  51.  
  52. int main()
  53. {
  54.     node *head1;
  55.     int n; //Количество элементов
  56.     char s; //Число, передаваемое в дерево
  57.  
  58.     cout << "введите количество элементов  ";
  59.     cin >> n; //Вводим количество элементов
  60.  
  61.     head1 = NULL;
  62.     cout << head1;
  63.     for (int i = 0; i<n; ++i)
  64.     {
  65.         cout << "ведите число  ";
  66.         cin >> s; //Считываем элемент за элементом
  67.  
  68.         Add(s, head1); //И каждый кладем в дерево
  69.     }
  70.     cout << "You tree\n";
  71.     cout << head1;
  72.     Print(head1, 0);
  73.     cout << head1;
  74.     cin >> n;
  75.  
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement