Advertisement
Guest User

Radix Tree - Rafael Rivas

a guest
Nov 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <Windows.h>
  5. using namespace std;
  6.  
  7. class Radix {
  8. private:
  9.     struct node {
  10.         node *key[26];
  11.         string word;
  12.         node(string word) :word(word) {
  13.             for (int i = 0; i < 26; i++) {
  14.                 key[i] = nullptr;
  15.             }
  16.         }
  17.     };
  18.     node* root;
  19. public:
  20.     Radix(){
  21.         root = new node("");
  22.     }
  23.     node* get_root() {
  24.         return root;
  25.     }
  26.     char* findsuf(string word1,string word2) {
  27.         string newsuf;
  28.         int n = 0;
  29.         int g = 0;
  30.         for (int i = 0; i < word2.size(); i++) {
  31.             if (word1.at(i) == word2.at(i)) {
  32.                 g = g + 1;
  33.             }
  34.             else
  35.                 break;
  36.         }
  37.         n = word1.size();
  38.         newsuf = word1.substr(g, n);
  39.         char *x = strdup(newsuf.c_str());
  40.         return x;
  41.     }
  42.     void insert(string word) {
  43.         node* aux = root;
  44.         node* act = root;
  45.         string newpref;
  46.         bool esultimo = 0;
  47.         int p = 0;
  48.         int r;
  49.         int t = word.size();
  50.         int idx_w = 0;
  51.         int idx_nodo = idx_w;
  52.         int i = 0;
  53.         int index = word.at(i) - 'a';
  54.         act = act->key[index];
  55.         bool proceso = false; // crear sufijo y prefijo
  56.         if (act == nullptr) {
  57.             aux->key[index] = new node(word);
  58.             proceso = true;
  59.         }
  60.         while (act != nullptr && !proceso) {
  61.             idx_nodo = idx_w;
  62.             for (i = 0; i < act->word.size(); i++) {
  63.                 int e = word.at(idx_w) - 'a';
  64.                 int a = act->word.at(i) - 'a';
  65.                 if (word.at(idx_w) != act->word.at(i) && act->key[e] == nullptr && act->key[a] == nullptr) {
  66.                     newpref = act->word.substr(0, i);
  67.                     aux->key[index] = new node(newpref);
  68.                     node* nuevo = aux->key[index];
  69.                     string n_word = word.substr(idx_nodo, word.size());
  70.                     char* E = findsuf(n_word, act->word);
  71.                     string SE(E);
  72.                     char* A = findsuf(act->word, n_word);
  73.                     string SA(A);
  74.                     nuevo->key[a] = act;
  75.                     nuevo->key[e] = new node(E);
  76.                     act->word = SA;
  77.                     proceso = true;
  78.                     break;
  79.                 }
  80.                 idx_w++;
  81.             }
  82.             aux = act;
  83.             index = word.at(idx_w) - 'a';
  84.             act = act->key[index];
  85.         }
  86.  
  87.         if (!proceso) {
  88.             if (t > idx_w) {
  89.                 string n_word = word.substr(idx_nodo, t);
  90.                 char* suf = findsuf(n_word, aux->word); // aguacate, agua => cate
  91.                 string SE(suf);
  92.                 aux->key[index] = new node(suf);
  93.             }
  94.         }
  95.     }
  96.     void gotoxy(int x, int y) {
  97.         HANDLE hcon;
  98.         hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  99.         COORD dwPos;
  100.         dwPos.X = x;
  101.         dwPos.Y = y;
  102.         SetConsoleCursorPosition(hcon, dwPos);
  103.     }
  104.     void mostrar(node* a,int x,int y) {
  105.         node*aux = a;
  106.         for (int i = 0; i < 26; i++) {
  107.             if (aux->key[i] != nullptr) {
  108.                 y++;
  109.                 gotoxy(x, y);
  110.                 if (y++) {
  111.                     x = x - 2;
  112.                 }
  113.                 switch (y)
  114.                 {
  115.                 case 2: { x = x - 3;
  116.  
  117.                 }
  118.                 //case 3: {
  119.                 //  x = x - 4;
  120.                 //}
  121.                 //case 4: {
  122.                 //  x = x - 3;
  123.                 //}
  124.                 //case 5:
  125.                 //{
  126.                 //  x = x - 2;
  127.                 //}
  128.                 }
  129.                 char *a = strdup(aux->key[i]->word.c_str());
  130.                 cout << a<<endl;
  131.                 mostrar(aux->key[i],x,y);
  132.                 y--;
  133.                 switch (y)
  134.                 {
  135.                 case 1: { x = x + 10;
  136.  
  137.                 }
  138.                 case 2: {
  139.                     x = x + 8;
  140.                 }
  141.                 case 3: {
  142.                     x = x + 6;
  143.                 }
  144.                 case 4:
  145.                 {
  146.                     x = x + 4;
  147.                 }
  148.                 case 5:
  149.                 {
  150.                     x = x + 2;
  151.                 }
  152.                 }
  153.  
  154.                 if (y--)
  155.                 {
  156.                     x = x + 7;
  157.                 }
  158.                
  159.             }
  160.         }
  161.     }
  162. };
  163.  
  164. void main() {
  165.     Radix* arbol = new Radix();
  166.     arbol->insert("jugar");
  167.     arbol->insert("juego");
  168.     arbol->insert("jugo");
  169.     arbol->insert("jugare");
  170.     arbol->insert("jugamos");
  171.     arbol->insert("ganamos");
  172.     arbol->insert("ganar");
  173.     arbol->insert("gane");
  174.     arbol->insert("gato");
  175.     arbol->insert("gata");
  176.     arbol->mostrar(arbol->get_root(),20,0);
  177.     _getch();
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement