35 #ifndef TREESTRING_HPP
36 #define TREESTRING_HPP
40 #include <forward_list>
45 using std::forward_list;
46 using std::stringstream;
75 virtual const char*
what()
const throw(){
113 Node(
char data,
int frequency):
194 int heights[2] = {0,0};
198 heights[1] = 1 + child->height();
200 if(heights[0] < heights[1]){
202 heights[0] = heights[1];
220 if(n_data == (*it)->_tag){
221 (*it)->_wordFrequency += frequency;
229 tmp =
new Node(n_data, frequency);
247 return string(&
_tag);
250 string desc = string(&
_tag);
252 desc +=
", " + child->toString();
262 void toList(forward_list<string> &words,
string word){
265 words.push_front(word);
269 child->toList(words, word);
293 child->toFrequencedList(words, word);
338 void put(
const string &word){
342 int i_end = word.size()-1;
344 for(i=0; i<i_end; ++i){
346 lastInserted = lastInserted->
append(word[i], 0);
349 lastInserted->
append(word[i], 1);
379 #endif // TREESTRING_HPP
void append(T n_data)
Definition: tree.hpp:190
void getWordsFrequencies(forward_list< pair< string, int >> &words)
Definition: treestring.hpp:373
Defines tree nodes.
Definition: tree.hpp:82
int _wordFrequency
Definition: treestring.hpp:92
int height()
Definition: treestring.hpp:187
forward_list< Node< T > > _children
children of the Node
Definition: tree.hpp:90
void toFrequencedList(forward_list< pair< string, int >> &words, string word)
Definition: treestring.hpp:278
char _tag
letter stored into Node, the tag
Definition: treestring.hpp:94
Node * append(const char n_data, int frequency)
Definition: treestring.hpp:214
string toString()
Definition: treestring.hpp:245
void getWords(forward_list< string > &list)
Definition: treestring.hpp:364
T _tag
letter stored into Node, the tag
Definition: tree.hpp:88
Tree is a recursive structure using nodes.
Definition: treestring.hpp:304
virtual ~TreeStringException()
Definition: treestring.hpp:68
Node()
Definition: treestring.hpp:124
Node(char data, int frequency)
Definition: treestring.hpp:113
TreeStringException(char *cause)
Definition: treestring.hpp:61
bool isLeaf()
Definition: treestring.hpp:179
forward_list< Node * > _children
children of the Node
Definition: treestring.hpp:96
char getTag()
Definition: treestring.hpp:239
void toList(forward_list< string > &words, string word)
Definition: treestring.hpp:262
Node & operator=(const Node &other)
Definition: treestring.hpp:145
int height()
Definition: treestring.hpp:331
~TreeString()
Definition: treestring.hpp:324
TreeString(const TreeString &other)
Definition: treestring.hpp:318
exception class for trees
Definition: treestring.hpp:53
int _childNbr
Number of children.
Definition: tree.hpp:86
string toString()
Definition: treestring.hpp:356
void put(const string &word)
Definition: treestring.hpp:338
virtual const char * what() const
Definition: treestring.hpp:75
~Node()
Definition: treestring.hpp:135
bool operator==(const Node &rhs)
Definition: treestring.hpp:161
Node _root
Definition: treestring.hpp:307
Node(const Node &other)
Definition: treestring.hpp:102
char * _cause
Definition: treestring.hpp:55
TreeString()
Definition: treestring.hpp:312
bool operator!=(const Node &rhs)
Definition: treestring.hpp:171