-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphabetTree.cpp
More file actions
128 lines (105 loc) · 3.82 KB
/
Copy pathAlphabetTree.cpp
File metadata and controls
128 lines (105 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Created by Kyle on 7/27/2020.
//
#define TOP_AMOUNT 10
#include "AlphabetTree.h"
// Default Constructor
AlphabetTree::AlphabetTree() {
this->topNode = new AlphabetNode(-1, nullptr);
}
// Adds a word into the tree
void AlphabetTree::addWord(string word) {
AlphabetNode* currentNode = topNode;
// Increase total word count
currentNode->incCount();
int stringPos = 0;
// Get to the last node in the word
while (currentNode != nullptr && stringPos < word.length()){
currentNode = currentNode->addSubletter(word.at(stringPos));
stringPos++;
}
// Increment the last letter
currentNode->incCount();
}
// Removes a word
void AlphabetTree::delWord(string word) {
AlphabetNode* currentNode = topNode;
// Get to the last node in the word
for (int stringPos = 0; stringPos < word.length(); stringPos++){
AlphabetNode* tempNext = currentNode->getSubletter(word.at(stringPos));
if(tempNext == nullptr)
return;
currentNode = tempNext;
}
// Need to decrement the last letter
currentNode->decCount();
// Decrement the word count as well
topNode->decCount();
}
// Returns the number of times the word shows up in the tree
unsigned int AlphabetTree::getCount(string word) {
AlphabetNode* currentNode = topNode;
// Get to the last node in the word
for (int stringPos = 0; stringPos < word.length(); stringPos++){
AlphabetNode* tempNext = currentNode->getSubletter(word.at(stringPos));
if(tempNext == nullptr)
return 0;
currentNode = tempNext;
}
return currentNode->getCount();
}
// Prints the top 10 most common words in the list
void AlphabetTree::printTopTen(){
stack<AlphabetNode *> nodeStack;
pair<int, AlphabetNode*> topTen[TOP_AMOUNT];
nodeStack.push(topNode);
while (!nodeStack.empty()){
AlphabetNode *currentNode = nodeStack.top();
nodeStack.pop();
unsigned int count = currentNode->getCount();
// Insertion sort into topTen
if(count > topTen[TOP_AMOUNT - 1].first && currentNode->getLetter() != -1){
topTen[TOP_AMOUNT - 1] = make_pair(count, currentNode);
for(int i = TOP_AMOUNT - 2; i >= 0; i--) {
if (topTen[i].first < count) {
topTen[i + 1] = topTen[i];
topTen[i] = make_pair(count, currentNode);
}
else
break;
}
}
// Push children of the currentNode to stack from right to left
for(auto iter = currentNode->getSubletters()->end(); iter != currentNode->getSubletters()->begin();){
iter--;
nodeStack.push(iter->second);
}
}
// Prints the top 10
for(int i = 0; i < TOP_AMOUNT; i++){
if(topTen[i].second == nullptr)
break;
cout << i+1 << ". \"" << topTen[i].second->getRootToSelfString() << "\" with " << topTen[i].first << " entries" << endl;
}
}
// Prints all words in the tree
void AlphabetTree::printWords(){
stack<AlphabetNode *> nodeStack;
nodeStack.push(topNode);
while (!nodeStack.empty()){
AlphabetNode *currentNode = nodeStack.top();
nodeStack.pop();
unsigned int tempCount = currentNode->getCount();
if(tempCount > 0)
cout << currentNode->getRootToSelfString() << ": " << tempCount << endl;
// Push children of the popped node to stack from right to left
for(auto iter = currentNode->getSubletters()->end(); iter != currentNode->getSubletters()->begin();){
iter--;
nodeStack.push(iter->second);
}
}
}
// Prints the number of times there have been words added
void AlphabetTree::printCount() {
cout << "There are " << topNode->getCount() << " (non-unique) words in this dataset." << endl;
}