Advertisement
nadya_misheva

9b

Dec 9th, 2022
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int COUNT = 0;
  6.  
  7. struct Element{
  8.     int data;
  9.     Element* left;
  10.     Element* right;
  11. };
  12.  
  13. Element* ibd(int n){
  14.     Element* d;
  15.     int x;
  16.     int lyavo = n/2;
  17.     int dyasno = n - lyavo - 1;
  18.     if(n>0){
  19.        d = new Element;
  20.        cin >> x;
  21.        d->data = x;
  22.        d->left = ibd(lyavo);
  23.        d->right = ibd(dyasno);
  24.        return d;
  25.     }
  26.     else return NULL;
  27. }
  28.  
  29. void print2(Element* root, int space)
  30. {
  31. COUNT = 10;
  32.     if (root == NULL)
  33.         return;
  34.  
  35.     space += COUNT;
  36.     print2(root->right, space);
  37.     cout << endl;
  38.     for (int i = COUNT; i < space; i++)
  39.         cout << " ";
  40.     cout << root->data << "\n";
  41.     print2(root->left, space);
  42. }
  43.  
  44. int main(){
  45.     int n;
  46.     cin >> n;
  47.     Element* durvo;
  48.     durvo = ibd(n);
  49.     print2(durvo,5);
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement