193030

SAA. Kontrolno 2, zad 1, characters and '*'

Dec 2nd, 2020 (edited)
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. //string input = "amd*fu***e**ckl**r***";
  6.  
  7.  
  8. // The input is from the console
  9.  
  10. struct Node
  11. {
  12.     struct Node* lchild;
  13.     char data;
  14.     struct Node* rchild;
  15. };
  16.  
  17. typedef Node* point;
  18. point Root;
  19. void Create_B_Tree(point &P) {
  20.   char x;
  21.   cin >> x;
  22.   if (x == '*') P = NULL;
  23.   else {
  24.     P = new Node;
  25.     P -> data = x;
  26.     cout << "left of " << P->data << "=";
  27.     Create_B_Tree(P -> lchild);
  28.     cout << "right of" << P->data << "=";
  29.     Create_B_Tree(P -> rchild);
  30.   }
  31. }
  32.  
  33.  
  34.  
  35. int height(struct Node *p)
  36. {
  37.     if(p)
  38.     {
  39.         return 1+ max(height(p->rchild),height(p->lchild));
  40.     }
  41.         return 0;
  42. }
  43.  
  44. /* Print nodes at a given level */
  45. void printGivenLevel(struct Node* p, int level)
  46. {
  47.     if (p == NULL)
  48.         return;
  49.     if (level == 1)
  50.         printf("%c ", p->data);
  51.     else if (level > 1)
  52.     {
  53.         printGivenLevel(p->lchild, level-1);
  54.         printGivenLevel(p->rchild, level-1);
  55.     }
  56. }
  57.  
  58. void printLevelOrder(struct Node* p)
  59. {
  60.     int h = height(p);
  61.     int i;
  62.     for (i=1; i<=h; i++)
  63.     {
  64.         printGivenLevel(p, i);
  65.         printf("\n");
  66.     }
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72.     cout << "Enter the whole input" << endl;
  73.     Root = NULL;
  74.     Create_B_Tree(Root);
  75.     cout << endl;
  76.     printLevelOrder(Root);
  77. }
  78.  
RAW Paste Data Copied