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
tree.hpp
Go to the documentation of this file.
1 
35 #ifndef TREE_HPP
36 #define TREE_HPP
37 
38 #include <string>
39 //#include <list> // less efficiant
40 #include <forward_list>
41 
42 using std::string;
43 using std::forward_list;
44 
49 class TreeException : std::exception {
50  private:
51  char* _cause;
52  public:
57  TreeException(char* cause):
58  _cause(cause)
59  {}
60 
64  virtual ~TreeException() throw(){
65  // do nothing
66  }
67 
71  virtual const char* what()const throw(){
72  return _cause;
73  }
74 };
75 
81 template <typename T = char>
82 class Node {
83 
84  private:
86  int _childNbr;
88  T _tag;
90  forward_list<Node<T>> _children;
91 
92  public:
96  Node(const Node<T> &other):
97  _childNbr(other._childNbr),
98  _tag(other._tag),
99  _children(other._children)
100  {}
101 
105  Node(T data):
106  _childNbr(0),
107  _tag(data)
108  {
109  // initialize the simply-linked list
110  _children = forward_list<Node<T>>();
111  }
112 
115  ~Node(){
116  // usefull ?
117  //delete &_children;
118  //delete &_tag;
119  }
120 
126  // prevent objet copying itself
127  if(this != &other){
128  this->_childNbr = other._childNbr;
129  this->_tag = other._key;
130  this->_children = other._children;
131  }
132  return (*this); // allow a = b = c
133  }
134 
140  bool operator==(const Node<T>& rhs){
141  // same adress ⇒ same item
142  return &this == &rhs;
143  }
144 
150  bool operator!=(const Node<T>& rhs){
151  return &this != &rhs;
152  // return not(lhs == rhs);
153  }
154 
158  bool isLeaf(){
159  return 0 == _childNbr;
160  }
161 
165  int height(){
166  if(isLeaf()){
167  return 0;
168  }
169  else {
170  // start heights at zero
171  // heights[0] : greatest height, heights[1] : computed height in the loop
172  int heights[2] = {0,0};
173  // for each child - using C++11 syntax
174  for(Node<T> child : _children){
175  // compute child height and store it
176  heights[1] = 1 + child.height();
177  // if computed height is greater then the old one
178  if(heights[0] < heights[1]){
179  // store it as the new greate one
180  heights[0] = heights[1];
181  } // else, nothing
182  }
183  return heights[0];
184  }
185  }
186 
190  void append(T n_data){
191  if(isLeaf()){
192  // add the value in children list as a new node
193  _children.push_front(Node<T>(n_data));
194  _childNbr++;
195  }
196  else {
197  std::cout << _tag << std::endl;
198  bool undone = true;
199  auto it = _children.begin();
200  while(it != _children.end() and undone){
201  if(it->_tag != n_data){
202  it->append(n_data);
203  undone = !undone;
204  }
205  else{
206  ++it;
207  }
208  }
209  }
210  }
211 
216  void remove(T data){
217  // remove child with the right tag
218  bool undone = true;
219  auto it=_children.begin();
220  while(it != it.end() and undone){
221  if(it->_tag == data and it->isLeaf()){
222  undone = !undone;
223  delete it;
224  }
225  ++it;
226  }
227  if(undone){
228  throw TreeException("Element was not removed");
229  }
230  }
231 
235  T getTag(){ return _tag; }
236 
242  bool contains(T element){
243  if(isLeaf()){
244  return element == _tag;
245  }
246  else {
247  // browse children
248  bool here = false;
249  auto it = _children.begin();
250  while(not here and not it.end()){
251  here = it.contains(element);
252  ++it;
253  }
254  return here;
255  }
256  }
257 
261  string toString(){
262  if(isLeaf()){
263  return string(_tag);
264  }
265  else {
266  string desc = string(_tag);
267  for(Node<T> child: _children){
268  desc += ", " + child.toString();
269  }
270  return desc;
271  }
272  }
273 
274 };
275 
280 template <typename T = string>
281 class Tree {
282 
283  private:
286  public:
289  Tree():
290  _root(nullptr)
291  {}
292 
295  Tree(const Tree<T> &other):
296  _root(other._root)
297  {}
298 
303  Tree(T element):
304  _root(Node<T>(element))
305  {}
306  //_root = Node<T>(element);
307 
310  ~Tree(){
311  //delete &_root;
312  }
313 
318  bool contains(T element){
319  return _root->contains(element);
320  }
321 
325  int height(){
326  return _root.height();
327  }
328 
332  void put(T element){
333  _root.append(element);
334  }
335 
339  void remove(T element){
340  try{
341  _root.remove(element);
342  } catch(TreeException ex){
343  // ?
344  }
345  }
346 
351  string toString(){
352  return _root.toString();
353  }
354 
355 };
356 
357 #endif // TREE_HPP
Node< T > & operator=(Node< T > &other)
Definition: tree.hpp:125
void put(T element)
Definition: tree.hpp:332
void append(T n_data)
Definition: tree.hpp:190
Defines tree nodes.
Definition: tree.hpp:82
int height()
Definition: tree.hpp:165
forward_list< Node< T > > _children
children of the Node
Definition: tree.hpp:90
~Tree()
Definition: tree.hpp:310
Tree(T element)
Definition: tree.hpp:303
bool operator!=(const Node< T > &rhs)
Definition: tree.hpp:150
string toString()
Definition: tree.hpp:261
char * _cause
Definition: tree.hpp:51
exception class for trees
Definition: tree.hpp:49
Tree()
Definition: tree.hpp:289
T _tag
letter stored into Node, the tag
Definition: tree.hpp:88
bool contains(T element)
Definition: tree.hpp:242
Tree is a recursive structure using nodes.
Definition: tree.hpp:281
string toString()
Definition: tree.hpp:351
bool operator==(const Node< T > &rhs)
Definition: tree.hpp:140
virtual const char * what() const
Definition: tree.hpp:71
Node(T data)
Definition: tree.hpp:105
bool isLeaf()
Definition: tree.hpp:158
int _childNbr
Number of children.
Definition: tree.hpp:86
virtual ~TreeException()
Definition: tree.hpp:64
Node(const Node< T > &other)
Definition: tree.hpp:96
~Node()
Definition: tree.hpp:115
int height()
Definition: tree.hpp:325
T getTag()
Definition: tree.hpp:235
bool contains(T element)
Definition: tree.hpp:318
Node< T > _root
Definition: tree.hpp:284
TreeException(char *cause)
Definition: tree.hpp:57
Tree(const Tree< T > &other)
Definition: tree.hpp:295