Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <new>
- using namespace std;
- const int MAX = 100;
- typedef bool BOOL;
- typedef string WORD;
- /*
- structure describing a word entry in the dictionary
- */
- typedef struct Pair {
- int count; /* frequency count for a particular word */
- WORD w; /* the word itself */
- } Pair;
- typedef struct entry {
- Pair e;
- struct entry* leftChild; /* left sub-tree */
- struct entry* rightChild; /* right sub-tree */
- struct entry* next; /* next sibling */
- struct entry* prev; /* previous sibling */
- } ENTRY;
- /*
- structure describing the dictionary
- */
- typedef struct dict {
- int maxEntries; /* maximum number of entries allowed; this is an artificial limit */
- /* linked lists can be as big as you want. This limit ensures that */
- /* this code tries to behave like the previous ones */
- int numWords; /* number of words in the dictionary */
- ENTRY* Words; /* pointer to the entries in binary tree */
- ENTRY* wordLen; /* pointer to entries in thread */
- } DICT;
- /*
- you will have to write code for these 5 functions.
- */
- ENTRY* LocateWord(DICT&, WORD);
- BOOL FullDictionary(DICT&);
- BOOL InsertWord(DICT&, WORD);
- WORD GetNextWord(void);
- void DumpDictionary(DICT&);
- void rebuildList(DICT&, ENTRY*);
- /*
- note that these are global variables so that they are already initialized to 0
- do not write your functions so that they use these directly. All input variables MUST
- be passed as parameters!!!!
- */
- DICT dictionary = { MAX,0,0,0 }; /* your dictionary */
- WORD word;
- BOOL InsertWord(DICT& dict, WORD word)
- {
- /*
- adds word to dictionary (tree and thread ), if word can't be added returns false else returns true
- */
- ENTRY* parent = new(ENTRY);
- ENTRY* current = new(ENTRY);
- current = NULL;
- parent->leftChild = nullptr;
- parent->rightChild = nullptr;
- ENTRY* Root = new (ENTRY);
- if (!FullDictionary(dictionary)) {
- return false;
- }
- if ((dict.Words) == NULL) {
- //ENTRY* Root = new (ENTRY);
- Root->e.w = word;
- Root->leftChild = NULL;
- Root->rightChild = NULL;
- dict.Words = &*Root;
- return true;
- }
- while(Root){
- 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
- //while (parent->rightChild != NULL) {
- if (parent->rightChild != NULL) {
- current = parent;
- parent = parent->rightChild;
- }
- //}
- ENTRY* newNode = new(ENTRY);
- newNode->leftChild = nullptr;
- newNode->rightChild = nullptr;
- parent->rightChild = newNode;
- newNode->e.w = word;
- return true;
- }
- //left child insertion
- else {
- //while (parent->leftChild != NULL) { //Tree left tra
- if (parent->leftChild != NULL) {
- current = parent;
- parent = parent->leftChild;
- }
- //}
- ENTRY* newNode = new(ENTRY);
- newNode->leftChild = nullptr;
- newNode->rightChild = nullptr;
- parent->leftChild = newNode; //broke here
- newNode->e.w = word;
- return true;
- }
- }
- } //End of insert
- void DumpDictionary(DICT& dict)
- {
- /*
- display the contents of dictionary in sorted order as well as dumping thread elements
- */
- // Starts @ root, goes left until it finds a null. Once it's null, it will try to
- //
- //
- ENTRY* current = new(ENTRY);
- current->leftChild = nullptr;
- current->rightChild = nullptr;
- current = &*dictionary.Words;
- ENTRY* previous = new(ENTRY);
- previous;
- previous->leftChild = nullptr;
- previous->rightChild = nullptr;
- while (current != NULL) { //Begin traversal
- if (current->leftChild == NULL) { //IF null
- cout << current->e.w;
- current = current->rightChild;
- }
- else {
- previous = current->leftChild;
- while (previous->rightChild != NULL && previous->rightChild != current) {
- previous = previous->rightChild;
- }
- if (previous->rightChild == NULL) {
- previous->rightChild = current;
- current = current->leftChild;
- }
- else {
- cout << current->e.w;
- previous->rightChild = NULL;
- current = current->rightChild;
- }
- }
- }
- }
- WORD GetNextWord(void)
- {
- /*
- will retrieve next word in input stream. Word is defined just as in assignment #1
- returns WORD or empty string if no more words in input stream
- */
- word.clear();
- int tmp = 0;
- while (std::cin.good()) //If next Char is good, will begin to build string
- {
- char ch = std::cin.get();
- if (isalpha(ch)) /* function test is ch is A-Z, a-z */
- {
- word.push_back(ch); //Builds word // <<---- Need to update. this isn't the word i need to put
- word[tmp] = tolower(word[tmp]); // Handling repeat words with upper and lower.
- tmp++;
- }
- else if (!isalpha(ch) && (tmp > 0))
- return word;
- else {
- return ""; //Word is built and returned
- }
- }
- }
- BOOL FullDictionary(DICT& dict)
- {
- /*
- if dictionary is full, return true else false
- */
- if (MAX == (dict.numWords)) {
- return false;
- }
- else return true;
- }
- int main(void) {
- ENTRY* pos;
- word = GetNextWord();
- InsertWord(dictionary, word);
- word = GetNextWord();
- InsertWord(dictionary, word);
- word = GetNextWord();
- InsertWord(dictionary, word);
- //word = GetNextWord();
- //InsertWord(dictionary, word);
- /*
- cout << FullDictionary(dictionary);
- while (1) {
- word = GetNextWord();
- if (word.empty()) {
- DumpDictionary(dictionary);
- break;
- }
- if ((pos = LocateWord(dictionary, word)) != nullptr) {
- (pos->e).count++;
- rebuildList(dictionary, pos);
- }
- else
- if (!InsertWord(dictionary, word)) cout << "dictionary full" << word << "cannot be added\n";
- }
- */
- DumpDictionary(dictionary);
- /*
- Below is testing the dump dictionary function
- ENTRY* w1 = new(ENTRY);
- ENTRY* w2 = new(ENTRY);
- ENTRY* w3 = new(ENTRY);
- w1->leftChild = nullptr;
- w1->rightChild = nullptr;
- w2->leftChild = nullptr;
- w2->rightChild = nullptr;
- w3->leftChild = nullptr;
- w3->rightChild = nullptr;
- word = GetNextWord();
- dictionary.Words = &*w1;
- w1->e.w = word;
- w1->leftChild = w2;
- w1->rightChild = w3;
- word = GetNextWord();
- w2->e.w = word;
- word = GetNextWord();
- w3->e.w = word;
- */
- // cout << w1->e.w << endl;
- // cout << dictionary.Words->rightChild->e.w << endl;
- // cout << dictionary.Words->leftChild->e.w << endl;
- //DumpDictionary(dictionary);
- //cout << w1->leftChild->e.w;
- //cout << w1->rightChild->e.w;
- // dictionary.Words->e.w = word;
- // cout << dictionary.Words;
- // cout << dictionary.Words->rightChild->e.w;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement