그 ¶
구 .......
갈, 구 게 ....... ......
갈, 구 게 ....... ......
¶
~cpp #include <iostream> using namespace std; #include <fstream> struct node{ node * left_child; char data_word[30]; node * right_child; }; class SearchWord { private: node * head; node * temphead; int count; public: SearchWord(); void ReadWord(); node * Search(char *, node *); void InsertWord(char *, node *); void Display(node *); }; SearchWord::SearchWord() { count = 0; } // void SearchWord::ReadWord() { ifstream file; char line[300]; char * word = NULL; file.open("test.txt"); while(!file.eof()) { file.getline(line, 300); cout << line << "\n"; word = strtok(line, " ."); count ++; // temphead = head; temphead = Search(word, temphead); // if(temphead == NULL) { temphead = head; InsertWord(word, temphead); } // else { } while(word) { cout << word << "\n"; word = strtok(0, " ."); count++; // temphead = head; temphead = Search(word, temphead); // if(temphead == NULL) { temphead = head; InsertWord(word, temphead); } // else { } } } cout << "count : " << count << "\n"; } // , NULL node * SearchWord::Search(char * word, node * root) { if(!root) return NULL; if(!strcmp(word, root->data_word)) return root; if(strcmp(word, root->data_word) < 0) return Search(word, root->left_child); return Search(word, root->right_child); } void SearchWord::InsertWord(char * word, node * root) { if(!root) { head = new node; strcpy(head->data_word, word); head->left_child = head->right_child = NULL; } else { while(root != NULL) { if(strcmp(word, root->data_word) < 0) root = root->left_child; else root = root->right_child; } node * temp = new node; strcpy(temp->data_word, word); temp->left_child = temp->right_child = NULL; root = temp; } } void SearchWord::Display(node * tree) { cout << tree->data_word << "\n"; } int main() { SearchWord word; SearchWord(); word.ReadWord(); return 0; }