Advertisement
Sanlover

Трай дерево с удалением

Jun 28th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9.     vector<Node*> children;
  10.     vector<size_t> keys;
  11.     char symbol = '+';
  12. };
  13.  
  14. class TrieTree
  15. {
  16.     Node* root;
  17.    
  18.     void addMethod(Node* node, const string& word, const size_t& id, const size_t& key) {
  19.        
  20.         if (id >= word.size())
  21.             return;
  22.  
  23.         for (size_t j = 0; j < node->children.size(); j++)
  24.             if (node->children[j]->symbol == word[id])
  25.             {
  26.                 node->children[j]->keys.push_back(key);
  27.                 addMethod(node->children[j], word, id + 1, key);
  28.                 return;
  29.             }
  30.  
  31.         Node* tmp = new Node;
  32.         tmp->symbol = word[id];
  33.         tmp->keys.push_back(key);
  34.  
  35.         node->children.push_back(tmp);
  36.         addMethod(tmp, word, id + 1, key);
  37.  
  38.     };
  39.     void removeMethod(Node* node,const size_t& key) {
  40.  
  41.         for (size_t i = 0; i < node->children.size(); i++)
  42.         {
  43.             for (size_t j = 0; j < node->children[i]->keys.size(); j++)
  44.             {
  45.                 if (node->children[i]->keys[j] == key)
  46.                 {
  47.                     removeMethod(node->children[i], key);
  48.                     if (node->children[i]->keys.size() != 1)
  49.                         node->children[i]->keys.erase(node->children[i]->keys.begin() + j);
  50.                     else
  51.                         node->children.erase(node->children.begin() + i);
  52.                     return;
  53.                 }
  54.  
  55.             }
  56.         }
  57.  
  58.     };
  59.  
  60.     void getByKeyMethod(Node* node, string& word, const size_t& key)
  61.     {
  62.         for (size_t i = 0; i < node->children.size(); i++)
  63.         {
  64.             for (size_t j = 0; j < node->children[i]->keys.size(); j++)
  65.             {
  66.                 if (node->children[i]->keys[j] == key)
  67.                 {
  68.                     word += node->children[i]->symbol;
  69.                     getByKeyMethod(node->children[i], word, key);
  70.                     return;
  71.                 }
  72.  
  73.             }
  74.         }
  75.     }
  76.  
  77. public:
  78.     TrieTree()
  79.     {
  80.         root = new Node;
  81.         root->symbol = '-';
  82.     }
  83.     void add(const string& word, const size_t& key)
  84.     {
  85.         addMethod(root, word,0, key);
  86.     }
  87.     void remove(const size_t& key)
  88.     {
  89.         removeMethod(root,key);
  90.     }
  91.     string getByKey(const size_t& key)
  92.     {
  93.         string word;
  94.         getByKeyMethod(root, word, key);
  95.         return word;
  96.     }
  97. };
  98.  
  99.  
  100. int main()
  101. {
  102.     SetConsoleCP(1251);
  103.     SetConsoleOutputCP(1251);
  104.  
  105.     TrieTree a;
  106.     a.add("бобёр", 1);
  107.     a.add("лань", 2);
  108.     a.add("бобровый", 3);
  109.     a.add("боб", 4);
  110.     a.add("красный", 5);
  111.     a.add("краснознамённый", 6);
  112.  
  113.     cout << endl << "Before: " << endl;
  114.     for (size_t i = 1; i < 7; i++)
  115.     {
  116.         cout << i << "] " << a.getByKey(i) << endl;
  117.     }
  118.  
  119.     a.remove(4);
  120.     a.remove(6);
  121.  
  122.     cout << endl << "After: " << endl;
  123.     for (size_t i = 1; i < 7; i++)
  124.     {
  125.         cout << i << "] " << a.getByKey(i) << endl;
  126.     }
  127.  
  128.     return 0;
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement