pb_jiang

binary tree deserializer

Jul 9th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. struct Node {
  6.     ll val;
  7.     Node *left;
  8.     Node *right;
  9.     ~Node()
  10.     {
  11.         delete left;
  12.         delete right;
  13.     }
  14. };
  15.  
  16. // all symbols
  17. // [0, val] means number
  18. // [1, x] means left bracket
  19. // [2, x] means right bracket
  20. // [3, x] means OEF
  21. using a2l = array<ll, 2>;
  22.  
  23. Node *deserialize(const string &s)
  24. {
  25.     ll idx = 0;
  26.     auto lookup_sym = [&](bool consume) -> a2l {
  27.         a2l ret;
  28.         ll nid = idx;
  29.         while (nid < s.size() && s[nid] == ' ')
  30.             ++nid;
  31.         if (nid == s.size()) {
  32.             ret = {3, 0};
  33.         } else if (s[nid] == ')') {
  34.             ret = {2, 0}, ++nid;
  35.         } else if (s[nid] == '(') {
  36.             ret = {1, 0}, ++nid;
  37.         } else {
  38.             ll val = 0;
  39.             while (nid < s.size() && s[nid] >= '0' && s[nid] <= '9') {
  40.                 val = val * 10 + s[nid] - '0';
  41.                 nid++;
  42.             }
  43.             ret = {0, val};
  44.         }
  45.         if (consume)
  46.             idx = nid;
  47.         return ret;
  48.     };
  49.     /*
  50.     for (auto x = lookup_sym(true); x[0] != 3; x = lookup_sym(true)) {
  51.         cerr << "symbol: " << x[0] << " val: " << x[1] << " pos: " << idx << endl;
  52.     }
  53.     return nullptr;
  54.     */
  55.  
  56.     function<Node *()> read_subtree = [&]() {
  57.         a2l sym = lookup_sym(true);
  58.         assert(sym[0] == 1);
  59.         sym = lookup_sym(true);
  60.         Node *n = nullptr;
  61.         if (sym[0] == 2) {
  62.             return n;
  63.         } else {
  64.             assert(sym[0] == 0);
  65.             n = new Node;
  66.             n->val = sym[1];
  67.             n->left = read_subtree();  // consume left child
  68.             n->right = read_subtree(); // consume right child
  69.         }
  70.         sym = lookup_sym(true); // consume right bracket
  71.         assert(sym[0] == 2);
  72.         return n;
  73.     };
  74.  
  75.     return read_subtree();
  76. }
  77.  
  78. /*
  79.        4
  80.     2     6
  81.   1  3 null  8
  82. */
  83. int main()
  84. {
  85.     string s = "(4 (2 (1 () ()) (3 () ())) (6 () (85 () ())))";
  86.     Node *n = deserialize(s);
  87.     assert(n->val == 4);
  88.     assert(n->left->val == 2);
  89.     assert(n->left->left->val == 1);
  90.     assert(n->left->right->val == 3);
  91.     assert(n->left->left->left == nullptr);
  92.     assert(n->left->left->right == nullptr);
  93.     assert(n->left->right->left == nullptr);
  94.     assert(n->left->right->right == nullptr);
  95.  
  96.     assert(n->right->val == 6);
  97.     assert(n->right->left == nullptr);
  98.     assert(n->right->right->val == 85);
  99.     assert(n->right->right->left == nullptr);
  100.     assert(n->right->right->right == nullptr);
  101.  
  102.     delete n;
  103.  
  104.     return 0;
  105. }
Add Comment
Please, Sign In to add comment