Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<vector>
  4. #include<cstdlib>
  5. #include<fstream>
  6. #include<string.h>
  7. #define ALPHABETS 26
  8. #define MAX_WORD_SIZE 100
  9. using namespace std;
  10.  
  11.  
  12. class TrieTreeNode
  13. {
  14.  
  15. public:
  16.  
  17. TrieTreeNode* parent;
  18.  
  19. TrieTreeNode* children[ALPHABETS];
  20. vector<int> occurrences;
  21. };
  22.  
  23. class TrieTree
  24. {
  25.  
  26. public:
  27.  
  28. TrieTreeNode* root;
  29. TrieTree();
  30. void tree(char text[], int index);
  31.  
  32. void PrintWord(TrieTreeNode* trie_tree, vector<char> printUtilVector);
  33. TrieTreeNode* searchWord(TrieTreeNode* trie_tree, char* text);
  34. void deleteWord(TrieTreeNode* trie_tree, char* word);
  35.  
  36.  
  37.  
  38. };
  39.  
  40. TrieTree::TrieTree()
  41. {
  42.  
  43. root = (TrieTreeNode*)calloc(1, sizeof(TrieTreeNode));
  44.  
  45. }
  46.  
  47. void TrieTree::tree(char text[], int index)
  48. {
  49.  
  50. vector<char> word(text, text + strlen(text));
  51. TrieTreeNode* temp = root;
  52. int i = 0;
  53.  
  54. while (i < word.size())
  55. {
  56.  
  57. if (temp->children[word[i] - 'a'] == NULL)
  58. {
  59.  
  60. temp->children[word[i] - 'a'] = (TrieTreeNode*)calloc(1, sizeof(TrieTreeNode));
  61.  
  62. temp->children[word[i] - 'a']->parent = temp;
  63.  
  64. }
  65.  
  66. temp = temp->children[word[i] - 'a'];
  67.  
  68. ++i;
  69.  
  70. }
  71.  
  72. temp->occurrences.push_back(index);
  73.  
  74. }
  75.  
  76. void TrieTree::PrintWord(TrieTreeNode* trie_tree, vector<char>printUtil)
  77. {
  78.  
  79. int i, j;
  80. string inword;
  81. char AAA[1];
  82.  
  83. bool noChild = true;
  84.  
  85. vector<int>::iterator itr = trie_tree->occurrences.begin();
  86.  
  87. for (i = 0; i < ALPHABETS; ++i)
  88. {
  89.  
  90. if (trie_tree->children[i] == NULL)
  91. {
  92.  
  93. continue;
  94.  
  95. }
  96.  
  97. else
  98. {
  99.  
  100. noChild = false;
  101.  
  102. printUtil.push_back('a' + i);
  103. PrintWord(trie_tree->children[i], printUtil);
  104. printUtil.pop_back();
  105. }
  106.  
  107. }
  108.  
  109. if (trie_tree->occurrences.size() != 0)
  110. {
  111.  
  112. vector<char>::iterator itr = printUtil.begin();
  113. ofstream outputFile("tree.txt", ios::out);
  114.  
  115. if (!outputFile)
  116. {
  117.  
  118. cout << "Error !! some thing wrong during opening file:/" << endl;
  119.  
  120.  
  121. }
  122.  
  123. while (itr != printUtil.end())
  124. {
  125.  
  126. cout << *itr;
  127.  
  128. ++itr;
  129.  
  130.  
  131. }
  132.  
  133. outputFile << "*" << endl << "&";
  134.  
  135. cout << "\n";
  136.  
  137. }
  138.  
  139. printUtil.pop_back();
  140.  
  141. }
  142.  
  143. TrieTreeNode* TrieTree::searchWord(TrieTreeNode* trie_tree, char* text)
  144. {
  145.  
  146. vector<char>word(text, text + strlen(text));
  147. TrieTreeNode* temp = trie_tree;
  148.  
  149. while (word.size() != 0)
  150. {
  151.  
  152. if (temp->children[word[0] - 'a'] != NULL)
  153. {
  154.  
  155. temp = temp->children[word[0] - 'a'];
  156. word.erase(word.begin());
  157. }
  158.  
  159. else
  160.  
  161. {
  162.  
  163. break;
  164.  
  165. }
  166.  
  167. }
  168.  
  169. if (word.size() == 0 && temp->occurrences.size() != 0)
  170. {
  171.  
  172. return temp;
  173.  
  174. }
  175.  
  176. else
  177. {
  178.  
  179. return NULL;
  180.  
  181. }
  182.  
  183. }
  184.  
  185. void TrieTree::deleteWord(TrieTreeNode* trie_tree, char* word)
  186.  
  187. {
  188.  
  189. TrieTreeNode* temp = searchWord(trie_tree, word);
  190.  
  191. if (temp == NULL)
  192. {
  193.  
  194. cout << "not found\n";
  195. return;
  196. }
  197.  
  198. temp->occurrences.pop_back();
  199. bool noChild = true;
  200. int childCount = 0; int i;
  201.  
  202. for (i = 0; i < ALPHABETS; ++i)
  203. {
  204.  
  205. if (temp->children[i] != NULL)
  206. {
  207.  
  208. noChild = false;
  209.  
  210. ++childCount;
  211.  
  212. }
  213.  
  214. }
  215.  
  216. if (!noChild)
  217. {
  218.  
  219. return;
  220.  
  221. }
  222.  
  223. TrieTreeNode* traverse;
  224.  
  225. while (temp->occurrences.size() == 0 && temp->parent != NULL && childCount < 2)
  226. {
  227.  
  228. traverse = temp->parent;
  229.  
  230. for (i = 0; i < ALPHABETS; ++i)
  231. {
  232.  
  233. if (temp == traverse->children[i])
  234. {
  235.  
  236. traverse->children[i] = NULL;
  237. break;
  238.  
  239. }
  240.  
  241. }
  242.  
  243. free(temp);
  244. temp = traverse;
  245.  
  246. for (i = 0; i < ALPHABETS; ++i)
  247. {
  248.  
  249. if (temp->children[i] != NULL)
  250. {
  251.  
  252. ++childCount;
  253.  
  254. }
  255.  
  256. }
  257.  
  258. }
  259.  
  260. }
  261.  
  262. int main()
  263.  
  264. {
  265.  
  266. int number, i, j, n;
  267.  
  268. char inword[1][MAX_WORD_SIZE];
  269.  
  270. char words[6000][MAX_WORD_SIZE];
  271. char ensertword[1][MAX_WORD_SIZE];
  272. char wantedword[MAX_WORD_SIZE][MAX_WORD_SIZE];
  273. TrieTreeNode* temp;
  274.  
  275. TrieTree trie;
  276. vector<char> util;
  277. ifstream inputFile("D:\\#DEVELOPING Home\\C++ PROJECTS\\Trie Tree\\wordsEn.txt", ios::in);
  278. if (!inputFile)
  279. {
  280.  
  281. cout << "Error !! file not found :|" << endl;
  282. return 0;
  283. }
  284.  
  285. for (i = 0; i < 6000; i++)
  286. {
  287.  
  288. inputFile >> words[i];
  289. trie.tree(words[i], i + 1);
  290. }
  291.  
  292. for (; 1 == 1;)
  293. {
  294.  
  295. cout << "\nEXIT = 0\nINSERT = 1\nDELETE = 2\nSEARCH = 3\nPRINT = 4\nSPEEL_CHECHER = 5\n";
  296. cout << "\npleas enter number : ";
  297. cin >> number;
  298.  
  299. if (number == 0)
  300. {
  301.  
  302. cout << "\nEXIT\nGood Luck :)\n";
  303.  
  304. break;
  305.  
  306. }
  307.  
  308. if (number == 1)
  309. {
  310.  
  311. cout << "INSERT\npleas enter word : ";
  312. cin >> ensertword[0];
  313. trie.tree(ensertword[0], i + 1);
  314. }
  315.  
  316. if (number == 2)
  317. {
  318.  
  319. cout << "DELETE\npleas enter word : ";
  320. cin >> inword[0];
  321. trie.deleteWord(trie.root, inword[0]);
  322. }
  323.  
  324. if (number == 3)
  325. {
  326.  
  327. cout << "SEARCH\npleas enter word : ";
  328. cin >> inword[0];
  329. temp = trie.searchWord(trie.root, inword[0]);
  330.  
  331. if (temp == NULL)
  332. {
  333.  
  334. cout << "not found\n";
  335.  
  336. }
  337.  
  338. else
  339. {
  340.  
  341. cout << "found\n";
  342.  
  343. }
  344.  
  345. }
  346.  
  347. if (number == 4)
  348. {
  349.  
  350. cout << "PRINT \n";
  351. trie.PrintWord(trie.root, util);
  352. }
  353.  
  354. if (number == 5)
  355. {
  356.  
  357. long start, ende;
  358.  
  359. ifstream inputFile("wordsEn.txt", ios::in);
  360.  
  361. if (!inputFile)
  362. {
  363.  
  364. cout << "Error !! file not found :|" << endl;
  365. return 0;
  366. }
  367.  
  368. cout << "enter number how much is word ? ";
  369. cin >> n;
  370. cout << "if your word is not found in txt show word in ()\n ";
  371.  
  372. for (j = 0; j < n; j++)
  373. {
  374.  
  375. inputFile >> wantedword[j];
  376. temp = trie.searchWord(trie.root, wantedword[j]);
  377.  
  378. if (temp == NULL)
  379. {
  380.  
  381. cout << "(" << wantedword[j] << ")";
  382.  
  383. }
  384.  
  385. else
  386. {
  387.  
  388. cout << " " << wantedword[j] << " ";
  389.  
  390. }
  391.  
  392. }
  393.  
  394. }
  395.  
  396. }
  397.  
  398. return 0;
  399.  
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement