6 * $Date: 2012-07-07 00:03:48 +0200 (Sa, 07. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration and implementation of the class PQNode.
12 * This file contains the header for the class template PQNode. The
13 * class template PQNode is used as an abstract base class for all
14 * nodes in a PQ-tree. The derived classes are a class template for
15 * Q- and P-nodes internalNodes (PQInternalNode) and a class template
16 * PQLeaf for the leaves of the tree.
18 * \author Sebastian Leipert
21 * This file is part of the Open Graph Drawing Framework (OGDF).
25 * See README.txt in the root directory of the OGDF installation for details.
28 * This program is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU General Public License
30 * Version 2 or 3 as published by the Free Software Foundation;
31 * see the file LICENSE.txt included in the packaging of this file
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public
42 * License along with this program; if not, write to the Free
43 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
44 * Boston, MA 02110-1301, USA.
46 * \see http://www.gnu.org/copyleft/gpl.html
47 ***************************************************************/
55 #ifndef OGDF_PQ_NODE_H
56 #define OGDF_PQ_NODE_H
59 #include <ogdf/basic/List.h>
60 #include <ogdf/internal/planarity/PQNodeRoot.h>
66 template<class T
,class X
,class Y
> class PQTree
;
67 template<class T
,class X
,class Y
> class PQLeafKey
;
68 template<class T
,class X
,class Y
> class PQNodeKey
;
69 template<class T
,class X
,class Y
> class PQInternalKey
;
72 template<class T
,class X
,class Y
> class PQNode
: public PQNodeRoot
75 * All members and member function of PQNode are needed
76 * by the class template PQTree. Therefore the class PQTree
77 * was made friendof PQNode, since this prevents the use of a large
78 * amount of extra public functions.
80 friend class PQTree
<T
,X
,Y
>;
85 * The (first) constructor combines the node with its information and
86 * will automatically set the \a m_nodePointer (see basicKey) of
87 * the element of type PQNodeKey.
89 PQNode(int count
, PQNodeKey
<T
,X
,Y
>* infoPtr
);
93 * The (second) constructor is called,
94 * if no information is available or neccessary.
99 * The destructor does not delete any accompanying information class as PQLeafKey,
100 * PQNodeKey and PQInternalKey. This has been avoided, since applications may
101 * need the existence of these information classes after the corresponding node
102 * has been deleted. If the deletion of an accompanying information class should
103 * be performed with the deletion of a node, either derive a new class
104 * with an appropriate destructor, or make use of the function
105 * CleanNode() of the class template PQTree.
110 delete partialChildren
;
114 * The function changeEndmost() replaces the old endmost child \a oldEnd
115 * of the node by a new child \a newEnd.
116 * If the node is a Q-node, then it must have two valid
117 * pointers to its endmost children. If one of the endmost children is
118 * \a oldEnd, it is replaced by \a newEnd.
119 * The function changeEndmost() returns 1 if it succeeded in
120 * replacing \a oldEnd by \a newEnd. Otherwise the function returns
121 * 0, leaving with an error message.
123 bool changeEndmost(PQNode
<T
,X
,Y
>* oldEnd
, PQNode
<T
,X
,Y
>* newEnd
);
126 * The function changeSiblings() replaces the old sibling \a oldSib of the
127 * node by a new sibling \a newSib.
128 * If the node has \a oldSib as sibling, then it changes the
129 * sibling pointer that references to \a oldSib and places \a newSib
131 * The function changeSiblings() returns 1 if it succeeded in
132 * replacing \a oldSib by \a newSib. Otherwise the function returns
133 * 0, leaving with an error message.
135 bool changeSiblings(PQNode
<T
,X
,Y
>* oldSib
, PQNode
<T
,X
,Y
>* newSib
);
138 * The function endmostChild() checks if a node is endmost child of
139 * a Q-node. This is 1 if one of the sibling pointers \a m_sibLeft
140 * or \a m_sibRight is 0. If the node is endmost child of a Q-node,
141 * then it has a valid parent pointer.
143 bool endmostChild() {
144 return (m_sibLeft
== 0 || m_sibRight
== 0);
148 * Returns one of the endmost children of node, if node is a Q-node.
149 * The function getEndmost() accepts as input a pointer to a
150 * PQNode stored in \a other. The returned endmost child is unequal
151 * to the one specified in \a other. In case that an arbitrary endmost child
152 * should be looked up, set \a other = 0. This makes the function
153 * getEndmost() return an arbitrary endmost child (it returns the
154 * left endmost child).
156 PQNode
<T
,X
,Y
>* getEndmost(PQNode
<T
,X
,Y
>* other
) const {
157 if (m_leftEndmost
!= other
)
158 return m_leftEndmost
;
159 else if (m_rightEndmost
!= other
)
160 return m_rightEndmost
;
166 * Returns one of the endmost children of node, if node is a Q-node.
167 * The function accepts an integer denoting a direction causing the
168 * function to return either the left or the endmost child.
170 PQNode
<T
,X
,Y
>* getEndmost(int side
) const {
171 if (side
== LEFT
|| side
== 0)
172 return m_leftEndmost
;
173 else if(side
== RIGHT
)
174 return m_rightEndmost
;
179 //! Returns the identification number of a node.
180 PQNodeKey
<T
,X
,Y
>* getNodeInfo() const { return m_pointerToInfo
; }
183 * The function getSib() returns one of the siblings of the node.
184 * It accepts an integer denoting a dircetion causing the
185 * function to return either the left or the right sibling.
187 PQNode
<T
,X
,Y
>* getSib(int side
) const {
190 else if (side
== RIGHT
)
197 * The function getNextSib() returns one of the siblings of the node.
198 * The function getNextSib() accepts as input a pointer to a
199 * PQNode stored in \a other. The returned sibling is unequal to the
200 * one specified in \a other. In case
201 * that no sibling has been looked up before, set \a other = 0.
202 * This makes the function getNextSib() return an arbitrary sibling
203 * (it returns the left sibling).
205 PQNode
<T
,X
,Y
>* getNextSib(PQNode
<T
,X
,Y
>* other
) const {
206 if (m_sibLeft
!= other
)
208 else if (m_sibRight
!= other
)
215 //! Returns the identification number of a node.
216 int identificationNumber() const { return m_identificationNumber
; }
218 //! Returns the number of children of a node.
219 int childCount() const { return m_childCount
; }
221 //! Sets the number of children of a node.
222 void childCount(int count
) { m_childCount
= count
; }
225 * The function parent() returns a pointer to the parent of a node.
227 * \warning After reducing the PQ-tree, some nodes may not have
228 * valid parent pointers anymore. This is no fault, the datastructur
229 * was designed this way. See also Booth and Lueker.
231 PQNode
<T
,X
,Y
>* parent() const { return m_parent
; }
234 * Sets the parent pointer of a node. This function
235 * is needed in more ellaborated algorithms implemented as derivation of
236 * the class template PQTree. Here, the parent pointer probably is
237 * always needed and therefore has to be set within special functions,
238 * used in a pre-run before applying the bubble Phase of the PQTree.
240 PQNode
<T
,X
,Y
>* parent(PQNode
<T
,X
,Y
>* newParent
)
242 return m_parent
= newParent
;
245 //! Returns the type of the parent of a node.
246 int parentType() const { return m_parentType
; }
249 * Sets the type of the parent of a node.
250 * This does not change the type of the parent!
252 void parentType(int newParentType
) { m_parentType
= newParentType
; }
254 //! Returs the number of pertinent children of a node.
255 int pertChildCount() const { return m_pertChildCount
; }
257 //! Sets the number of pertinent children of a node.
258 void pertChildCount(int count
) { m_pertChildCount
= count
; }
261 * The default function putSibling()
262 * stores a new sibling at a free sibling pointer
263 * of the node. This is only possible, if the node has at most one sibling.
264 * The function then detects a non used sibling pointer and places \a newSib
265 * onto it. putSibling() returns 0 if there have been two siblings
266 * detected, occupying the two possible pointers. In this case the new sibling
267 * \a newSib cannot be stored. If there was at a maximum one sibling stored,
268 * the function will place \a newSib on the free pointer and return either
269 * \a LEFT or \a RIGHT, depending wich pointer has been used.
271 * This function will always scan the pointer to the left brother first.
273 SibDirection
putSibling(PQNode
<T
,X
,Y
>* newSib
)
275 if (m_sibLeft
== 0) {
280 OGDF_ASSERT(m_sibRight
== 0);
286 * The function putSibling()
287 * with preference stores a new sibling at a free sibling pointer
288 * of the node. This is only possible, if the node has at most one sibling.
289 * The function then detects a non used sibling pointer and places \a newSib
290 * onto it. putSibling() returns 0 if there have been two siblings
291 * detected, occupying the two possible pointers. In this case the new sibling
292 * \a newSib could not be stored. If there was at a maximum one sibling
293 * stored, the function will place \a newSib on the free pointer and
294 * return either \a LEFT or \a RIGHT, depending wich pointer has been used.
296 * This function scans the brother first, which has been specified in the
297 * preference. If the preference has value \a LEFT, it scans the pointer
298 * to the left brother first. If the value is \a RIGHT, it scans the pointer
299 * to the right brother first.
301 SibDirection
putSibling(PQNode
<T
,X
,Y
>* newSib
, int preference
)
303 if (preference
== LEFT
)
304 return putSibling(newSib
);
306 OGDF_ASSERT(preference
== RIGHT
);
314 OGDF_ASSERT(m_sibLeft
== 0);
319 //! Returns a pointer to the reference child if node is a P-node.
320 PQNode
<T
,X
,Y
>* referenceChild() const { return m_referenceChild
; }
322 //! Returns the pointer to the parent if node is a reference child.
323 PQNode
<T
,X
,Y
>* referenceParent() const { return m_referenceParent
; }
325 //! Sets the pointer \a m_pointerToInfo to the specified adress of \a pointerToInfo.
326 bool setNodeInfo(PQNodeKey
<T
,X
,Y
>* pointerToInfo
) {
327 m_pointerToInfo
= pointerToInfo
;
328 if (pointerToInfo
!= 0)
330 m_pointerToInfo
->setNodePointer(this);
338 * getKey() returns a pointer to the PQLeafKeyof a node, in case that
339 * the node is supposed to have a key, such as elements of the derived
340 * class template PQLeaf.
341 * The key contains information and is of type PQLeafKey.
343 virtual PQLeafKey
<T
,X
,Y
>* getKey() const = 0;
345 //! Sets a specified pointer variable in a derived class to the specified adress of \a pointerToKey that is of type PQLeafKey.
347 * If a derived class, such as PQInternalNode, is not supposed to store
348 * informations of type PQLeafKey, setKey() ignores the informations as long as
349 * \a pointerToKey = 0. The return value then is 1.
350 * In case that \a pointerToKey != 0, the return value is 0.
352 * If a derived class, such as PQLeaf is supposed to
353 * store informations of type PQLeafKey, \a pointerToKey
354 * has to be instantiated by the client. The function setKey() does
355 * not instantiate the corresponding variable in the derived class.
356 * The return value is always 1 unless \a pointerKey was equal to 0.
358 virtual bool setKey(PQLeafKey
<T
,X
,Y
>* pointerToKey
) = 0;
361 * getInternal() returns a pointer to the PQInternalKey
362 * information of a node, in case that
363 * the node is supposed to have PQInternalKey information,
364 * such as elements of the derived class template PQInternalNode.
365 * The internal information is of type PQInternalKey.
367 virtual PQInternalKey
<T
,X
,Y
>* getInternal() const = 0;
370 * setInternal() sets a specified pointer variable in a derived class
371 * to the specified adress of \a pointerToInternal that is of type
374 * If a derived class, such as PQLeaf,
375 * is not supposed to store informations of type PQInternalKey,
376 * setInternal() ignores the informations as long as
377 * \a pointerToInternal = 0. The return value then is 1.
378 * In case that \a pointerToInternal != 0, the return value is 0.
380 * If a derived class, such as PQInternalNode is
381 * supposed to store informations of type PQInternalKey,
382 * \a pointerToInternal has to be instantiated by the client. The
383 * function setInternal() does
384 * not instantiate the corresponding variable in the derived class.
385 * The return value is always 1 unless \a pointerInternal was
388 virtual bool setInternal(PQInternalKey
<T
,X
,Y
>* pointerToInternal
) = 0;
391 * mark() returns the variable \a m_mark in the
392 * derived class PQLeaf and PQInternalNode.
393 * In a derived class this function has to return the designation used in
394 * the first pass of Booth and Luekers algorithm called Bubble(). A
395 * node then is either marked \a BLOCKED, \a UNBLOCKED or \a QUEUED (see PQNode).
397 virtual PQNodeMark
mark() const = 0;
399 //! mark() sets the variable \a m_mark in the derived class PQLeaf and PQInternalNode.
400 virtual void mark(PQNodeMark
) = 0;
402 //! Returns the variable \a m_status in the derived class PQLeaf and PQInternalNode.
404 * Its objective is to manage
405 * status of a node in the PQ-tree. A status is
406 * any kind of information of the current situation in the frontier of
407 * a node (the frontier of a node are all descendant leaves of the
408 * node). A status is anything such as \a EMPTY, \a FULL or
409 * \a PARTIAL (see PQNode). Since there might be more than those three possibilities,
410 * (e.g. in computing planar subgraphs this
411 * function probably has to be overloaded by the client.
413 virtual PQNodeStatus
status() const = 0;
415 //! Sets the variable \a m_status in the derived class PQLeaf and PQInternalNode.
416 virtual void status(PQNodeStatus
) = 0;
418 //! Returns the variable \a m_type in the derived class PQLeaf and PQInternalNode.
420 * Its objective it to manage the type of a node.
421 * node the current node is. The type of a node in the class template
422 * PQTree is either \a PNode, \a QNode or \a leaf (see PQNode).
423 * There may be of course more types such as <em>sequence indicators</em>.
425 * Observe that the derived class template PQLeaf does
426 * not have a variable \a m_type, since it obviously is of type \a leaf.
428 virtual PQNodeType
type() const = 0;
430 //! Sets the variable \a m_type in the derived class PQLeaf and PQInternalNode.
431 virtual void type(PQNodeType
) = 0;
437 // Stores the number of children of the node.
441 * Needed for debuging
442 * purposes. The PQ-trees can be visualized with the help of the <em>Tree
443 * Interface</em> and the \a m_debugTreeNumber is needed to print out the
444 * tree in the correct file format.
446 int m_debugTreeNumber
;
449 * Each node that has been introduced once into
450 * the tree gets a unique number. If the node is removed from the
451 * tree during a reduction or with the help of one of the functions
452 * that is provided by the class template PQtree, its number <b>is not reused</b>.
453 * This always allows exact identification of nodes
454 * during any process that is envoked on the PQ-tree. We strongly
455 * recommend users who construct the tree with the help of the
456 * construction functions and who instantiate the nodes by them selves
459 int m_identificationNumber
;
461 //! Stores the type of the parent which can be either a P- or Q-node.
464 //! Stores the number of pertinent children of the node.
465 int m_pertChildCount
;
467 //! Stores the number of pertinent leaves in the frontier of the node.
470 //! Stores a pointer to the first full child of a Q-node.
471 PQNode
<T
,X
,Y
> *m_firstFull
;
473 PQNode
<T
,X
,Y
> *m_leftEndmost
;
476 * Is a pointer to the parent. Observe that this
477 * pointer may not be up to date after a few applications of the
480 PQNode
<T
,X
,Y
> *m_parent
;
483 * Stores a pointer to one child, the <b>reference child</b> of the
484 * doubly linked cirkular list of children of a
485 * P-node. With the help of this pointer, it is possible to access
486 * the children of the P-node
488 PQNode
<T
,X
,Y
> *m_referenceChild
;
491 * Is a pointer to the parent, in case that the
492 * parent is a P-node and the node itself is its reference child.
493 * The pointer is needed in order to identify the reference child
494 * among all children of a P-node.
496 PQNode
<T
,X
,Y
> *m_referenceParent
;
498 //! Stores the right endmost child of a Q-node.
499 PQNode
<T
,X
,Y
> *m_rightEndmost
;
502 * Stores a pointer ot the left sibling of PQNode.
503 * If PQNode is child of a Q-node and has no left sibling,
504 * \a m_sibLeft is set to 0. If PQNode is child of a P-node,
505 * all children of the P-node are linked in a cirkular list. In the
506 * latter case, \a m_sibLeft is never 0.
508 PQNode
<T
,X
,Y
> *m_sibLeft
;
511 * Stores a pointer ot the right sibling of PQNode.
512 * If PQNode is child of a Q-node and has no right sibling,
513 * \ m_sibRight is set to 0. If PQNode is child of a P-node,
514 * all children of the P-node are linked in a cirkular list. In the
515 * latter case, \a m_sibRight is never 0.
517 PQNode
<T
,X
,Y
> *m_sibRight
;
519 //! Stores a pointer to the corresponding information of the node.
520 PQNodeKey
<T
,X
,Y
> *m_pointerToInfo
;
523 //! Stores all full children of a node during a reduction.
524 List
<PQNode
<T
,X
,Y
>*> *fullChildren
;
526 //! Stores all partial children of a node during a reduction.
527 List
<PQNode
<T
,X
,Y
>*> *partialChildren
;
533 The function [[changeEndmost]] replaces the old endmost child [[oldEnd]] of
534 the node by a new child [[newEnd]].
535 If the node is a $Q$-node, then it must have two valid
536 pointers to its endmost children. If one of the endmost children is [[oldEnd]],
537 it is replaced by [[newEnd]].
538 The function [[changeEndmost]] returns [[1]] if it succeeded in
539 replacing [[oldEnd]] by [[newEnd]]. Otherwise the function returns
540 [[0]], leaving with an error message.
542 template<class T
,class X
,class Y
>
543 bool PQNode
<T
,X
,Y
>::changeEndmost(PQNode
<T
,X
,Y
>* oldEnd
, PQNode
<T
,X
,Y
>* newEnd
)
545 if (m_leftEndmost
== oldEnd
)
547 m_leftEndmost
= newEnd
;
550 else if (m_rightEndmost
== oldEnd
)
552 m_rightEndmost
= newEnd
;
559 The function [[changeSiblings]] replaces the old sibling [[oldSib]] of the
560 node by a new sibling [[newSib]].
562 If the node has [[oldSib]] as sibling, then it changes the
563 sibling pointer that references to [[oldSib]] and places [[newSib]]
566 The function [[changeSiblings]] returns [[1]] if it succeeded in replacing
567 [[oldSib]] by [[newSib]]. Otherwise the function returns
568 [[0]], leaving with an error message.
570 template<class T
,class X
,class Y
>
571 bool PQNode
<T
,X
,Y
>::changeSiblings(PQNode
<T
,X
,Y
>* oldSib
, PQNode
<T
,X
,Y
>* newSib
)
573 if (m_sibLeft
== oldSib
)
578 else if (m_sibRight
== oldSib
)
588 The (first) constructor combines the node with its information and
589 will automatically set the [[m_nodePointer]] (see \ref{basicKey}) of
590 the element of type [[PQNodeKey]] (see \ref{PQNodeKey}).
592 template<class T
,class X
,class Y
>
593 PQNode
<T
,X
,Y
>::PQNode(int count
,PQNodeKey
<T
,X
,Y
>* infoPtr
)
595 m_identificationNumber
= count
;
597 m_pertChildCount
= 0;
599 m_debugTreeNumber
= 0;
606 m_referenceChild
= 0;
607 m_referenceParent
= 0;
611 fullChildren
= OGDF_NEW List
<PQNode
<T
,X
,Y
>*>;
612 partialChildren
= OGDF_NEW List
<PQNode
<T
,X
,Y
>*>;
614 m_pointerToInfo
= infoPtr
;
615 infoPtr
->setNodePointer(this);
620 The (second) constructor is called,
621 if no information is available or neccessary.
623 template<class T
,class X
,class Y
>
624 PQNode
<T
,X
,Y
>::PQNode(int count
)
626 m_identificationNumber
= count
;
628 m_pertChildCount
= 0;
630 m_debugTreeNumber
= 0;
637 m_referenceChild
= 0;
638 m_referenceParent
= 0;
642 fullChildren
= OGDF_NEW List
<PQNode
<T
,X
,Y
>*>;
643 partialChildren
= OGDF_NEW List
<PQNode
<T
,X
,Y
>*>;