9 template <
typename Item>
44 for(std::size_t i = 0; i <
_child.size(); ++i){
104 friend std::ostream&
operator <<
106 out << T.data() <<
":{ ";
107 for(std::size_t i = 0; i < T._child.size(); ++i){
108 if(i == T._child.size() - 1)
111 out << *T._child[i] <<
" , ";
120 template <
typename Item>
127 for(std::size_t i = 0; i < root->
children().size(); ++i)
133 template <
typename Item>
141 for(std::size_t i = 0; i < root->
children().size(); ++i)
142 tChild.insert(tChild.end(),copy(root->
children()[i]));
146 template <
typename Item>
153 if(root == NULL)
return 0;
156 while(it != root->
children().end()){
163 template <
typename Item>
174 if(root->
isLeaf())
return 1;
179 for(std::size_t i = 0; i < root->
children().size(); ++i){
185 template <
typename Item>
193 if(!root)
return nullptr;
194 if(root->
data() == item)
return root;
196 for(std::size_t i = 0; i < root->
children().size(); ++i){
197 result = search(root->
children()[i],item);
198 if(result)
return result;
203 template <
typename Item>
212 if(!root || !branch)
return nullptr;
213 if(root == branch)
return nullptr;
215 for(std::size_t i = 0; i < root->
children().size(); ++i){
219 for(std::size_t i = 0; i < root->
children().size(); ++i){
220 result = parent(root->
children()[i],branch);
221 if(result)
return result;
226 template <
typename Item>
233 std::vector<TreeNode<Item>*> s;
236 s.insert(s.begin(),root);
240 while(it != root->
children().end()){
241 std::vector<TreeNode<Item>*> t = allLeaves(*it);
242 s.insert(s.end(),t.begin(),t.end());
250 template <
typename Item>
336 if(!
inTree(parent))
return;
347 return rt::search(
_root,item);
356 if(node ==
_root)
return true;
358 if(rt::parent(
_root,node))
384 return rt::leaves(
_root);
394 return rt::parent(
_root,branch);
409 void set(
const Item& item){
434 if(child >=
_current->children().size())
return;
469 return (
_current->children().size());
485 return rt::size(
_root);
493 return rt::allLeaves(
_root);
502 friend std::ostream& operator <<(std::ostream& out, const Tree<Item> T){
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