自定义字典树模板.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <map>
  2. #include <string>
  3. class Trie_std {
  4. private:
  5. struct diag {
  6. std::map<char, diag*> son;
  7. diag* father;
  8. char ch;
  9. int endcnt;
  10. diag()
  11. : son(), ch(0), endcnt(0), father(nullptr) {}
  12. diag(const std::map<char, diag*>& son, char ch, int endcnt)
  13. : son(son), ch(ch), endcnt(endcnt), father(nullptr) {}
  14. } *root;
  15. public:
  16. Trie_std() {
  17. root = new(diag);
  18. }
  19. void insert(std::string x) {
  20. int siz = x.size();
  21. diag* now = root;
  22. for (int i = 0; i < siz; i++) {
  23. if ((now->son.find(x[i])) == (now->son.end()))
  24. now->son[x[i]] = new(diag),
  25. now->son[x[i]]->father = now,
  26. now->son[x[i]]->ch = x[i];
  27. now = now->son[x[i]];
  28. }
  29. now->endcnt++;
  30. }
  31. bool find(std::string x) {
  32. int siz = x.size();
  33. diag* now = root;
  34. for (int i = 0; i < siz; i++) {
  35. if (now->son.find(x[i]) == now->son.end())
  36. return false;
  37. now = now->son[x[i]];
  38. }
  39. if (now->endcnt)
  40. return true;
  41. else
  42. return false;
  43. }
  44. bool erase(std::string x) {
  45. int siz = x.size();
  46. diag* now = root;
  47. for (int i = 0; i < siz; i++) {
  48. if (now->son.find(x[i]) == now->son.end())
  49. return false;
  50. now = now->son[x[i]];
  51. }
  52. diag* t;
  53. now->endcnt--;
  54. while (siz--) {
  55. t = now->father;
  56. if (!(now->son.size())) {
  57. t->son.erase(now->ch);
  58. delete now;
  59. }
  60. else return true;
  61. now = t;
  62. }
  63. return true;
  64. }
  65. };
  66. class Trie_lst {
  67. private:
  68. struct diag {
  69. std::map<char, diag*> son;
  70. diag* father;
  71. char ch;
  72. int endcnt;
  73. diag()
  74. : son(), ch(0), endcnt(0), father(nullptr) {}
  75. diag(const std::map<char, diag*>& son, char ch, int endcnt)
  76. : son(son), ch(ch), endcnt(endcnt), father(nullptr) {}
  77. } *root;
  78. public:
  79. Trie_lst() {
  80. root = new(diag);
  81. }
  82. void insert(char* x, int siz) {
  83. diag* now = root;
  84. for (int i = 0; i < siz; i++) {
  85. if ((now->son.find(x[i])) == (now->son.end()))
  86. now->son[x[i]] = new(diag),
  87. now->son[x[i]]->father = now,
  88. now->son[x[i]]->ch = x[i];
  89. now = now->son[x[i]];
  90. }
  91. now->endcnt++;
  92. }
  93. bool find(char* x, int siz) {
  94. diag* now = root;
  95. for (int i = 0; i < siz; i++) {
  96. if (now->son.find(x[i]) == now->son.end())
  97. return false;
  98. now = now->son[x[i]];
  99. }
  100. if (now->endcnt)
  101. return true;
  102. else
  103. return false;
  104. }
  105. bool erase(char* x, int siz) {
  106. diag* now = root;
  107. for (int i = 0; i < siz; i++) {
  108. if (now->son.find(x[i]) == now->son.end())
  109. return false;
  110. now = now->son[x[i]];
  111. }
  112. diag* t;
  113. now->endcnt--;
  114. while (siz--) {
  115. t = now->father;
  116. if (!(now->son.size())) {
  117. t->son.erase(now->ch);
  118. delete now;
  119. }
  120. else return true;
  121. now = t;
  122. }
  123. return true;
  124. }
  125. };