Advertisement
Guest User

Tree

a guest
Nov 15th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <new>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. const int MAX = 100;
  12. typedef bool BOOL;
  13. typedef string WORD;
  14.  
  15. /*
  16.     structure describing a word entry in the dictionary
  17. */
  18.  
  19. typedef struct Pair {
  20.     int count;                  /* frequency count for a particular word */
  21.     WORD w;                     /* the word itself */
  22. } Pair;
  23.  
  24. typedef struct entry {
  25.     Pair e;
  26.     struct entry* leftChild;    /* left sub-tree */
  27.     struct entry* rightChild;   /* right sub-tree */
  28.     struct entry* next;         /* next sibling */
  29.     struct entry* prev;       /* previous sibling */
  30.  
  31. } ENTRY;
  32.  
  33. /*
  34.     structure describing the dictionary
  35. */
  36.  
  37. typedef struct dict {
  38.     int maxEntries;     /* maximum number of entries allowed; this is an artificial limit */
  39.                         /* linked lists can be as big as you want. This limit ensures that   */
  40.                         /* this code tries to behave like the previous ones */
  41.  
  42.     int numWords;       /* number of words in the dictionary */
  43.     ENTRY* Words;       /* pointer to the entries in binary tree */
  44.     ENTRY* wordLen;     /* pointer to entries in thread */
  45. } DICT;
  46.  
  47. /*
  48.   you will have to write code for these 5 functions.
  49. */
  50.  
  51. ENTRY* LocateWord(DICT&, WORD);
  52. BOOL FullDictionary(DICT&);
  53. BOOL InsertWord(DICT&, WORD);
  54. WORD GetNextWord(void);
  55. void DumpDictionary(DICT&);
  56. void rebuildList(DICT&, ENTRY*);
  57.  
  58. /*
  59.   note that these are global variables so that they are already initialized to 0
  60.   do not write your functions so that they use these directly. All input variables MUST
  61.   be passed as parameters!!!!
  62. */
  63.  
  64. DICT dictionary = { MAX,0,0,0 };  /* your dictionary */
  65. WORD word;
  66.  
  67.  
  68.  
  69. BOOL InsertWord(DICT& dict, WORD word)
  70. {
  71.     /*
  72.       adds word to dictionary (tree and thread ), if word can't be added returns false else returns true
  73.     */
  74.  
  75.  
  76.  
  77.     ENTRY* parent = new(ENTRY);
  78.     ENTRY* current = new(ENTRY);
  79.    
  80.     current = NULL;
  81.  
  82.     parent->leftChild = nullptr;
  83.     parent->rightChild = nullptr;
  84.     ENTRY* Root = new (ENTRY);
  85.    
  86.     if (!FullDictionary(dictionary)) {
  87.         return false;
  88.     }
  89.     if ((dict.Words) == NULL) {
  90.         //ENTRY* Root = new (ENTRY);
  91.         Root->e.w = word;
  92.         Root->leftChild = NULL;
  93.         Root->rightChild = NULL;
  94.         dict.Words = &*Root;
  95.         return true;
  96.     }
  97.     while(Root){
  98.     if (std::lexicographical_compare(dictionary.Words->e.w.begin(), dictionary.Words->e.w.end(), word.begin(), word.end())) {    //Checks if a > b, if B > A make right child, else left
  99.         //while (parent->rightChild != NULL) {
  100.             if (parent->rightChild != NULL) {
  101.                 current = parent;
  102.                 parent = parent->rightChild;
  103.             }
  104.         //}
  105.        
  106.                 ENTRY* newNode = new(ENTRY);
  107.                 newNode->leftChild = nullptr;
  108.                 newNode->rightChild = nullptr;
  109.  
  110.                 parent->rightChild = newNode;
  111.                 newNode->e.w = word;
  112.                 return true;       
  113.     }
  114.     //left child insertion
  115.     else {
  116.         //while (parent->leftChild != NULL) {     //Tree left tra
  117.             if (parent->leftChild != NULL) {
  118.                 current = parent;
  119.                 parent = parent->leftChild;
  120.             }
  121.         //}
  122.                 ENTRY* newNode = new(ENTRY);
  123.                 newNode->leftChild = nullptr;
  124.                 newNode->rightChild = nullptr;
  125.  
  126.                 parent->leftChild = newNode; //broke here
  127.                 newNode->e.w = word;
  128.                 return true;
  129.                
  130.     }
  131.     }
  132. }  //End of insert
  133.  
  134. void DumpDictionary(DICT& dict)
  135. {
  136.     /*
  137.       display the contents of dictionary in sorted order as well as dumping thread elements
  138.     */
  139.    
  140.     //   Starts @ root, goes left until it finds a null. Once it's null, it will try to
  141.     //
  142.     //
  143.  
  144.     ENTRY* current = new(ENTRY);
  145.     current->leftChild = nullptr;
  146.     current->rightChild = nullptr;
  147.     current = &*dictionary.Words;
  148.     ENTRY* previous = new(ENTRY);
  149.     previous;
  150.     previous->leftChild = nullptr;
  151.     previous->rightChild = nullptr;
  152.    
  153.     while (current != NULL) {                                                            //Begin traversal
  154.  
  155.         if (current->leftChild == NULL) {                                               //IF null
  156.             cout << current->e.w;
  157.             current = current->rightChild;        
  158.         }
  159.         else {
  160.             previous = current->leftChild;        
  161.        
  162.             while (previous->rightChild != NULL && previous->rightChild != current) {    
  163.             previous = previous->rightChild;
  164.             }
  165.             if (previous->rightChild == NULL) {
  166.                 previous->rightChild = current;
  167.                 current = current->leftChild;
  168.             }
  169.             else {
  170.                 cout << current->e.w;
  171.                 previous->rightChild = NULL;
  172.                 current = current->rightChild;
  173.             }
  174.         }
  175.  
  176.     }
  177.  
  178. }
  179.  
  180. WORD GetNextWord(void)
  181. {
  182.     /*
  183.        will retrieve next word in input stream. Word is defined just as in assignment #1
  184.        returns WORD or empty string if no more words in input stream
  185.     */
  186.    
  187.  
  188.     word.clear();
  189.     int tmp = 0;
  190.     while (std::cin.good())                           //If next Char is good, will begin to build string
  191.     {
  192.         char ch = std::cin.get();
  193.         if (isalpha(ch))                             /* function test is ch  is A-Z, a-z */
  194.         {
  195.             word.push_back(ch);                     //Builds word   // <<---- Need to update. this isn't the word i need to put
  196.             word[tmp] = tolower(word[tmp]);             // Handling repeat words with upper and lower.
  197.             tmp++;
  198.            
  199.         }
  200.         else if (!isalpha(ch) && (tmp > 0))
  201.                 return word;
  202.         else {
  203.            
  204.             return "";                             //Word is built and returned
  205.         }
  206.     }
  207.  
  208. }
  209.  
  210. BOOL FullDictionary(DICT& dict)
  211. {
  212.     /*
  213.        if dictionary is full, return true else false
  214.      */
  215.    
  216.     if (MAX == (dict.numWords)) {
  217.         return false;
  218.     }
  219.     else return true;
  220.  
  221. }
  222.  
  223.  
  224.  
  225.     int main(void) {
  226.  
  227.         ENTRY* pos;
  228.         word = GetNextWord();
  229.         InsertWord(dictionary, word);
  230.         word = GetNextWord();
  231.         InsertWord(dictionary, word);
  232.         word = GetNextWord();
  233.         InsertWord(dictionary, word);
  234.         //word = GetNextWord();
  235.         //InsertWord(dictionary, word);
  236.     /*
  237.         cout << FullDictionary(dictionary);
  238.         while (1) {
  239.             word = GetNextWord();
  240.  
  241.             if (word.empty()) {
  242.                 DumpDictionary(dictionary);
  243.                 break;
  244.             }
  245.             if ((pos = LocateWord(dictionary, word)) != nullptr) {
  246.                 (pos->e).count++;
  247.                 rebuildList(dictionary, pos);
  248.             }
  249.             else
  250.                 if (!InsertWord(dictionary, word)) cout << "dictionary full" << word << "cannot be added\n";
  251.  
  252.         }
  253.         */
  254.         DumpDictionary(dictionary);
  255.        
  256.  
  257. /*
  258.       Below is testing the dump dictionary function  
  259.  
  260.  
  261.         ENTRY* w1 = new(ENTRY);
  262.         ENTRY* w2 = new(ENTRY);
  263.         ENTRY* w3 = new(ENTRY);
  264.         w1->leftChild = nullptr;
  265.         w1->rightChild = nullptr;
  266.         w2->leftChild = nullptr;
  267.         w2->rightChild = nullptr;
  268.         w3->leftChild = nullptr;
  269.         w3->rightChild = nullptr;
  270.  
  271.         word = GetNextWord();
  272.         dictionary.Words = &*w1;
  273.         w1->e.w = word;
  274.         w1->leftChild = w2;
  275.         w1->rightChild = w3;
  276.         word = GetNextWord();
  277.         w2->e.w = word;
  278.         word = GetNextWord();
  279.         w3->e.w = word;
  280. */
  281.     //  cout << w1->e.w << endl;
  282.     //  cout << dictionary.Words->rightChild->e.w << endl;
  283.     //  cout << dictionary.Words->leftChild->e.w << endl;
  284.     //DumpDictionary(dictionary);
  285.  
  286.         //cout << w1->leftChild->e.w;
  287.         //cout << w1->rightChild->e.w;
  288.  
  289.        // dictionary.Words->e.w = word;
  290.     //  cout << dictionary.Words;
  291.  
  292.  
  293.     //  cout << dictionary.Words->rightChild->e.w;
  294.  
  295.        
  296.    
  297.     return 0;
  298.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement