Glossygloss  0.2
Glossygloss is set of classes to use several data structure as Tree or hash table.
 All Classes Files Functions Variables Macros Pages
treestring.hpp
Go to the documentation of this file.
1 
35 #ifndef TREESTRING_HPP
36 #define TREESTRING_HPP
37 
38 #include <cassert>
39 #include <string>
40 #include <forward_list>
41 #include <utility>
42 #include <sstream>
43 
44 using std::string;
45 using std::forward_list;
46 using std::stringstream;
47 using std::pair;
48 
53 class TreeStringException : std::exception {
54  private:
55  char* _cause;
56  public:
61  TreeStringException(char* cause):
62  _cause(cause)
63  {}
64 
68  virtual ~TreeStringException() throw(){
69  // do nothing
70  }
71 
75  virtual const char* what()const throw(){
76  return _cause;
77  }
78 };
79 
85 class Node {
86 
87  private:
89  int _childNbr;
94  char _tag;
96  forward_list<Node*> _children;
97 
98  public:
102  Node(const Node &other):
103  _childNbr(other._childNbr),
105  _tag(other._tag),
106  _children(other._children)
107  {}
108 
113  Node(char data, int frequency):
114  _childNbr(0),
115  _wordFrequency(frequency),
116  _tag(data)
117  {
118  // initialize the simply-linked list
119  _children = forward_list<Node*>();
120  }
121 
124  Node():
125  _childNbr(0),
126  _wordFrequency(0),
127  _tag('@')
128  {
129  // initialize the simply-linked list
130  _children = forward_list<Node*>();
131  }
132 
135  ~Node(){
136  // usefull ?
137  //delete &_children;
138  //delete &_tag;
139  }
140 
145  Node& operator=(const Node &other){
146  // prevent objet copying itself
147  if(this != &other){
148  this->_childNbr = other._childNbr;
149  this->_tag = other._tag;
150  this->_wordFrequency = other._wordFrequency;
151  this->_children = other._children;
152  }
153  return (*this); // allow a = b = c
154  }
155 
161  bool operator==(const Node& rhs){
162  // same adress ⇒ same item
163  return this == &rhs;
164  }
165 
171  bool operator!=(const Node &rhs){
172  return this != &rhs;
173  // return not(lhs == rhs);
174  }
175 
179  bool isLeaf(){
180  return 0 == _childNbr;
181  //return frenquency > 0;
182  }
183 
187  int height(){
188  if(isLeaf()){
189  return 0;
190  }
191  else {
192  // start heights at zero
193  // heights[0] : greatest height, heights[1] : computed height in the loop
194  int heights[2] = {0,0};
195  // for each child - using C++11 syntax
196  for(Node* child : _children){
197  // compute child height and store it
198  heights[1] = 1 + child->height();
199  // if computed height is greater then the old one
200  if(heights[0] < heights[1]){
201  // store it as the new greate one
202  heights[0] = heights[1];
203  } // else, nothing
204  }
205  return heights[0];
206  }
207  }
208 
214  Node* append(const char n_data, int frequency){
215  Node* tmp;
216  bool undone = true;
217  auto it = _children.begin();
218  // while n_data is not added
219  while(undone and it != _children.end()){
220  if(n_data == (*it)->_tag){
221  (*it)->_wordFrequency += frequency; // update word frenquency
222  tmp = (*it);
223  undone = !undone; // job is now done
224  }
225  ++it;
226  }
227  // if letter is not present, add it
228  if(undone){
229  tmp = new Node(n_data, frequency);
230  _children.push_front(tmp);
231  _childNbr++;
232  }
233  return tmp;
234  }
235 
239  char getTag(){ return _tag; }
240 
241 
245  string toString(){
246  if(isLeaf()){
247  return string(&_tag);
248  }
249  else {
250  string desc = string(&_tag);
251  for(Node* child: _children){
252  desc += ", " + child->toString();
253  }
254  return desc;
255  }
256  }
257 
262  void toList(forward_list<string> &words, string word){
263  '@' != _tag?word += _tag:word; // if it's not the root, add tag
264  if(0 < _wordFrequency){
265  words.push_front(word);
266  }
267  if(not isLeaf()){
268  for(Node* child : _children){
269  child->toList(words, word);
270  }
271  }
272  }
273 
278  void toFrequencedList(forward_list<pair<string,int>> &words, string word){
279  // if it's not the root, add tag
280  // avoid to prefix words with '@', the root char
281  '@' != _tag?word += _tag:word;
282  // if it's a word end
283  if(0 < _wordFrequency){
284  // add the word and his frequency in a pair and
285  // add it to the list
286  words.push_front(pair<string,int>(word, _wordFrequency));
287  }
288  // if it's not a leaf
289  if(not isLeaf()){
290  // for each child
291  for(Node* child : _children){
292  // get words in the subtree
293  child->toFrequencedList(words, word);
294  }
295  }
296  }
297 
298 };
299 
304 class TreeString {
305 
306  private:
309  public:
313  _root(Node())
314  {}
315 
318  TreeString(const TreeString &other):
319  _root(other._root)
320  {}
321 
325  //delete &_root;
326  }
327 
331  int height(){
332  return _root.height();
333  }
334 
338  void put(const string &word){
339  // adress of the last added Node
340  Node* lastInserted = &_root;
341  int i;
342  int i_end = word.size()-1;
343  // from the first to the last-but-one char
344  for(i=0; i<i_end; ++i){
345  // add it to the tree
346  lastInserted = lastInserted->append(word[i], 0);
347  }
348  // add the last char and the frequence of the word
349  lastInserted->append(word[i], 1); // end of the word
350  }
351 
356  string toString(){
357  return _root.toString();
358  }
359 
364  void getWords(forward_list<string> &list){
365  _root.toList(list, string());
366  }
367 
373  void getWordsFrequencies(forward_list<pair<string,int>> &words){
374  // get the list of all words stored
375  _root.toFrequencedList(words, string());
376  }
377 };
378 
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