wintest

ИБД СЛавова

Jan 22nd, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. typedef struct Node *po;
  5.  
  6. int A[] = { 3, -2, 11, 7, 12, 1, 4, 5, 33, 13 };
  7. int elBroi = 10;
  8. int B[10];
  9. int i = 0;
  10. int jm = 0;
  11. po ibd(int n);
  12. void printTriTo(po help);
  13. struct Node {//всеки елемент на дървото
  14.     int Data;
  15.     po Left;
  16.     po Right;
  17. };
  18.  
  19. int main() {
  20.  
  21.     //броят на данните е известен
  22.     cout << "Im doing stuff" << endl;
  23.     po root;
  24.     root = ibd(elBroi);
  25.     cout << "Stuff has been done" << endl;
  26.  
  27.     cout << "Im pritining stuff" << endl;
  28.     printTriTo(root);
  29.     return 0;
  30.  
  31. }
  32. po ibd(int n) {
  33.     po pesho;
  34.     pesho = new Node;//нов елемент
  35.     if (n > 0) {
  36.         pesho = new Node;
  37.         pesho->Data = A[i];
  38.         i++;
  39.         pesho->Left = ibd(n / 2);//ляво
  40.         pesho->Right = ibd(n - n / 2 - 1);//дясно
  41.         return pesho;
  42.     }
  43.     else return NULL;//върни укакзател към нищото, листо
  44.     //алгоритъмът слага данни на потъване, но закача структурата на изплуване
  45. }
  46. void printTriTo(po help) {
  47.     if (help == NULL) return;
  48.  
  49.     //закачи ибд от ляво
  50.     printTriTo(help->Left);
  51.     //операция над корен
  52.     cout << help->Data << endl;
  53.     B[jm] = help->Data;
  54.     jm++;
  55.     //закачи ибд от дясно
  56.     printTriTo(help->Right);
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment