#include <qgarlib/GenTree.H>
Such a tree is represented by:
See class qgar::GenTreeNode for the way the inner nodes are linked together.
For efficiency reasons, the validity of pointers given as arguments to function members is not checked. In particular:
The deletion of a tree causes the deletion of all its nodes. Consequently, a program abortion may occur when:
Definition at line 356 of file GenTree.H.
Public Types | |
Types | |
| typedef T | value_type |
| Type of the data stored in nodes. | |
| typedef value_type & | reference |
| Reference to qgar::GenTree::value_type. | |
| typedef const value_type & | const_reference |
| Constant reference to qgar::GenTree::value_type. | |
| typedef value_type * | pointer |
| Pointer to qgar::GenTree::value_type. | |
| typedef const value_type * | const_pointer |
| Constant pointer to qgar::GenTree::value_type. | |
Public Member Functions | |
Constructors | |
| GenTree () | |
| Default constructor. | |
| GenTree (const GenTree< value_type > &aTree) | |
| Copy constructor. | |
| GenTree (const_reference aData) | |
| Create a tree with a single node containing the given data. | |
| GenTree (GenTreeNode< value_type > *aPNode) | |
| Create a tree from a given node. | |
Destructor | |
| virtual | ~GenTree () |
| Virtual destructor. | |
Predicates | |
| bool | empty () const |
| Is tree empty? | |
Access to links | |
WARNING The behavior of functions taking a pointer to a node as extra argument is undefined if:
| |
| GenTreeNode< value_type > * | pRoot () const |
| Get pointer to the root of the tree. | |
| GenTreeNode< value_type > * | pCurrent () const |
| Get pointer to the current node. | |
| GenTreeNode< value_type > * | pParent () const |
| Get pointer to the parent of the current node. | |
| GenTreeNode< value_type > * | pParent (GenTreeNode< value_type > *aPNode) const |
| Get pointer to the parent of the given node. | |
| GenTreeNode< value_type > * | pLSibling () const |
| Get pointer to the left sibling of the current node. | |
| GenTreeNode< value_type > * | pLSibling (GenTreeNode< value_type > *aPNode) const |
| Get pointer to the left sibling of the given node. | |
| GenTreeNode< value_type > * | pRSibling () const |
| Get pointer to the right sibling of the current node. | |
| GenTreeNode< value_type > * | pRSibling (GenTreeNode< value_type > *aPNode) const |
| Get pointer to the right sibling of the given node. | |
| GenTreeNode< value_type > * | pFirstChild () const |
| Get pointer to the first child of the current node. | |
| GenTreeNode< value_type > * | pFirstChild (GenTreeNode< value_type > *aPNode) const |
| Get pointer to the first child of the given node. | |
Access to data | |
WARNING The behavior of functions taking a pointer to a node as extra extra argument is undefined if:
| |
| const_reference | accessData () const |
| Get data stored in current node. | |
| const_reference | accessData (GenTreeNode< value_type > *aPNode) const |
| Get data stored in given node. | |
| value_type | data () const |
| Get a copy of the data stored in current node. | |
| value_type | data (GenTreeNode< value_type > *aPNode) const |
| Get a copy of the data stored in current node. | |
Moving in the tree | |
WARNING
| |
| void | gotoRoot () |
| The root becomes the current node. | |
| void | gotoParent () |
| The parent of the current node becomes the current node. | |
| void | gotoParent (GenTreeNode< value_type > *aPNode) |
| The parent of the given node becomes the current node. | |
| void | gotoLSibling () |
| The left sibling of the current node becomes the current node. | |
| void | gotoLSibling (GenTreeNode< value_type > *aPNode) |
| The left sibling of the given node becomes the current node. | |
| void | gotoRSibling () |
| The right sibling of the current node becomes the current node. | |
| void | gotoRSibling (GenTreeNode< value_type > *aPNode) |
| The right sibling of the given node becomes the current node. | |
| void | gotoFirstChild () |
| The first child of the current node becomes the current node. | |
| void | gotoFirstChild (GenTreeNode< value_type > *aPNode) |
| The first child of the given node becomes the current node. | |
Store data in nodes | |
WARNING The behavior of functions taking a pointer to a node as extra extra argument is undefined if:
| |
| void | setData (const_reference aData) |
| Store data in current node. | |
| void | setData (const_reference aData, GenTreeNode< value_type > *aPNode) |
| Store data in given node. | |
Insert new nodes | |
WARNING When a pointer to a node is given as argument, the corresponding node is supposed to belong to the tree! | |
| void | insertParent (const_reference aData) |
| Insert a node containing given data as parent of current root. | |
| void | insertLSibling (const_reference aData) throw (QgarErrorUser) |
| Insert a node containing given data as left sibling of current node, which must not be the root. | |
| void | insertLSibling (const_reference aData, GenTreeNode< value_type > *aPNode) throw (QgarErrorUser) |
| Insert a node containing given data as left sibling of given node, which must not be the root. | |
| void | insertRSibling (const_reference aData) throw (QgarErrorUser) |
| Insert a node containing given data as right sibling of current node, which must not be the root. | |
| void | insertRSibling (const_reference aData, GenTreeNode< value_type > *aPNode) throw (QgarErrorUser) |
| Insert a node containing given data as right sibling of given node, which must not be the root. | |
| void | insertFirstChild (const_reference aData) |
| Insert a node containing given data as first child of current node. | |
| void | insertFirstChild (const_reference aData, GenTreeNode< value_type > *aPNode) |
| Insert a node containing given data as first child of given node. | |
Merge | |
WARNING
| |
| void | mergeLSibling (const GenTree< value_type > &aTree) throw (QgarErrorUser) |
| Merge given tree as left sibling of current node, which must not be the root. | |
| void | mergeLSibling (const GenTree< value_type > &aTree, GenTreeNode< value_type > *aPNode) throw (QgarErrorUser) |
| Merge given tree as left sibling of given node, which must not be the root. | |
| void | mergeRSibling (const GenTree< value_type > &aTree) throw (QgarErrorUser) |
| Merge given tree as right sibling of current node, which must not be the root. | |
| void | mergeRSibling (const GenTree< value_type > &aTree, GenTreeNode< value_type > *aPNode) throw (QgarErrorUser) |
| Merge given tree as right sibling of current node, which must not be the root. | |
| void | mergeFirstChild (const GenTree< value_type > &aTree) |
| Merge given tree as first child of current node. | |
| void | mergeFirstChild (const GenTree< value_type > &aTree, GenTreeNode< value_type > *aPNode) |
| Merge given tree as first child of current node. | |
Remove nodes | |
| GenTreeNode< value_type > * | remove () |
| Remove current node and return it. | |
| GenTreeNode< value_type > * | remove (GenTreeNode< value_type > *aPNode) |
| Remove given node and return it. | |
Operators | |
| GenTree< value_type > & | operator= (const GenTree< value_type > &aTree) |
| Assignment. | |
Protected Attributes | |
Representation of a tree | |
| GenTreeNode< value_type > * | _pRoot |
| Pointer to the root of the tree. | |
| GenTreeNode< value_type > * | _pCurrent |
| Pointer to the current node. | |
Private Member Functions | |
Auxiliaries | |
| GenTreeNode< value_type > * | PRIVATEdeepCopy (GenTreeNode< value_type > *aPNode, GenTreeNode< value_type > *aPFCopy=0, GenTreeNode< value_type > *aPLSCopy=0) const |
| Perform a deep copy of a part of the tree and return a pointer to the root of the copy. | |
| void | PRIVATEdelete (GenTreeNode< value_type > *aPNode) |
| Delete the (sub)tree of given root. | |
|
|||||
|
Constant pointer to qgar::GenTree::value_type.
|
|
|||||
|
Constant reference to qgar::GenTree::value_type.
|
|
|||||
|
Pointer to qgar::GenTree::value_type.
|
|
|||||
|
Reference to qgar::GenTree::value_type.
|
|
|||||
|
Type of the data stored in nodes.
|
|
|||||||||
|
Default constructor. Create an empty tree. Definition at line 305 of file GenTree.TCC. |
|
||||||||||
|
Copy constructor. The current node of the resulting tree is its root.
Definition at line 331 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTree< T >::_pRoot. |
|
||||||||||
|
Create a tree with a single node containing the given data. This node becomes the current node.
|
|
||||||||||
|
Create a tree from a given node. If the pointer is 0, create an empty tree. Otherwise, the given node is considered as a root of a tree, which is copied to become the current tree. The current node is the root.
|
|
|||||||||
|
Virtual destructor.
Definition at line 380 of file GenTree.TCC. References qgar::GenTree< T >::_pRoot, and qgar::GenTree< T >::PRIVATEdelete(). |
|
||||||||||
|
Get data stored in given node.
|
|
|||||||||
|
Get data stored in current node.
Definition at line 514 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::accessData(). |
|
||||||||||
|
Get a copy of the data stored in current node.
|
|
|||||||||
|
Get a copy of the data stored in current node.
Definition at line 534 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::data(). |
|
|||||||||
|
Is tree empty?
Definition at line 394 of file GenTree.TCC. References qgar::GenTree< T >::_pRoot. Referenced by qgar::GenTree< T >::mergeFirstChild(), and qgar::GenTree< T >::remove(). |
|
||||||||||
|
The first child of the given node becomes the current node.
|
|
|||||||||
|
The first child of the current node becomes the current node.
Definition at line 628 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pFirstChild(). |
|
||||||||||
|
The left sibling of the given node becomes the current node.
|
|
|||||||||
|
The left sibling of the current node becomes the current node.
Definition at line 588 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pLSibling(). |
|
||||||||||
|
The parent of the given node becomes the current node.
|
|
|||||||||
|
The parent of the current node becomes the current node.
Definition at line 569 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pParent(). |
|
|||||||||
|
The root becomes the current node.
Definition at line 559 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTree< T >::_pRoot. Referenced by qgar::ConnectedComponentsImpl::run(). |
|
||||||||||
|
The right sibling of the given node becomes the current node.
|
|
|||||||||
|
The right sibling of the current node becomes the current node.
Definition at line 608 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pRSibling(). |
|
||||||||||||||||
|
Insert a node containing given data as first child of given node.
GIV GIV
|\ /|\
| \ / | \
| \ ===> / | \
| \ / | \
| \ / | \
fc<-->rs<- NEW<-->fc<-->rs<-
/|\ /|\ /|\ /|\
|
|
||||||||||
|
Insert a node containing given data as first child of current node.
CUR CUR
|\ /|\
| \ / | \
| \ ===> / | \
| \ / | \
| \ / | \
fc<-->rs<- NEW<-->fc<-->rs<-
/|\ /|\ /|\ /|\
If the tree is empty, the new node becomes the only node of the tree, i.e. the root and the current node. Otherwise:
|
|
||||||||||||||||
|
Insert a node containing given data as left sibling of given node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->GIV<-->rs<- ->ls<-->NEW<-->GIV<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\
|
|
||||||||||
|
Insert a node containing given data as left sibling of current node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->CUR<-->rs<- ->ls<-->NEW<-->CUR<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\
If the tree is empty, the new node becomes the only node of the tree, i.e. the root and the current node. Otherwise:
|
|
||||||||||
|
Insert a node containing given data as parent of current root.
NEW
|
|
ROOT ===> ROOT
/|\ /|\
If the tree is empty, the new node becomes the only node of the tree, i.e. the root and the current node. Otherwise:
Referenced by qgar::ConnectedComponentsImpl::run(). |
|
||||||||||||||||
|
Insert a node containing given data as right sibling of given node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->GIV<-->rs<- ->ls<-->GIV<-->NEW<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\
|
|
||||||||||
|
Insert a node containing given data as right sibling of current node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->CUR<-->rs<- ->ls<-->CUR<-->NEW<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\
If the tree is empty, the new node becomes the only node of the tree, i.e. the root and the current node. Otherwise:
|
|
||||||||||||||||
|
Merge given tree as first child of current node.
GIV GIV
|\ /|\
| \ / | \
| \ ===> / | \
| \ / | \
| \ / | \
fc<-->rs<- NEW<-->fc<-->rs<-
/|\ /|\ /|\
/ | \
c1 c2 c3
NEW
/|\
/ | \
c1 c2 c3
If the given tree is empty, the function has no effect. Otherwise:
|
|
||||||||||
|
Merge given tree as first child of current node.
CUR CUR
|\ /|\
| \ / | \
| \ ===> / | \
| \ / | \
| \ / | \
fc<-->rs<- GIV<-->fc<-->rs<-
/|\ /|\ /|\
/ | \
c1 c2 c3
GIV
/|\
/ | \
c1 c2 c3
If the given tree is empty, the function has no effect. If the current tree is empty, the given tree becomes the current tree. The new root becomes the current node. Otherwise:
Definition at line 1182 of file GenTree.TCC. References qgar::GenTree< T >::_pRoot, qgar::GenTree< T >::empty(), qgar::GenTree< T >::PRIVATEdeepCopy(), qgar::GenTree< T >::pRoot(), qgar::GenTreeNode< T >::setPLSibling(), qgar::GenTreeNode< T >::setPParent(), and qgar::GenTreeNode< T >::setPRSibling(). |
|
||||||||||||||||
|
Merge given tree as left sibling of given node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->GIV<-->rs<- ->ls<-->NEW<-->GIV<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\ /|\
/ | \
c1 c2 c3
NEW
/|\
/ | \
c1 c2 c3
If the given tree is empty, the function has no effect. Otherwise:
.
|
|
||||||||||
|
Merge given tree as left sibling of current node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->CUR<-->rs<- ->ls<-->GIV<-->CUR<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\ /|\
/ | \
c1 c2 c3
GIV
/|\
/ | \
c1 c2 c3
If the given tree is empty, the function has no effect. If the current tree is empty, the given tree becomes the current tree. The new root becomes the current node. Otherwise:
Definition at line 971 of file GenTree.TCC. References qgar::GenTreeNode< T >::setPLSibling(), qgar::GenTreeNode< T >::setPParent(), and qgar::GenTreeNode< T >::setPRSibling(). |
|
||||||||||||||||
|
Merge given tree as right sibling of current node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->GIV<-->rs<- ->ls<-->GIV<-->NEW<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\ /|\
/ | \
c1 c2 c3
NEW
/|\
/ | \
c1 c2 c3
If the given tree is empty, the function has no effect. Otherwise:
|
|
||||||||||
|
Merge given tree as right sibling of current node, which must not be the root.
p p
/|\ /|\
/ | \ / | \
/ | \ ===> / | \
/ | \ / | \
/ | \ / | \
->ls<-->CUR<-->rs<- ->ls<-->CUR<-->GIV<-->rs<-
/|\ /|\ /|\ /|\ /|\ /|\ /|\
/ | \
c1 c2 c3
GIV
/|\
/ | \
c1 c2 c3
If the given tree is empty, the function has no effect. If the current tree is empty, the given tree becomes the current tree. The new root becomes the current node. Otherwise:
Definition at line 1080 of file GenTree.TCC. References qgar::GenTreeNode< T >::setPLSibling(), qgar::GenTreeNode< T >::setPParent(), and qgar::GenTreeNode< T >::setPRSibling(). |
|
||||||||||
|
Assignment. The current node of the resulting tree is its root.
Definition at line 1367 of file GenTree.TCC. References qgar::GenTree< T >::_pRoot, and qgar::GenTree< T >::PRIVATEdeepCopy(). |
|
|||||||||
|
Get pointer to the current node.
Definition at line 419 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent. |
|
||||||||||
|
Get pointer to the first child of the given node.
|
|
|||||||||
|
Get pointer to the first child of the current node.
Definition at line 489 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pFirstChild(). |
|
||||||||||
|
Get pointer to the left sibling of the given node.
|
|
|||||||||
|
Get pointer to the left sibling of the current node.
Definition at line 449 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pLSibling(). |
|
||||||||||
|
Get pointer to the parent of the given node.
|
|
|||||||||
|
Get pointer to the parent of the current node.
Definition at line 429 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pParent(). |
|
||||||||||||||||||||
|
Perform a deep copy of a part of the tree and return a pointer to the root of the copy.
Referenced by qgar::GenTree< T >::mergeFirstChild(), and qgar::GenTree< T >::operator=(). |
|
||||||||||
|
Delete the (sub)tree of given root.
Referenced by qgar::GenTree< T >::~GenTree(). |
|
|||||||||
|
Get pointer to the root of the tree.
Definition at line 409 of file GenTree.TCC. References qgar::GenTree< T >::_pRoot. Referenced by qgar::ConnectedComponents::ConnectedComponents(), qgar::GenTree< T >::mergeFirstChild(), qgar::ConnectedComponents::operator=(), qgar::ConnectedComponents::pRoot(), and qgar::ConnectedComponentsImpl::run(). |
|
||||||||||
|
Get pointer to the right sibling of the given node.
|
|
|||||||||
|
Get pointer to the right sibling of the current node.
Definition at line 469 of file GenTree.TCC. References qgar::GenTree< T >::_pCurrent, and qgar::GenTreeNode< T >::pRSibling(). |
|
||||||||||
|
Remove given node and return it.
p p
/|\ /|
/ | \ / |
/ | \ ===> / |
/ | \ / |
/ | \ / |
->ls<-->GIV<-->rs<- ->ls<-->rs<-
/|\ /|\ /|\ /|\ /|\
|
|
|||||||||
|
Remove current node and return it.
p p
/|\ /|
/ | \ / |
/ | \ ===> / |
/ | \ / |
/ | \ / |
->ls<-->CUR<-->rs<- ->ls<-->rs<-
/|\ /|\ /|\ /|\ /|\
No effect if the tree is empty.
Definition at line 1274 of file GenTree.TCC. References qgar::GenTree< T >::_pRoot, qgar::GenTree< T >::empty(), qgar::GenTreeNode< T >::pParent(), qgar::GenTreeNode< T >::setPFirstChild(), qgar::GenTreeNode< T >::setPLSibling(), and qgar::GenTreeNode< T >::setPRSibling(). |
|
||||||||||||||||
|
Store data in given node.
|
|
||||||||||
|
Store data in current node.
|
|
|||||
|
|||||
|
Pointer to the root of the tree.
Definition at line 1261 of file GenTree.H. Referenced by qgar::GenTree< T >::empty(), qgar::GenTree< T >::GenTree(), qgar::GenTree< T >::gotoRoot(), qgar::GenTree< T >::mergeFirstChild(), qgar::GenTree< T >::operator=(), qgar::GenTree< T >::pRoot(), qgar::GenTree< T >::remove(), and qgar::GenTree< T >::~GenTree(). |