Advertisement
Korotkodul

tree

Apr 12th, 2025
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <exception>
  4. #include <cmath>
  5. #include <unordered_set>
  6. #include <vector>
  7. #include <unordered_map>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. const int inf = 2e9;
  12.  
  13. struct node{
  14.     node* self;
  15.     node* parent;
  16.     node* l;
  17.     node* r;
  18.     int val;
  19.     node(node *par) : parent(par), l(nullptr), r(nullptr), self(this), val(inf) {}
  20.     node() : parent(nullptr), l(nullptr), r(nullptr), self(this),val (inf) {}
  21.     node(node *par, node *L, node *R) : parent(par), l(L), r(R), self(this), val(inf) {}
  22. };
  23.  
  24. struct tree{
  25.     vector <int> arr = {1, 2, 3, 4, 5}; //используется ТОЛЬКО для инициализациидерева
  26.     node main_root;
  27.     node create_subtree(node* par, int Li, int Ri) {
  28.         /*Ri == Li + 2 - работает по алгоритмы
  29.         Ri == Li + 1 - особ случ
  30.         Ri == Li - особ случ*/
  31.         node root(par);
  32.         root.val = this->arr[(Ri + Li) / 2];
  33.         node left_root;
  34.         node right_root;
  35.         if (Ri == Li) {
  36.             return root;
  37.         }
  38.         if (Ri == Li + 1) { //Ri - root, Li - left_root
  39.             left_root = create_subtree(root.self, Li, Li);
  40.             root.l = left_root.self;
  41.             return root;
  42.         }
  43.         left_root = create_subtree(root.self, Li, (Ri + Li) / 2 - 1);
  44.         right_root = create_subtree(root.self, (Ri + Li) / 2 + 1, Ri);
  45.         root.l = left_root.self;
  46.         root.r = right_root.self;
  47.         return root;
  48.     }
  49.     void create(vector <int> a) {
  50.         arr = a;
  51.         if (a.size() == 0) {
  52.             return;
  53.         }
  54.         int len = a.size();
  55.         create_subtree(nullptr, 0, len - 1);
  56.         return;
  57.     }
  58.     void output() {
  59.  
  60.         return;
  61.     }
  62.     void find_first(int*  v) {
  63.         return;
  64.     }
  65.     void find_nex(int* v) {
  66.         return;
  67.     }
  68.     void insert_after(int* v) { //а insert_defore надо?
  69.         return;
  70.     }
  71. };
  72.  
  73. int main() {
  74.     tree T;
  75.     vector <int> a = {1, 2, 3, 4, 5};
  76.     T.create(a);
  77.     T.output();
  78. }
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement