Vla_DOS

Untitled

Jun 10th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int data;
  6.     struct Node* left;
  7.     struct Node* right;
  8. };
  9.  
  10.  
  11. void Postorder(Node* root) {
  12.     if (root == NULL) return;
  13.  
  14.     Postorder(root->left);
  15.     Postorder(root->right);
  16.     cout << root->data << " ";
  17. }
  18.  
  19. Node* Insert(Node* root, int data) {
  20.     if (root == NULL) {
  21.         root = new Node();
  22.         root->data = data;
  23.         root->left = root->right = NULL;
  24.     }
  25.     else if (data <= root->data)
  26.         root->left = Insert(root->left, data);
  27.     else
  28.         root->right = Insert(root->right, data);
  29.     return root;
  30. }
  31.  
  32. int main() {
  33.     setlocale(0, "");
  34.     Node* root = NULL;
  35.     int size = 0;
  36.     cout << "Array size: ";
  37.     cin >> size;
  38.     int* arr = new int[size];
  39.  
  40.     for (int i = 0; i < size; i++) {
  41.         arr[i] = rand() % 10;
  42.     }
  43.     for (int i = 0; i < size; i++) {
  44.         root = Insert(root, arr[i]);
  45.     }
  46.     for (int i = 0; i < size; i++) {
  47.         cout << arr[i] << " ";
  48.     }
  49.     cout << "\n";
  50.     cout << "Eлементи дерева в префiксному порядку: " << endl;
  51.     Postorder(root);
  52.     system("Pause");
  53. }
Advertisement
Add Comment
Please, Sign In to add comment