Advertisement
Guest User

TreeNode

a guest
Dec 6th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. bool is_operator(char c) {
  8.     return c == '+' || c == '-' || c == '*';
  9. }
  10. typedef struct Tree_Node Tree_Node;
  11. struct Tree_Node {
  12.     char label;
  13.     Tree_Node* left;
  14.     Tree_Node* right;
  15.     bool value;
  16. };
  17. Tree_Node* new_Tree_Node(char label) {
  18.     Tree_Node* root = (Tree_Node*) malloc(sizeof(Tree_Node));
  19.     root->label = label;
  20.     root->left = nullptr;
  21.     root->right = nullptr;
  22.     root->value = (int) NULL;
  23. //    if(!is_operator(label))
  24. //        root->value = label-'0';
  25.     root->value = false;
  26.     return root;
  27. }
  28.  
  29. void inorder(Tree_Node* root) {
  30.     if(root == NULL)
  31.         return;
  32.     inorder(root->left);
  33.     printf("%c ", root->label);
  34.     inorder(root->right);
  35. }
  36.  
  37. long long gcd(long long a, long long b) {
  38.     if(a == b)
  39.         return a;
  40.     long long bigger = max(a,b);
  41.     long long smaller = min(a,b);
  42.     if(bigger % smaller == 0)
  43.         return smaller;
  44.     return gcd(smaller, bigger % smaller);
  45.  
  46. }
  47. int main() {
  48.     ios_base::sync_with_stdio(false);
  49.     int n;
  50.     cin >> n;
  51.     bool arr_bool[27];
  52.     for(int i = 65; i < 65+n; i++) {
  53.         char c;
  54.         cin >> c;
  55.         arr_bool[i - 65] = c == 'T';
  56.     }
  57.  
  58.     string postfix_str;
  59.     getline(cin, postfix_str);
  60.     getline(cin, postfix_str);
  61. //    cout << postfix_str << endl;
  62. //    cout <<" end";
  63.     int post_fix_size = 0;
  64.     int postfix_ind = 0;
  65.     char postfix[250];
  66. //    cout << "printing postfix_str:\n";
  67. //    cout << postfix_str;
  68. //    for(char c = po)
  69.     for(char i : postfix_str) {
  70.         if(i != ' ') {
  71.             postfix[postfix_ind] = i;
  72.             postfix_ind++;
  73.             post_fix_size++;
  74.         }
  75.     }
  76. //    cout << "printing postfix:\n";
  77. //    for(int i = 0; i < post_fix_size; i++)
  78. //        cout << postfix[i];
  79.     stack<Tree_Node*> nodes;
  80.     for(int i = 0; i < post_fix_size; i++) {
  81.         char current_char = postfix[i];
  82.         if(!is_operator(current_char)) {
  83.             Tree_Node* t = new_Tree_Node(current_char);
  84.             t->value = arr_bool[current_char - 65];
  85.             nodes.push(t);
  86.         }
  87.         else {
  88.             if(current_char != '-') {
  89.                 Tree_Node* t = new_Tree_Node(current_char);
  90.                 Tree_Node* t1 = nodes.top();
  91.                 nodes.pop();
  92.                 Tree_Node* t2 = nodes.top();
  93.                 nodes.pop();
  94.                 t->left = t2;
  95.                 t->right = t1;
  96.                 switch(current_char) {
  97.                     case '+': {
  98.                         bool res = t1->value | t2->value;
  99.                         t->value = res;
  100.                         break;
  101.                     }
  102.                     case '*': {
  103.                         bool res = t1->value & t2->value;
  104.                         t->value = res;
  105.                         break;
  106.                     }
  107.                 }
  108.                 nodes.push(t);
  109.             }
  110.             else {
  111.                 Tree_Node* t1 = nodes.top();
  112.                 t1->value = !(t1->value);
  113. //                Tree_Node *t = new_Tree_Node(current_char); //new node with operator
  114. //                Tree_Node *t1 = nodes.top(); //pop the last two values
  115. //                nodes.pop();
  116. //                t->value = !(t->value);
  117. //                nodes.push(t);
  118.             }
  119.         }
  120.     }
  121.  
  122. //    cout << " IDEMO ODMA \n";
  123.     Tree_Node* t = nodes.top();
  124. //    inorder(t);
  125. //    cout << "endl\n";
  126. //    cout << "label is " << t->label;
  127.     if(!t->value) {
  128.         cout <<"F";
  129.         return 0;
  130.     }
  131.     cout << "T";
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement