118 lines
2.6 KiB
C++
118 lines
2.6 KiB
C++
/*
|
|
Copyright (C) 2019 Dominik Meyer
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
#include <Tree/tree.hpp>
|
|
|
|
|
|
std::shared_ptr<Tree::BaseNode> Tree::BaseNode::findNext(const std::string &t)
|
|
{
|
|
std::list<std::shared_ptr<Tree::BaseNode>>::iterator childIT;
|
|
std::shared_ptr<Tree::BaseNode> temp;
|
|
|
|
if (type() == t )
|
|
{
|
|
return shared_from_this();
|
|
}
|
|
|
|
for(childIT=children.begin(); childIT != children.end(); ++childIT)
|
|
{
|
|
temp=(*childIT)->findNext(t);
|
|
if (temp != nullptr)
|
|
{
|
|
return temp;
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* return the number of children
|
|
*/
|
|
const unsigned int Tree::BaseNode::getNrChildren() const
|
|
{
|
|
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator childIT;
|
|
unsigned int nr=children.size();
|
|
|
|
for(childIT=children.begin(); childIT != children.end(); ++childIT)
|
|
{
|
|
nr += (*childIT)->getNrChildren();
|
|
}
|
|
|
|
return nr;
|
|
}
|
|
|
|
/*
|
|
* print the tree and indent according to depth
|
|
*/
|
|
void Tree::BaseNode::printTree(const uint32_t depth)
|
|
{
|
|
for (uint32_t i=0; i< depth; i++)
|
|
{
|
|
std::cout << " ";
|
|
}
|
|
|
|
std::cout << toString() << std::endl;
|
|
|
|
std::list<std::shared_ptr<Tree::BaseNode>>::iterator childIT;
|
|
|
|
for(childIT=getChildrenBegin(); childIT != getChildrenEnd(); ++childIT)
|
|
{
|
|
(*childIT)->printTree(depth+1);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void Tree::BaseNode::removeChild(const std::shared_ptr<Tree::BaseNode> &c)
|
|
{
|
|
std::list<std::shared_ptr<Tree::BaseNode>>::iterator it;
|
|
it=std::find(children.begin(), children.end(),c);
|
|
children.erase(it);
|
|
}
|
|
|
|
void Tree::BaseNode::deleteChild(const std::shared_ptr<Tree::BaseNode> &c)
|
|
{
|
|
std::list<std::shared_ptr<Tree::BaseNode>>::iterator it;
|
|
it=std::find(children.begin(), children.end(),c);
|
|
children.erase(it);
|
|
|
|
c->setParent(nullptr);
|
|
}
|
|
|
|
|
|
/*
|
|
* delete all children
|
|
*/
|
|
void Tree::BaseNode::deleteChildren()
|
|
{
|
|
std::list<std::shared_ptr<Tree::BaseNode>>::iterator it;
|
|
|
|
for(it=children.begin(); it!=children.end(); ++it)
|
|
{
|
|
if ((*it) != nullptr )
|
|
{
|
|
deleteChild((*it));
|
|
}
|
|
}
|
|
|
|
}
|