Natural Language Processing  0.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Tree.h
1 #ifndef TREE_H
2 #define TREE_H
3 #include <iostream>
4 #include <vector>
5 #include <cstdlib>
6 #include <cassert>
7 #include <iomanip>
8 
9 template <typename Item>
13 struct TreeNode{
17  typedef std::vector<TreeNode*> TNvector;
18 
22  Item _data;
23 
28 
34  TreeNode(const Item& data = Item(),
35  const TNvector& children = TNvector()) {
36  _data = data;
37  _child = children;
38  }
39 
44  for(std::size_t i = 0; i < _child.size(); ++i){
45  delete _child[i];
46  }
47  }
48 
53  Item& data() {return _data;}
54 
59  TNvector& children() {return _child;}
60 
65  void setData(const Item& data) {_data = data;}
66 
73 
78  void addChild(const Item& data) {_child.insert(_child.end(),new TreeNode<Item>(data));}
79 
84  Item data() const {return _data;}
85 
90  TNvector children() const {return _child;}
91 
96  bool isLeaf() const {return _child.empty();}
97 
104  friend std::ostream& operator <<
105  (std::ostream& out, const TreeNode<Item>& T) {
106  out << T.data() << ":{ ";
107  for(std::size_t i = 0; i < T._child.size(); ++i){
108  if(i == T._child.size() - 1)
109  out << *T._child[i];
110  else
111  out << *T._child[i] << " , ";
112  }
113  out << " } ";
114  return out;
115  }
116 };
117 
118 //Namespace exclusive for TreeNode Functions
119 namespace rt{
120  template <typename Item>
125  void clear(TreeNode<Item>*& root){
126  if(!root) return;
127  for(std::size_t i = 0; i < root->children().size(); ++i)
128  clear(root->children()[i]);
129  delete root;
130  root = nullptr;
131  }
132 
133  template <typename Item>
139  TreeNode<Item>* copy(const TreeNode<Item>* root){
140  typename TreeNode<Item>::TNvector tChild;
141  for(std::size_t i = 0; i < root->children().size(); ++i)
142  tChild.insert(tChild.end(),copy(root->children()[i]));
143  return new TreeNode<Item>(root->data(),tChild);
144  }
145 
146  template <typename Item>
152  std::size_t size(const TreeNode<Item>* root){
153  if(root == NULL) return 0;
154  std::size_t sum = 0;
155  typename TreeNode<Item>::TNvector::const_iterator it = root->children().begin();
156  while(it != root->children().end()){
157  sum += size(*it);
158  ++it;
159  }
160  return 1 + sum;
161  }
162 
163  template <typename Item>
169  std::size_t leaves(const TreeNode<Item>* root){
170 // using namespace std;
171 // cout << "in leaves" << endl;
172  if(!root) return 0;
173 // cout << "root is not nULL" << endl;
174  if(root->isLeaf()) return 1;
175 // cout << "root is not leaf" << endl;
176 
177 
178  std::size_t num = 0;
179  for(std::size_t i = 0; i < root->children().size(); ++i){
180  num += leaves(root->children()[i]);
181  }
182  return num;
183  }
184 
185  template <typename Item>
192  TreeNode<Item>* search(TreeNode<Item>* root, const Item& item){
193  if(!root) return nullptr;
194  if(root->data() == item) return root;
195  TreeNode<Item>* result;
196  for(std::size_t i = 0; i < root->children().size(); ++i){
197  result = search(root->children()[i],item);
198  if(result) return result;
199  }
200  return nullptr;
201  }
202 
203  template <typename Item>
211  TreeNode<Item>* parent(TreeNode<Item>* root, const TreeNode<Item>* branch){
212  if(!root || !branch) return nullptr;
213  if(root == branch) return nullptr;
214  TreeNode<Item>* result;
215  for(std::size_t i = 0; i < root->children().size(); ++i){
216  if(branch == root->children()[i])
217  return root;
218  }
219  for(std::size_t i = 0; i < root->children().size(); ++i){
220  result = parent(root->children()[i],branch);
221  if(result) return result;
222  }
223  return nullptr;
224  }
225 
226  template <typename Item>
232  std::vector<TreeNode<Item>*> allLeaves(TreeNode<Item>* root){
233  std::vector<TreeNode<Item>*> s;
234  if(!root) return s;
235  if(root->isLeaf()){
236  s.insert(s.begin(),root);
237  return s;
238  }
239  typename TreeNode<Item>::TNvector::const_iterator it = root->children().begin();
240  while(it != root->children().end()){
241  std::vector<TreeNode<Item>*> t = allLeaves(*it);
242  s.insert(s.end(),t.begin(),t.end());
243  ++it;
244  }
245  return s;
246  }
247 }
248 
249 
250 template <typename Item>
254 class Tree{
255 protected:
265 public:
269  Tree(){
270  _root = nullptr;
271  _current = _root;
272  }
273 
280  _root = root;
281  _current = _root;
282  }
283 
289  Tree(const Tree<Item>& T){
290  _root = rt::copy(T._root);
291  _current = _root;
292  }
293 
297  ~Tree(){
298  clear();
299  }
300 
307  clear();
308  _root = rt::copy(T._root);
309  _current = _root;
310  return *this;
311  }
312 
317  void addHere(const Item& item){
318  if(!_root){
319  _root = new TreeNode<Item>(item);
320  _current = _root;
321  }
322  else _current->addChild(item);
323  }
324 
330  void addNode(TreeNode<Item>* parent, const Item& item){
331  if(!_root){
332  _root = new TreeNode<Item>(item);
333  _current = _root;
334  }
335  else{
336  if(!inTree(parent)) return;
337  parent->addChild(item);
338  }
339  }
340 
346  TreeNode<Item>* search(const Item& item){
347  return rt::search(_root,item);
348  }
349 
355  bool inTree(TreeNode<Item>* node){
356  if(node == _root) return true;
357  else{
358  if(rt::parent(_root,node))
359  return true;
360  else return false;
361  }
362  }
363 
368  bool empty() const{
369  return (!_root);
370  }
371 
375  void clear(){
376  if(!empty()) rt::clear(_root);
377  }
378 
383  std::size_t leafNum(){
384  return rt::leaves(_root);
385  }
386 
394  return rt::parent(_root,branch);
395  }
396 
402  return _current;
403  }
404 
409  void set(const Item& item){
410  _current->setData(item);
411  }
412 
416  void shiftToRoot(){
417  _current = _root;
418  }
419 
423  void shiftUp(){
424  if(!hasParent()) return;
426  }
427 
433  void shiftDown(std::size_t child){
434  if(child >= _current->children().size()) return;
435  _current = _current->children()[child];
436  }
437 
443  void shiftTo(TreeNode<Item>* target){
444  if(inTree(target))
445  _current = target;
446  }
447 
452  bool hasParent(){
453  return (_current != _root);
454  }
455 
460  bool hasChild(){
461  return (!_current->isLeaf());
462  }
463 
468  std::size_t childNum(){
469  return (_current->children().size());
470  }
471 
476  Item& get(){
477  return (_current->data());
478  }
479 
484  std::size_t size() const{
485  return rt::size(_root);
486  }
487 
492  std::vector<TreeNode<Item>*> getLeaves(){
493  return rt::allLeaves(_root);
494  }
495 
502  friend std::ostream& operator <<(std::ostream& out, const Tree<Item> T){
503  out << *T._root;
504  return out;
505  }
506 };
507 
508 
509 #endif // TREE_H
bool isLeaf() const
isLeaf checks if the node is a leaf, i.e. has no children
Definition: Tree.h:96
std::size_t childNum()
childNum Finds the number of children of current
Definition: Tree.h:468
void setData(const Item &data)
setData changes the item
Definition: Tree.h:65
Tree(const Tree< Item > &T)
Tree Copy Constructor.
Definition: Tree.h:289
TreeNode(const Item &data=Item(), const TNvector &children=TNvector())
TreeNode Constructor.
Definition: Tree.h:34
TreeNode< Item > * search(const Item &item)
search searches for a specific item
Definition: Tree.h:346
~Tree()
~Tree Destructor
Definition: Tree.h:297
Item _data
_data the item inside the node
Definition: Tree.h:22
void shiftUp()
shiftUp Shift the current to its parent
Definition: Tree.h:423
TreeNode< Item > * getCurrent()
getCurrent Returns the current node pointer
Definition: Tree.h:401
bool hasParent()
hasParent Checks if current has a parent
Definition: Tree.h:452
TNvector _child
_child a vector of children whose parent is this node
Definition: Tree.h:27
TNvector & children()
children returns a reference to the children vector
Definition: Tree.h:59
void set(const Item &item)
set Set the item of the current node
Definition: Tree.h:409
TreeNode< Item > * getParent(TreeNode< Item > *branch)
getParent Gets the parent of the branch
Definition: Tree.h:393
std::size_t leafNum()
leafNum Counts the number of leaves in the tree
Definition: Tree.h:383
Item data() const
data returns a non reference item
Definition: Tree.h:84
Tree()
Tree Default Constructor.
Definition: Tree.h:269
bool empty() const
empty Checks if the tree is empty
Definition: Tree.h:368
void shiftTo(TreeNode< Item > *target)
shiftTo Shift the current to the specified target
Definition: Tree.h:443
std::vector< TreeNode< Item > * > getLeaves()
getLeaves Returns a vector of the leaves of the tree
Definition: Tree.h:492
void addHere(const Item &item)
addHere Adds an item as a child to the current node
Definition: Tree.h:317
The Tree class a class to contain a TreeNode*.
Definition: Tree.h:254
bool hasChild()
hasChild Checks if current has children
Definition: Tree.h:460
bool inTree(TreeNode< Item > *node)
inTree Checks if the node is part of the tree
Definition: Tree.h:355
void shiftToRoot()
shiftToRoot shift the current to the root
Definition: Tree.h:416
TreeNode< Item > * _root
_root the root of the tree
Definition: Tree.h:259
std::vector< TreeNode * > TNvector
TNvector typedef of std::vector<TreeNode*>
Definition: Tree.h:17
~TreeNode()
~TreeNode destructor for TreeNode. Destroys all of its children.
Definition: Tree.h:43
void setChildren(const TNvector &children)
setChildren changes the children
Definition: Tree.h:72
void addNode(TreeNode< Item > *parent, const Item &item)
addNode Adds an item as a child to the parent
Definition: Tree.h:330
The TreeNode struct the node that will occupy trees.
Definition: Tree.h:13
TNvector children() const
children returns a non reference vector
Definition: Tree.h:90
TreeNode< Item > * _current
_current the treenode in which many operations are performed. Can be changed via shift functions ...
Definition: Tree.h:264
std::size_t size() const
size Returns the size of the tree
Definition: Tree.h:484
void clear()
clear Clears the tree
Definition: Tree.h:375
Tree(TreeNode< Item > *root)
Tree Constructor.
Definition: Tree.h:279
void shiftDown(std::size_t child)
shiftDown Shift the current to one of its children
Definition: Tree.h:433
Tree< Item > & operator=(const Tree< Item > &T)
operator = assignment operator
Definition: Tree.h:306
void addChild(const Item &data)
addChild adds a child to the vector
Definition: Tree.h:78
Item & data()
data returns a reference to the item
Definition: Tree.h:53