latest version v1.9 - last update 10 Apr 2010 |
Tree container class. More...
#include <ltiTree.h>
Classes | |
class | const_iterator |
Const iterator type (allows read-only operations). More... | |
class | iterator |
Iterator type (allows read and write operations). More... | |
class | node |
The elements of the tree are instances of the "node" class. More... | |
class | nodeManager |
The nodeManager takes care of the memory administration. More... | |
Public Types | |
typedef T | value_type |
typedef int | size_type |
Public Member Functions | |
tree (const int &n=0) | |
tree (const tree< T > &other) | |
virtual | ~tree () |
const char * | getTypeName () const |
size_type | size () const |
size_type | height () const |
const_iterator | begin () const |
iterator | begin () |
const_iterator | end () const |
iterator | end () |
iterator | getIteratorTo (node &aNode) |
const_iterator | getIteratorTo (const node &aNode) const |
void | clear () |
void | fill (const T &iniValue) |
node & | root () |
const node & | root () const |
void | pushRoot (const T &newData) |
tree< T > & | copy (const tree< T > &other) |
virtual mathObject * | clone () const |
bool | empty () const |
bool | equals (const tree< T > &other) const |
bool | operator== (const tree< T > &other) const |
tree< T > & | operator= (const tree< T > &other) |
virtual bool | write (ioHandler &handler, const bool complete=true) const |
virtual bool | read (ioHandler &handler, const bool complete=true) |
Protected Attributes | |
node * | theRoot |
int | rootDegree |
nodeManager | theNodeManager |
Tree container class.
The lti::tree class allows the representation of rooted ordered trees.
The tree class is a container class implemented as template. The template type T is the type of a data element contained in each node of the tree, independently if it is a leaf or not. The only requirement for the type T is that is must not depend on dynamic allocation of internal data.
All types defined in ltiTypes.h use static members and can be contained by the lti::tree class.
The tree is always created empty. You can however specify in the constructor a default degree, i.e. the maximal number of childs to be expected per node.
The first element will be inserted with pushRoot().
typedef T lti::tree< T >::value_type |
Type of the data in the nodes of the tree.
Default constructor.
If you do not provide any arguments, an empty tree of zero-degree will be created.
You can also provide the degree of the tree, which is the maximal number of children a node can have, but even if you provide the degree, the tree will be empty. Use pushRoot to insert the first node in the tree.
n | the default number of children (degree) of the root node. |
Returns an iterator to the first element.
The use of the interators is similar to the iterators of the Standard Template Library (STL). If you need to iterate on all elements of the tree, you can use following code:
int tmp,accu; // a temporal variable lti::tree<int> myTree(); // empty tree // fill your tree here lti::tree<int>::iterator it; // an iterator for (it=myTree.begin();it!=myTree.end();++it) { tmp = (*it).getData(); // tmp has value of element pointed // by the iterator. accu += tmp; (*it).setData(accu); // change the value in the tree. }
Please note that if you define it
as a const_iterator, you can NOT make something like *it=accu
.
const_iterator lti::tree< T >::begin | ( | ) | const [inline] |
Returns const_iterator to a "first" node of the tree.
This node will be allways the root Note that you can not change the values of the tree elements when you use a const_iterator. See also begin()
void lti::tree< T >::clear | ( | ) |
Clears the tree (this tree will be empty!).
see fill(const T&) to initialize all tree nodes with some value
Referenced by lti::tree< T >::node::setDegree().
virtual mathObject* lti::tree< T >::clone | ( | ) | const [virtual] |
Assigment operator.
copy the contents of other
in this object.
other | the source tree to be copied. |
Reimplemented from lti::ioObject.
Referenced by lti::tree< objectStruct >::operator=().
const_iterator lti::tree< T >::end | ( | ) | const [inline] |
void lti::tree< T >::fill | ( | const T & | iniValue | ) |
Fills all tree elements with iniValue
.
iniValue | the elements will be initialized with this value. |
const_iterator lti::tree< T >::getIteratorTo | ( | const node & | aNode | ) | const [inline] |
Return a const_iterator which points to the given node.
const char* lti::tree< T >::getTypeName | ( | void | ) | const [inline, virtual] |
Returns the name of this class: "tree".
Reimplemented from lti::mathObject.
Assigment operator (alias for copy(other)).
other | the tree to be copied |
Reimplemented from lti::ioObject.
void lti::tree< T >::pushRoot | ( | const T & | newData | ) |
Insert a node at the root position.
pushRoot will insert the given data as the root of the tree, and the old root will be inserted as its first child. The root will have the mininum between 1 and the degree specified in the creation of the tree.
If the tree was empty, it will have a new node with the given data.
virtual bool lti::tree< T >::read | ( | ioHandler & | handler, | |
const bool | complete = true | |||
) | [virtual] |
Read the parameters from the given ioHandler.
The default implementation is to read just the parameters object.
handler | the ioHandler to be used | |
complete | if true (the default) the enclosing begin/end will be also written, otherwise only the data block will be written. |
Reimplemented from lti::mathObject.
Returns the total number of nodes of the tree.
Referenced by lti::tree< T >::node::equals(), lti::tree< T >::const_iterator::operator++(), and lti::tree< T >::iterator::operator++().
virtual bool lti::tree< T >::write | ( | ioHandler & | handler, | |
const bool | complete = true | |||
) | const [virtual] |
Write the functor in the given ioHandler.
The default implementation is to write just the parameters object.
handler | the ioHandler to be used | |
complete | if true (the default) the enclosing begin/end will be also written, otherwise only the data block will be written. |
Reimplemented from lti::mathObject.
int lti::tree< T >::rootDegree [protected] |
Degree for the root specified by the user in constructor.
nodeManager lti::tree< T >::theNodeManager [protected] |