Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

PQTree.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Stack.h>
35 #include <ogdf/basic/Queue.h>
36 #include <ogdf/basic/Array.h>
37 
44 
45 namespace ogdf {
46 
47 template<class T,class X,class Y>
48 class PQTree
49 {
50 public:
51  PQTree();
52 
62  virtual ~PQTree() { Cleanup(); }
63 
79  bool addNewLeavesToTree(
80  PQInternalNode<T,X,Y> *father,
81  SListPure<PQLeafKey<T,X,Y>*> &leafKeys);
82 
87  void emptyNode(PQNode<T,X,Y>* nodePtr);
88 
98  virtual void front(
99  PQNode<T,X,Y>* nodePtr,
100  SListPure<PQLeafKey<T,X,Y>*> &leafKeys);
101 
102  virtual void CleanNode(PQNode<T,X,Y>* /* nodePtr */) { }
103 
115  virtual void Cleanup();
116 
123  virtual void clientDefinedEmptyNode(PQNode<T,X,Y>* nodePtr) {
124  emptyNode(nodePtr);
125  }
126 
135  virtual void emptyAllPertinentNodes();
136 
154  virtual int Initialize(SListPure<PQLeafKey<T,X,Y>*> &leafKeys);
155 
167  virtual bool Reduction(SListPure<PQLeafKey<T,X,Y>*> &leafKeys);
168 
170  PQNode<T,X,Y>* root() const {
171  return m_root;
172  }
173 
176  void writeGML(const char *fileName);
177  void writeGML(ostream &os);
179 
180 protected:
183 
186 
189 
191 
196 
199 
209 
218  virtual bool Bubble(SListPure<PQLeafKey<T,X,Y>*> &leafKeys);
219 
248  virtual bool Reduce(SListPure<PQLeafKey<T,X,Y>*> &leafKeys);
249 
270  virtual bool templateL1(PQNode<T,X,Y> *nodePtr, bool isRoot);
271 
292  virtual bool templateP1(PQNode<T,X,Y> *nodePtr, bool isRoot);
293 
313  virtual bool templateP2(PQNode<T,X,Y> **nodePtr);
314 
338  virtual bool templateP3(PQNode<T,X,Y> *nodePtr);
339 
364  virtual bool templateP4(PQNode<T,X,Y> **nodePtr);
365 
381  virtual bool templateP5(PQNode<T,X,Y> *nodePtr);
382 
406  virtual bool templateP6(PQNode<T,X,Y> **nodePtr);
407 
433  virtual bool templateQ1(PQNode<T,X,Y> *nodePtr, bool isRoot);
434 
464  virtual bool templateQ2(PQNode<T,X,Y> *nodePtr, bool isRoot);
465 
466  /*
467  * Template matching for Q-nodes with empty and/or partial children at both
468  * ends and a sequence of full and/or partial children in the middle.
469  * The Q-node must be the root of the pertinent subtree.
470  * The function requires as input any pointer to a node stored in
471  * \p nodePtr. If the node stored in \p nodePtr is a Q-node
472  * with empty and/or partial children at both ends and a sequence
473  * full or partial children in the middle,
474  * templateQ3() considers itself responsible for the node and will
475  * apply the template matching \b Q3 to \p nodePtr.
476  * Observe that the user calling this function has to make sure that
477  * \p nodePtr is the root of the pertinent subtree.
478  *
479  * If templateQ3() was responsible for \p nodePtr and the
480  * reduction was successful, the return value is 1. Otherwise
481  * the return value is 0.
482  *
483  * Below a short description is given of all different cases that
484  * may occure and are handled by the function templateQ3(), \b regardless
485  * whether the Q-node \p nodePtr is the root of the pertinent subtree or not.
486  * The description is somewhat trunkated and should be understood as a
487  * stenographic description of the labels of the children of \p nodePtr
488  * when running through the children from one side to the other. Of course
489  * we leave the mirror-images out.
490  * - empty, full, empty
491  * - empty, partial, full, partial, empty
492  * - empty, partial, full, empty
493  * - empty, partial, full, partial
494  * - partial, full, partial
495  * - empty, partial, partial, empty
496  * - empty, partial, partial
497  * - partial, partial
498  */
499  virtual bool templateQ3(PQNode<T,X,Y> *nodePtr);
500 
512  virtual bool addNodeToNewParent(
513  PQNode<T,X,Y>* parent,
514  PQNode<T,X,Y>* child);
515 
547  virtual bool addNodeToNewParent(
548  PQNode<T,X,Y>* parent,
549  PQNode<T,X,Y>* child,
550  PQNode<T,X,Y>* leftBrother,
551  PQNode<T,X,Y>* rightBrother);
552 
568  virtual bool checkIfOnlyChild(
569  PQNode<T,X,Y> *child,
570  PQNode<T,X,Y> *parent);
571 
577  virtual void destroyNode(PQNode<T,X,Y> *nodePtr) {
579  }
580 
595  virtual void exchangeNodes(
596  PQNode<T,X,Y> *oldNode,
597  PQNode<T,X,Y> *newNode);
598 
607  virtual void linkChildrenOfQnode(
608  PQNode<T,X,Y> *installed,
609  PQNode<T,X,Y> *newChild);
610 
620  virtual void removeChildFromSiblings(PQNode<T,X,Y>* nodePtr);
621 
670  virtual int removeNodeFromTree(
671  PQNode<T,X,Y>* parent,
672  PQNode<T,X,Y>* child);
673 
674 
676  return nodePtr->fullChildren;
677  }
678 
679 
681  return nodePtr->partialChildren;
682  }
683 
684  virtual PQNode<T,X,Y>* clientLeftEndmost(PQNode<T,X,Y>* nodePtr) const {
685  return nodePtr->m_leftEndmost;
686  }
687 
688  virtual PQNode<T,X,Y>* clientRightEndmost(PQNode<T,X,Y>* nodePtr) const {
689  return nodePtr->m_rightEndmost;
690  }
691 
693  PQNode<T,X,Y>* other) const {
694  return nodePtr->getNextSib(other);
695  }
696 
697  virtual PQNode<T,X,Y>* clientSibLeft(PQNode<T,X,Y>* nodePtr) const {
698  return nodePtr->m_sibLeft;
699  }
700 
701  virtual PQNode<T,X,Y>* clientSibRight(PQNode<T,X,Y>* nodePtr) const {
702  return nodePtr->m_sibRight;
703  }
704 
712  virtual int clientPrintNodeCategorie(PQNode<T,X,Y>* nodePtr);
713 
721  virtual const char* clientPrintStatus(PQNode<T,X,Y>* nodePtr);
722 
730  virtual const char* clientPrintType(PQNode<T,X,Y>* nodePtr);
731 
732 private:
754  bool checkChain(
755  PQNode<T,X,Y> *nodePtr,
756  PQNode<T,X,Y> *firstFull,
757  PQNode<T,X,Y> **seqStart,
758  PQNode<T,X,Y> **seqEnd);
759 
774  PQNode<T,X,Y> *nodePtr,
775  PQNode<T,X,Y> *partialChild);
776 
791 
808  void removeBlock(PQNode<T,X,Y> *nodePtr, bool isRoot);
809 
814  void sortExceptions(int Exceptions[], int arraySize);
815 };
816 
817 template<class T,class X,class Y>
819  PQInternalNode<T,X,Y> *father,
820  SListPure<PQLeafKey<T,X,Y>*> &leafKeys)
821 {
822  if (!leafKeys.empty())
823  {
824  OGDF_ASSERT(!father->m_childCount);
825  // Father has children. Brothers expected
826 
828  SListIterator<PQLeafKey<T,X,Y>*> it = leafKeys.begin();
829  PQLeafKey<T,X,Y>* newKey = *it; //leafKeys[0];
830 
832  PQNode<T,X,Y>* firstSon = aktualSon;
833  firstSon->m_parent = father;
834  firstSon->m_parentType = father->type();
835  father->m_childCount++;
836  PQNode<T,X,Y>* oldSon = firstSon;
837 
839  for (++it; it.valid(); ++it)
840  {
841  newKey = *it; //leafKeys[i];
842  aktualSon = new PQLeaf<T,X,Y>(m_identificationNumber++, PQNodeRoot::PQNodeStatus::Empty,newKey);
843  aktualSon->m_parent = father;
844  aktualSon->m_parentType = father->type();
845  father->m_childCount++;
846  oldSon->m_sibRight = aktualSon;
847  aktualSon->m_sibLeft = oldSon;
848  oldSon = aktualSon;
849  }
850  if (father->type() == PQNodeRoot::PQNodeType::PNode)
852  {
853  firstSon->m_sibLeft = oldSon;
854  oldSon->m_sibRight = firstSon;
855  father->m_referenceChild = firstSon;
856  firstSon->m_referenceParent = father;
857  }
858  else if (father->type() == PQNodeRoot::PQNodeType::QNode)
860  {
861  father->m_leftEndmost = firstSon;
862  father->m_rightEndmost = oldSon;
863  }
864  return true;
865  }
866 
867  return false;
868 }
869 
870 template<class T,class X,class Y>
872  PQNode<T,X,Y>* parent,
873  PQNode<T,X,Y>* child)
874 {
877  //parent type not valid.
878 
879  if (child != nullptr)
880  {
881  OGDF_ASSERT(parent->m_childCount == 0);
882  //when adding new nodes: Brothers expected.
883  child->m_parent = parent;
884  child->m_parentType = parent->type();
885  parent->m_childCount++;
886 
887  /*
888  Set the reference pointers in case that [[parent]] is a $P$-node.
889  If [[parent]] is a $Q$-node, this chunk sets the endmost children
890  of [[parent]]. Since [[child]] is the only child of [[parent]]
891  both endmost pointers are set to [[child]].
892  */
893  if (parent->type() == PQNodeRoot::PQNodeType::PNode)
894  {
895  child->m_sibLeft = child;
896  child->m_sibRight = child;
897  parent->m_referenceChild = child;
898  child->m_referenceParent = parent;
899  }
900  else if (parent->type() == PQNodeRoot::PQNodeType::QNode)
901  {
902  parent->m_leftEndmost = child;
903  parent->m_rightEndmost = child;
904  }
905 
906  return true;
907  }
908 
909  return false;
910 }
911 
912 template<class T,class X,class Y>
914  PQNode<T,X,Y>* parent,
915  PQNode<T,X,Y>* child,
916  PQNode<T,X,Y>* leftBrother,
917  PQNode<T,X,Y>* rightBrother)
918 {
919 
920  if (parent != nullptr)
921  {
923  //parent type not valid
924  if ((leftBrother == nullptr) && (rightBrother == nullptr))
925  return addNodeToNewParent(parent,child);
926  else if (child != nullptr)
927  {
928  child->m_parent = parent;
929  child->m_parentType = parent->type();
930  parent->m_childCount++;
931 
932  if (parent->type() == PQNodeRoot::PQNodeType::PNode)
933  {
934  /*
935  The parent is a $P$-node with children.
936  Either [[leftBrother]] or [[rightBrother]] stores
937  a pointer to an existing child of [[parent]] and [[parent]]
938  is a $P$-node. In case that two brothers are stored, an
939  arbitrary one is choosen to be the next sibling of [[child]].
940  This brother is stored in [[brother]]. The pointer [[sister]]
941  denotes a pointer to an arbitrary sibling of [[brother]].
942  */
943  PQNode<T,X,Y>* brother = (leftBrother != nullptr) ? leftBrother : rightBrother;
944  PQNode<T,X,Y>* sister = brother->m_sibRight;
945  child->m_sibLeft = brother;
946  child->m_sibRight = sister;
947  brother->m_sibRight = child;
948  sister->m_sibLeft = child;
949  return true;
950  }
951 
952  else if (leftBrother == nullptr)
953  {
954  /*
955  The parent is a $Q$-node with children.
956  The [[leftBrother]] is a [[0]]-pointer while the
957  [[rightBrother]] denotes an existing child of [[parent]].
958  The node [[rightBrother]] <b>must be</b> one of the two endmost
959  children of [[parent]]. If this is not the case, the chunk
960  detects this, halts the procedure [[addNewLeavesToTree]]
961  printing an error message and returning [[0]].
962  If [[rightBrother]] is endmost child of [[parent]], then
963  this chunk adds [[child]] at the one end where
964  [[rightBrother]] hides. The node [[child]] is then made the
965  new endmost child of [[parent]] on the corresponding side.
966  */
967  if (rightBrother == parent->m_leftEndmost)
968  {
969  parent->m_leftEndmost = child;
970  child->m_sibRight = rightBrother;
971  rightBrother->putSibling(child,PQNodeRoot::SibDirection::Left);
972  return true;
973  }
974 
975  // missing second brother?
976  OGDF_ASSERT(rightBrother == parent->m_rightEndmost);
977  parent->m_rightEndmost = child;
978  child->m_sibLeft = rightBrother;
979  rightBrother->putSibling(child,PQNodeRoot::SibDirection::Left);
980  return true;
981  }
982 
983  else if (rightBrother == nullptr)
984  {
985  /*
986  The parent is a $Q$-node with children.
987  The [[rightBrother]] is a [[0]]-pointer while the
988  [[leftBrother]] denotes an existing child of [[parent]]. The
989  node [[leftBrother]] <b>must be</b> one of the two endmost
990  children of [[parent]]. If this is not the case, the chunk
991  detects this, halts the procedure [[addNodeToNewParent]]
992  printing an error message and returning [[0]].
993  If [[leftBrother]] is endmost child of [[parent]], then this
994  chunk adds [[child]] at the one end where [[leftBrother]]
995  hides. The node [[child]] is then made new endmost child of
996  [[parent]] on the corresponding side.
997  */
998  if (leftBrother == parent->m_rightEndmost)
999  {
1000  parent->m_rightEndmost = child;
1001  child->m_sibLeft = leftBrother;
1002  leftBrother->putSibling(child,PQNodeRoot::SibDirection::Right);
1003  return true;
1004  }
1005 
1006  // missing second brother?
1007  OGDF_ASSERT(leftBrother == parent->m_leftEndmost);
1008  parent->m_leftEndmost = child;
1009  child->m_sibRight = leftBrother;
1010  leftBrother->putSibling(child,PQNodeRoot::SibDirection::Right);
1011  return true;
1012  }
1013 
1014  else
1015  {
1016  /*
1017  The parent is a $Q$-node with children.
1018  Both the [[rightBrother]] and the [[leftBrother]] denote
1019  existing children of [[parent]]. In this case, [[leftBrother]]
1020  and [[rightBrother]] must be immideate siblings. If this is
1021  not the case, this will be detected during the function call
1022  [[changeSiblings]] of the class [[PQNode.h]] (see
1023  \ref{PQNode.changeSiblings}) in the first two lines of this
1024  chunk. If the chunk recognizes the failure of
1025  [[changeSiblings]] it halts the procedure
1026  [[addNewLeavesToTree]], printing an error message and
1027  returning [[0]].
1028  If the two brothers are immediate siblings, this chunk
1029  adds [[child]] between the two brothers as interior child of
1030  the $Q$-node [[parent]].
1031  */
1032 #ifdef OGDF_DEBUG
1033  bool ok =
1034 #endif
1035  rightBrother->changeSiblings(leftBrother,child) && leftBrother->changeSiblings(rightBrother,child);
1036 
1037  // brothers are not siblings?
1038  OGDF_ASSERT(ok);
1039 
1040  if (leftBrother->m_sibRight == child)
1041  {
1042  child->m_sibLeft = leftBrother;
1043  child->m_sibRight = rightBrother;
1044  }
1045  else
1046  {
1047  child->m_sibLeft = rightBrother;
1048  child->m_sibRight = leftBrother;
1049  }
1050  return true;
1051  }
1052  }
1053  else
1054  return false;
1055  }
1056  else if (leftBrother != nullptr && rightBrother != nullptr)
1057  {
1058  /*
1059  The parent is a $Q$-node with children.
1060  Both the [[rightBrother]] and the [[leftBrother]] denote
1061  existing children of [[parent]]. In this case, [[leftBrother]]
1062  and [[rightBrother]] must be immideate siblings. If this is
1063  not the case, this will be detected during the function call
1064  [[changeSiblings]] of the class [[PQNode.h]] (see
1065  \ref{PQNode.changeSiblings}) in the first two lines of this
1066  chunk. If the chunk recognizes the failure of
1067  [[changeSiblings]] it halts the procedure
1068  [[addNewLeavesToTree]], printing an error message and
1069  returning [[0]].
1070  If the two brothers are immediate siblings, this chunk
1071  adds [[child]] between the two brothers as interior child of
1072  the $Q$-node [[parent]].
1073  */
1074 #ifdef OGDF_DEBUG
1075  bool ok =
1076 #endif
1077  rightBrother->changeSiblings(leftBrother,child) && leftBrother->changeSiblings(rightBrother,child);
1078 
1079  // brothers are not siblings?
1080  OGDF_ASSERT(ok);
1081 
1082  if (leftBrother->m_sibRight == child)
1083  {
1084  child->m_sibLeft = leftBrother;
1085  child->m_sibRight = rightBrother;
1086  }
1087  else
1088  {
1089  child->m_sibLeft = rightBrother;
1090  child->m_sibRight = leftBrother;
1091  }
1092  return true;
1093  }
1094 
1095  return true;
1096 }
1097 
1098 template<class T,class X,class Y>
1100 {
1101  /* Variables:
1102  * - \a blockcount is the number of blocks of blocked nodes during
1103  * the bubbling up phase.
1104  * - \a numBlocked is the number of blocked nodes during the
1105  * bubbling up phase.
1106  * - \a blockedSiblings counts the number of blocked siblings that
1107  * are adjacent to \a checkNode. A node has 0, 1 or 2 blocked siblings.
1108  * A child of a P-node has no blocked siblings. Endmost children of
1109  * Q-nodes have at most 1 blocked sibling. The interior children of
1110  * a Q-Node have at most 2 blocked siblings.
1111  * - \a checkLeaf is a pointer used for finding the pertinent leaves.
1112  * - \a checkNode is a pointer to the actual node.
1113  * - \a checkSib is a pointer used to examin the siblings of [[checkNode]].
1114  * - \a offTheTop is a variable which is either 0 (that is its
1115  * initial value) or 1 in case that the root of the tree has been
1116  * process during the first phase.
1117  * - \a parent is a pointer to the parent of \a checkNode, if \a checkNode
1118  * has a valid parent pointer.
1119  * - \a processNodes is a first-in first-out list that is used for
1120  * sequencing the order in which the nodes are processed.
1121  * - \a blockedNodes is a stack storing all nodes that have been
1122  * once blocked. In case that the [[m_pseudoRoot]] has to be
1123  * introduced, the stack contains the blocked nodes.
1124  */
1125  Queue<PQNode<T,X,Y>*> processNodes;
1126 
1127  /*
1128  Enter the [[Full]] leaves into the queue [[processNodes]].
1129  In a first step the pertinent leaves have to be identified in the tree
1130  and entered on to the queue [[processNodes]]. The identification of
1131  the leaves can be done with the help of a pointer stored in every
1132  [[PQLeafKey]] (see \ref{PQLeafKey}) in constant time for every element.
1133  */
1135  for (it = leafKeys.begin(); it.valid(); ++it)
1136  {
1137  PQNode<T,X,Y>* checkLeaf = (*it)->nodePointer(); //leafKeys[i]->nodePointer();
1139  processNodes.append(checkLeaf);
1140  m_pertinentNodes->pushFront(checkLeaf);
1141  }
1142 
1143  int blockCount = 0;
1144  int numBlocked = 0;
1145  int offTheTop = 0;
1146  PQNode<T,X,Y>* checkSib = nullptr;
1147  Stack<PQNode<T,X,Y>*> blockedNodes;
1148 
1149  while ((processNodes.size() + blockCount + offTheTop) > 1)
1150  {
1151  if (processNodes.size() == 0)
1152  /*
1153  No consecutive sequence possible.
1154  The queue [[processNodes]] does not contain any nodes for
1155  processing and the sum of [[blockCount]] and [[offTheTop]] is
1156  greater than 1. If the queue is empty, the root of the pertinent
1157  subtree was already processed. Nevertheless, there are blocked
1158  nodes since [[offTheTop]] is either be [[0]] or [[1]], hence
1159  [[blockCount]] must be at least [[1]]. Such blocked nodes cannot
1160  form a consecutive sequence with all nodes in the set
1161  [[leafKeys]]. Observe that this chunk finishes the function
1162  [[Bubble]]. Hence every memory allocated by the function [[Bubble]]
1163  has to be deleted here as well.
1164  */
1165  return false;
1166  /*
1167  If there are still nodes to be processed in which case the queue
1168  [[processNodes]] is not empty, we get the next node from the queue.
1169  By default this node has to be marked as blocked.
1170  */
1171  PQNode<T,X,Y>* checkNode = processNodes.pop();
1172  blockedNodes.push(checkNode);
1174  int blockedSiblings = 0;
1175 
1176  /*
1177  Check if node is adjacent to an unblocked node.
1178  After getting the node [[checkNode]] from the queue, its siblings are
1179  checked, whether they are unblocked. If they are, then they have a
1180  valid pointer to their parent and the parent pointer of [[checkNode]]
1181  is updated.
1182  */
1183  if ((checkNode->m_parentType != PQNodeRoot::PQNodeType::PNode) && (checkNode != m_root))
1184  // checkNode is son of a QNode.
1185  // Check if it is blocked.
1186  {
1187  if (clientSibLeft(checkNode) == nullptr)
1188  // checkNode is endmost child of
1189  // a QNode. It has a valid pointer
1190  // to its parent.
1191  {
1193  if (clientSibRight(checkNode) &&
1194  clientSibRight(checkNode)->mark() == PQNodeRoot::PQNodeMark::Blocked)
1195  blockedSiblings++;
1196  }
1197 
1198  else if (clientSibRight(checkNode) == nullptr)
1199  // checkNode is endmost child of
1200  // a QNode. It has a valid pointer
1201  // to its parent.
1202  {
1204  if (clientSibLeft(checkNode) &&
1205  clientSibLeft(checkNode)->mark() == PQNodeRoot::PQNodeMark::Blocked)
1206  blockedSiblings++;
1207  }
1208 
1209 
1210  else
1211  // checkNode is not endmost child of
1212  // a QNode. It has not a valid
1213  // pointer to its parent.
1214  {
1215  if (clientSibLeft(checkNode)->mark() == PQNodeRoot::PQNodeMark::Unblocked)
1216  // checkNode is adjacent to an
1217  // unblocked node. Take its parent.
1218  {
1220  checkNode->m_parent = clientSibLeft(checkNode)->m_parent;
1221  }
1222  else if (clientSibLeft(checkNode)->mark() == PQNodeRoot::PQNodeMark::Blocked)
1223  blockedSiblings++;
1224 
1225  if (clientSibRight(checkNode)->mark() == PQNodeRoot::PQNodeMark::Unblocked)
1226  // checkNode is adjacent to an
1227  // unblocked node. Take its parent.
1228  {
1230  checkNode->m_parent = clientSibRight(checkNode)->m_parent;
1231  }
1232  else if (clientSibRight(checkNode)->mark() == PQNodeRoot::PQNodeMark::Blocked)
1233  blockedSiblings++;
1234  }
1235  }
1236 
1237  else
1238  // checkNode is son of a PNode
1239  // and children of P_NODEs
1240  // cannot be blocked.
1242 
1243  if (checkNode->mark() == PQNodeRoot::PQNodeMark::Unblocked)
1244  {
1245  PQNode<T,X,Y>* parent = checkNode->m_parent;
1246 
1247  /*
1248  Get maximal consecutive set of blocked siblings.
1249  This chunk belongs to the procedure [[bubble]].
1250  The node [[checkNode]] is [[Unblocked]].
1251  If the parent of [[checkNode]] is a $Q$-Node, then we check the
1252  siblings [[checkSib]] of [[checkNode]] whether they are
1253  [[Blocked]]. If they are blocked, they have to be marked
1254  [[Unblocked]] since they are adjacent to the [[Unblocked]] node
1255  [[checkNode]]. We then have to proceed with the siblings of
1256  [[checkSib]] in order to find [[Blocked]] nodes
1257  adjacent to [[checkSib]]. This is repeated until no [[Blocked]]
1258  nodes are found any more.
1259 
1260  Observe that while running through the children of the $Q$-Node
1261  (referred by the pointer [[parent]]), their parent pointers,
1262  as well as the [[pertChildCount]] of [[parent]] are updated.
1263  Furthermore we reduce simultaneously the count [[numBlocked]].
1264  */
1265  if (blockedSiblings > 0)
1266  {
1267  if (clientSibLeft(checkNode) != nullptr)
1268  {
1269  checkSib = clientSibLeft(checkNode);
1270  PQNode<T,X,Y>* oldSib = checkNode;
1271  while (checkSib->mark() == PQNodeRoot::PQNodeMark::Blocked)
1272  {
1274  checkSib->m_parent = parent;
1275  numBlocked--;
1276  parent->m_pertChildCount++;
1277  PQNode<T,X,Y>* holdSib = clientNextSib(checkSib,oldSib);
1278  oldSib = checkSib;
1279  checkSib = holdSib;
1280  //Blocked node as endmost child of a QNode.
1281  }
1282  }
1283 
1284  if (clientSibRight(checkNode) != nullptr)
1285  {
1286  checkSib = clientSibRight(checkNode);
1287  PQNode<T,X,Y>* oldSib = checkNode;
1288  while (checkSib->mark() == PQNodeRoot::PQNodeMark::Blocked)
1289  {
1291  checkSib->m_parent = parent;
1292  numBlocked--;
1293  parent->m_pertChildCount++;
1294  PQNode<T,X,Y>* holdSib = clientNextSib(checkSib,oldSib);
1295  oldSib = checkSib;
1296  checkSib = holdSib;
1297  //Blocked node as endmost child of a QNode.
1298  }
1299  }
1300  }// if (blockedSiblings > 0)
1301 
1302 
1303  /*
1304  Process parent of [[checkNode]]
1305  After processing the siblings of the [[Unblocked]] [[checkNode]]
1306  the parent has to be processed. If [[checkNode]] is the root
1307  of the tree we do nothing except setting the flag [[offTheTop]].
1308  If it is not the root and [[parent]] has not been placed onto the
1309  queue [[processNodes]], the [[parent]] is placed on to
1310  [[processNodes]].
1311 
1312  Observe that the number [[blockCount]] is updated. Since
1313  [[checkNode]] was [[Unblocked]] all pertinent nodes adjacent
1314  to that node became [[Unblocked]] as well. Therefore the number
1315  of blocks is reduced by the number of [[Blocked]] siblings of
1316  [[checkNode]].
1317  */
1318  if (parent == nullptr)
1319  // checkNode is root of the tree.
1320  offTheTop = 1;
1321  else
1322  // checkNode is not the root.
1323  {
1324  parent->m_pertChildCount++;
1325  if (parent->mark() == PQNodeRoot::PQNodeMark::Unmarked)
1326  {
1327  processNodes.append(parent);
1328  m_pertinentNodes->pushFront(parent);
1330  }
1331  }
1332 
1333  blockCount -= blockedSiblings;
1334  blockedSiblings = 0;
1335 
1336  }//if (checkNode->mark() == Unblocked)
1337 
1338  else
1339  {
1340  /*
1341  Process blocked [[checkNode]]
1342  Since [[checkNode]] is [[Blocked]], we cannot continue
1343  processing at this point in the Tree. We have to wait until
1344  this node becomes unblocked. So only the variables
1345  [[blockCount]] and [[numBlocked]] are updated.
1346  */
1347  blockCount += 1 - blockedSiblings;
1348  numBlocked++;
1349  }
1350 
1351  }//while ((processNodes.size() + blockCount + offTheTop) > 1)
1352 
1353  if (blockCount == 1)
1354  {
1355  /*
1356  If [[blockCount]] $= 1$ enter [[m_pseudoRoot]] to the tree
1357  In case that the root of the pertinent subtree is a $Q$-node
1358  with empty children on both sides and the pertinent children
1359  in the middle, it is possible that the $PQ$-tree is reducible.
1360  But since the sequence of pertinent children of the $Q$-node is
1361  blocked, the procedure is not able to find the parent of its
1362  pertinent children. This is due to the fact that the interior
1363  children of a $Q$-node do not have a valid parent pointer.
1364 
1365  So the root of the pertinent subtree is not known, hence cannot be
1366  entered into the processing queue used in the function call [[Reduce]]
1367  (see \ref{Reduce}). To solve this problem a special node only designed
1368  for this cases is used: [[m_pseudoRoot]]. It simulates the root of the
1369  pertinent subtree. This works out well, since for this node the only
1370  possible template maching is [[templateQ3]] (see \ref{templateQ3}),
1371  where no pointers to the endmost children of a $Q$-node are used.
1372  */
1373  while (!blockedNodes.empty())
1374  {
1375  PQNode<T,X,Y>* checkNode = blockedNodes.pop();
1376  if (checkNode->mark() == PQNodeRoot::PQNodeMark::Blocked)
1377  {
1379  checkNode->m_parent = m_pseudoRoot;
1380  m_pseudoRoot->m_pertChildCount++;
1381  OGDF_ASSERT(!checkNode->endmostChild());
1382  //Blocked node as endmost child of a QNode.
1383  }
1384  }
1385  }
1386 
1387  return true;
1388 }
1389 
1390 template<class T,class X,class Y>
1392  PQNode<T,X,Y> *nodePtr,
1393  PQNode<T,X,Y> *firstFull,
1394  PQNode<T,X,Y> **seqStart,
1395  PQNode<T,X,Y> **seqEnd)
1396 {
1397  /* Variables:
1398  * - \a fullCount counts the number of children that are
1399  * discovered by the function checkChain(). This is necessary, since
1400  * checkChain() is used by two template matching functions
1401  * templateQ2() and templateQ3() where in the latter case the
1402  * pointer \a firstFull may point to any full child in the front of the Q-node
1403  * \a nodePtr.
1404  * - \a notFull is set 1 when an empty child is encountered.
1405  * - \a checkNode is the actual node that is examined.
1406  * - \a leftNext is the next node that has to be examined on the
1407  * left side of \a firstFull.
1408  * - \a leftOld is the node that has been examined right before
1409  * \a checkNode on the left side of \a firstFull.
1410  * - \a rightNext is the next node that has to be examined on the
1411  * right side of \a firstFull. Not needed when checkChain() was
1412  * called by templateQ2().
1413  * - \a rightOld is the node that has been examined right before
1414  * \a checkNode on the right side of \a firstFull.
1415  * Not needed when checkChain() was called by templateQ2().
1416  */
1417  bool notFull = false;
1418  int fullCount = nodePtr->fullChildren->size();
1419  fullCount--; // firstFull does have a Full label.
1420 
1421  /*
1422  Start at the [[firstFull]] child sweeping through the full children on
1423  the left side of [[firstfull]]. It stops as soon as a nonfull child is
1424  detected. The last found full child is stored in [[seqEnd]].
1425  */
1426  PQNode<T,X,Y> *leftNext = clientSibLeft(firstFull);
1427  (*seqEnd) = firstFull;
1428  if (leftNext != nullptr)
1429  {
1430  if (leftNext->status() == PQNodeRoot::PQNodeStatus::Full) {
1431  fullCount--;
1432 
1433  PQNode<T,X,Y> *leftOld = firstFull;
1434  PQNode<T,X,Y> *checkNode = leftNext;
1435 
1436  while (fullCount > 0 && !notFull)
1437  // There are still full children to be
1438  // counted, and no empty child has been
1439  // encountered on this side.
1440  {
1441  leftNext = clientNextSib(checkNode,leftOld);
1442  if (leftNext != nullptr && leftNext->status() == PQNodeRoot::PQNodeStatus::Full)
1443  fullCount--;
1444  else
1445  notFull = true;
1446  leftOld = checkNode;
1447  checkNode = leftNext;
1448  }
1449 
1450  if (checkNode != nullptr && checkNode->status() == PQNodeRoot::PQNodeStatus::Full)
1451  (*seqEnd) = checkNode;
1452 
1453  else {
1454  //searching consecutive sequence in Q2 or Q3.
1455  OGDF_ASSERT(leftOld != nullptr);
1457  (*seqEnd) = leftOld;
1458  }
1459 
1460  } else {
1461  (*seqEnd) = firstFull;
1462  }
1463  }
1464 
1465  /*
1466  Start at the [[firstFull]] child sweeping through the full children on
1467  the right side of [[firstfull]]. It stops as soon as a nonfull child is
1468  detected.
1469  */
1470  notFull = false;
1471  PQNode<T,X,Y> *rightNext = clientSibRight(firstFull);
1472  (*seqStart) = firstFull;
1473  if(rightNext != nullptr)
1474  {
1475  if (rightNext->status() == PQNodeRoot::PQNodeStatus::Full) {
1476  fullCount--;
1477 
1478  PQNode<T,X,Y> *rightOld = firstFull;
1479  PQNode<T,X,Y> *checkNode = rightNext;
1480 
1481  while (fullCount > 0 && !notFull)
1482  // There are still full children to be
1483  // counted, and no empty child has been
1484  // encountered on this side.
1485  {
1486  rightNext = clientNextSib(checkNode,rightOld);
1487  if (rightNext != nullptr && rightNext->status() == PQNodeRoot::PQNodeStatus::Full)
1488  fullCount--;
1489  else
1490  notFull = true;
1491  rightOld = checkNode;
1492  checkNode = rightNext;
1493  }
1494  if (checkNode != nullptr && checkNode->status() == PQNodeRoot::PQNodeStatus::Full)
1495  (*seqStart) = checkNode;
1496 
1497  else {
1498  OGDF_ASSERT(rightOld != nullptr);
1500  (*seqStart) = rightOld;
1501  //searching consecutive seqeuence in Q2 or Q3.
1502  }
1503 
1504  } else {
1505  (*seqStart) = firstFull;
1506  }
1507  }
1508 
1509  if (firstFull == (*seqEnd))
1510  {
1511  PQNode<T,X,Y> *checkNode = (*seqEnd);
1512  (*seqEnd) = (*seqStart);
1513  (*seqStart) = checkNode;
1514  }
1515 
1516  if (fullCount == 0)
1517  // All full children occupy a consecutive
1518  // sequence.
1519  return true;
1520  else
1521  return false;
1522 }
1523 
1524 template<class T,class X,class Y>
1526  PQNode<T,X,Y> *child,
1527  PQNode<T,X,Y> *parent)
1528 {
1529  if ((parent->type() == PQNodeRoot::PQNodeType::PNode && parent->m_childCount == 1)
1530  || (parent->type() == PQNodeRoot::PQNodeType::QNode && parent->m_leftEndmost == child
1531  && parent->m_rightEndmost == child))
1532  {
1533  removeChildFromSiblings(child);
1534  child->m_parent = parent->m_parent;
1535  if (parent->m_parent != nullptr) // parent is not the root.
1536  exchangeNodes(parent,child);
1537  else
1538  {
1539  exchangeNodes(parent,child);
1540  m_root = child;
1541  }
1542  destroyNode(parent);
1543  return true;
1544  }
1545  else
1546  return false;
1547 }
1548 
1549 template<class T,class X,class Y>
1551 {
1552  /* In order to free the allocated memory, all nodes of the
1553  * tree have to be deleted, hence there destructors are called.
1554  * In order to achieve this, we start at the root of the tree and go down the
1555  * tree to the leaves for reaching every node. When a node is processed,
1556  * (besides the #m_root, this will always be the node \a checkNode)
1557  * the pointers of all its children are stored in a queue \a helpqueue and
1558  * then the processed node is deleted.
1559  *
1560  * The use of a queue \a helpqueue is a must, since the nodes do not
1561  * have pointers to all of their children, as the children mostly do
1562  * not have a pointer to their parent.
1563  *
1564  * It might look weird at the first glance that the function Cleanup()
1565  * calls the function emptyAllPertinentNodes(), but if some nodes were removed
1566  * during a reduction, they were stored in the stack #m_pertinentNodes.
1567  * These nodes have to be deleted as well
1568  * which is provided by the function emptyAllPertinentNodes().
1569  */
1570  PQNode<T,X,Y>* nextSon = nullptr;
1571  PQNode<T,X,Y>* lastSon = nullptr;
1572 
1573  Queue<PQNode<T,X,Y>*> helpqueue;
1574 
1575  if (m_root != nullptr)
1576  {
1578  PQNode<T,X,Y>* oldSib = nullptr;
1579 
1580  /*
1581  Process the [[m_root]] of the [[PQTree]]. Before deleting [[m_root]],
1582  pointers to all its children are stored in the queue [[helpqueue]].
1583  */
1584  if (m_root->type() == PQNodeRoot::PQNodeType::PNode)
1585  {
1586  if (m_root->m_referenceChild != nullptr)
1587  {
1588  PQNode<T,X,Y>* firstSon = m_root->m_referenceChild;
1589  helpqueue.append(firstSon);
1590 
1591  if (firstSon->m_sibRight != nullptr)
1592  nextSon = firstSon->m_sibRight;
1593  while (nextSon != firstSon)
1594  {
1595  helpqueue.append(nextSon);
1596  nextSon = nextSon->m_sibRight;
1597  }
1598  }
1599  }
1600  else if (m_root->type() == PQNodeRoot::PQNodeType::QNode)
1601  {
1602  PQNode<T,X,Y>* firstSon = m_root->m_leftEndmost;
1603  helpqueue.append(firstSon);
1604 
1605  lastSon = m_root->m_rightEndmost;
1606  helpqueue.append(lastSon);
1607 
1608  nextSon = lastSon->getNextSib(oldSib);
1609  oldSib = lastSon;
1610  while (nextSon != firstSon)
1611  {
1612  helpqueue.append(nextSon);
1613  PQNode<T,X,Y>* holdSib = nextSon->getNextSib(oldSib);
1614  oldSib = nextSon;
1615  nextSon = holdSib;
1616  }
1617  }
1618 
1619 
1620  CleanNode(m_root);
1621  delete m_root;
1622 
1623  while (!helpqueue.empty())
1624  {
1625  PQNode<T,X,Y>* checkNode = helpqueue.pop();
1626 
1627  /*
1628  Process an arbitrary node [[checkNode]] of the [[PQTree]].
1629  Before deleting [[checkNode]],
1630  pointers to all its children are stored in the queue [[helpqueue]].
1631  */
1632  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
1633  {
1634  if (checkNode->m_referenceChild != nullptr)
1635  {
1636  PQNode<T,X,Y>* firstSon = checkNode->m_referenceChild;
1637  helpqueue.append(firstSon);
1638 
1639  if (firstSon->m_sibRight != nullptr)
1640  nextSon = firstSon->m_sibRight;
1641  while (nextSon != firstSon)
1642  {
1643  helpqueue.append(nextSon);
1644  nextSon = nextSon->m_sibRight;
1645  }
1646  }
1647  }
1648  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
1649  {
1650  oldSib = nullptr;
1651 
1652  PQNode<T,X,Y>*firstSon = checkNode->m_leftEndmost;
1653  helpqueue.append(firstSon);
1654 
1655  lastSon = checkNode->m_rightEndmost;
1656  helpqueue.append(lastSon);
1657 
1658  nextSon = lastSon->getNextSib(oldSib);
1659  oldSib = lastSon;
1660  while (nextSon != firstSon)
1661  {
1662  helpqueue.append(nextSon);
1663  PQNode<T,X,Y>* holdSib = nextSon->getNextSib(oldSib);
1664  oldSib = nextSon;
1665  nextSon = holdSib;
1666  }
1667  }
1668 
1669  CleanNode(checkNode);
1670  delete checkNode;
1671  }
1672  }
1673 
1675  delete m_pseudoRoot;
1676 
1677  delete m_pertinentNodes;
1678 
1679  m_root = nullptr;
1680  m_pertinentRoot = nullptr;
1681  m_pseudoRoot = nullptr;
1682  m_pertinentNodes = nullptr;
1683 
1684  m_numberOfLeaves = 0;
1686 }
1687 
1688 template<class T,class X,class Y>
1690 {
1691  return (nodePtr != nullptr) ? 1 : 0;
1692  // 1 is the standard node categrie in the Tree Interface.
1693 }
1694 
1695 template<class T,class X,class Y>
1697 {
1698  return (nodePtr != nullptr) ? "ERROR" : "ERROR: clientPrintStatus: NO NODE ACCESSED";
1699 }
1700 
1701 template<class T,class X,class Y>
1703 {
1704  return (nodePtr != nullptr) ? "ERROR" : "ERROR: clientPrintType: NO NODE ACCESSED";
1705 }
1706 
1707 template<class T,class X,class Y> PQTree<T,X,Y>::PQTree()
1708 {
1709  m_root = nullptr;
1710  m_pertinentRoot = nullptr;
1711  m_pseudoRoot = nullptr;
1712 
1713  m_numberOfLeaves = 0;
1715 
1716  m_pertinentNodes = nullptr;
1717 }
1718 
1719 template<class T,class X,class Y>
1721  PQNode<T,X,Y> *nodePtr,
1722  PQNode<T,X,Y> *partialChild)
1723 {
1724  if (nodePtr->fullChildren->size() > 0)
1725  // There are some full children to
1726  // be copied.
1727  {
1728  nodePtr->m_childCount = nodePtr->m_childCount -
1729  nodePtr->fullChildren->size();
1730 
1732 
1733  // Introduce newNode as endmost
1734  // child of the partial Q-node.
1735  partialChild->m_childCount++;
1736  partialChild->fullChildren->pushFront(newNode);
1737 
1738  if (clientLeftEndmost(partialChild)->status() == PQNodeRoot::PQNodeStatus::Full)
1739  {
1740  PQNode<T,X,Y> *checkNode = partialChild->m_leftEndmost;
1741  partialChild->m_leftEndmost = newNode;
1742  linkChildrenOfQnode(checkNode,newNode);
1743 
1744  }
1745  else {
1746  // ERROR: Endmostchild not found?
1748 
1749  PQNode<T,X,Y> *checkNode = partialChild->m_rightEndmost;
1750  partialChild->m_rightEndmost = newNode;
1751  linkChildrenOfQnode(checkNode,newNode);
1752  }
1753 
1754  newNode->m_parent = partialChild;
1756  }
1757 }
1758 
1759 template<class T,class X,class Y>
1761  List<PQNode<T,X,Y>*> *fullNodes)
1762 {
1763  /* Variables:
1764  * - \a newNode stores the adress of the new allocated P-node or
1765  * the adress of the only full child.
1766  * - \a oldSon is a variable used for adding the full nodes as
1767  * children to the new P-node.
1768  * - \a firstSon stores the adress of the first detected full
1769  * child. It is needed for adding the full nodes as
1770  * children to the new P-node.
1771  * - \a checkSon is a variable used for adding the full nodes as
1772  * children to the new P-node.
1773  * - \a newPQnode is used for proper allocation of the new P-node.
1774  */
1775  PQNode<T,X,Y> *newNode = nullptr;
1776 
1777  if (fullNodes->size() == 1)
1778  {
1779  /*
1780  There is just one full child. So no new $P$-node is created. The
1781  full child is copied as child to the [[partialChild]].
1782  */
1783  newNode = fullNodes->popFrontRet();
1784  removeChildFromSiblings(newNode);
1785  }
1786 
1787  else
1788  {
1789  /*
1790  This chunk belongs to the function [[createNodeAndCopyFullChildren]].
1791  There is more than one full child, so a new $P$-node is created.
1792  This chunk first allocates the memory for the new $P$-node that will
1793  be stored in [[newNode]]. Then it pops the nodes out of the stack
1794  [[fullNodes]] and introduces them as sons of [[newNode]].
1795  Popping the nodes out of the stack implies at the same time
1796  that they are removed from the [[fullChildren]] stack of the
1797  $P$-node of their parent.
1798  */
1800  m_pertinentNodes->pushFront(newNode);
1801  newNode->m_pertChildCount = fullNodes->size();
1802  newNode->m_childCount = fullNodes->size();
1803 
1804  /*
1805  The first node is copied separately, since we need the pointer to it
1806  for setting the pointers to the siblings of the next full nodes.
1807  */
1808  PQNode<T,X,Y> *firstSon = fullNodes->popFrontRet();
1809  removeChildFromSiblings(firstSon);
1810  newNode->fullChildren->pushFront(firstSon);
1811  firstSon->m_parent = newNode;
1812  firstSon->m_parentType = newNode->type();
1813  PQNode<T,X,Y> *oldSon = firstSon;
1814 
1815 
1816  /*
1817  All remaining nodes that are stored in the stack [[fullNodes]] are
1818  introduced as children of the new $P$-node [[newNode]]. Observe
1819  that the children of a $P$-node must form a doubly linked list.
1820  Hence the last node and the [[firstSon]] must be linked via their
1821  siblings pointers.
1822  */
1823  while (!fullNodes->empty())
1824  {
1825  PQNode<T,X,Y> *checkSon = fullNodes->popFrontRet();
1826  removeChildFromSiblings(checkSon);
1827  newNode->fullChildren->pushFront(checkSon);
1828  oldSon->m_sibRight = checkSon;
1829  checkSon->m_sibLeft = oldSon;
1830  checkSon->m_parent = newNode;
1831  checkSon->m_parentType = newNode->type();
1832  oldSon = checkSon;
1833  }
1834  firstSon->m_sibLeft = oldSon;
1835  oldSon->m_sibRight = firstSon;
1836  newNode->m_referenceChild = firstSon;
1837  firstSon->m_referenceParent = newNode;
1838  }
1839 
1840  return newNode;
1841 }
1842 
1843 template<class T,class X,class Y>
1845 {
1846  while(!m_pertinentNodes->empty())
1847  {
1848  PQNode<T, X, Y> *nodePtr = m_pertinentNodes->popFrontRet();
1849  switch (nodePtr->status())
1850  {
1852  if (nodePtr == m_root)
1853  m_root = nullptr;
1854  CleanNode(nodePtr);
1855  OGDF_ASSERT(nodePtr);
1856  delete nodePtr;
1857  break;
1858 
1860  emptyNode(nodePtr);
1861  break;
1862 
1864  emptyNode(nodePtr);
1865  break;
1866 
1867  default:
1868  clientDefinedEmptyNode(nodePtr);
1869  break;
1870  }
1871  }
1872  m_pseudoRoot->m_pertChildCount = 0;
1873  m_pseudoRoot->m_pertLeafCount = 0;
1874  m_pseudoRoot->fullChildren->clear();
1875  m_pseudoRoot->partialChildren->clear();
1878 }
1879 
1880 template<class T,class X,class Y>
1882 {
1884  nodePtr->m_pertChildCount = 0;
1885  nodePtr->m_pertLeafCount = 0;
1886  nodePtr->fullChildren->clear();
1887  nodePtr->partialChildren->clear();
1889 }
1890 
1891 template<class T,class X,class Y>
1893  PQNode<T,X,Y> *oldNode,
1894  PQNode<T,X,Y> *newNode)
1895 {
1896  if (oldNode->m_referenceParent != nullptr)
1897  {
1898  /*
1899  The node [[oldNode]] is connected to its parent
1900  via the reference pointer of the doubly linked list. The [[newNode]]
1901  will be the new reference child and is linked via the reference
1902  pointers to the $P$-node.
1903  */
1904  oldNode->m_referenceParent->m_referenceChild = newNode;
1905  newNode->m_referenceParent = oldNode->m_referenceParent;
1906  oldNode->m_referenceParent = nullptr;
1907  }
1908 
1909  else if (oldNode->endmostChild())
1910  {
1911  /*
1912  The [[oldNode]] is endmost child of a Q-node. So its parent
1913  contains an extra pointer to [[oldNode]]. Link the parent of
1914  [[oldNode]] to [[newNode]] via this pointer and make [[newNode]]
1915  endmost child of its new parent.
1916  */
1917  if (oldNode->m_parent->m_leftEndmost == oldNode)
1918  oldNode->m_parent->m_leftEndmost = newNode;
1919  else if (oldNode->m_parent->m_rightEndmost == oldNode)
1920  oldNode->m_parent->m_rightEndmost = newNode;
1921  }
1922 
1923  if ((oldNode->m_sibLeft == oldNode) && (oldNode->m_sibRight == oldNode))
1924  {
1925  /*
1926  Two possible cases are occured.
1927  \begin{enumerate}
1928  \item [[oldNode]] is the only child of a $P$-node. In order to
1929  implement the doubly linked list of children of the $P$-node, the sibling
1930  pointers of [[newNode]] are set to [[newNode]].
1931  \item [[oldNode]] is the [[m_root]] of the $PQ$-tree. Since
1932  by our definition of the $PQ$-tree the sibling pointers of the
1933  [[m_root]] point to the root itself, (i.e. to make sure that
1934  checking for the endmost child property is also valid for the root)
1935  the sibling pointers of [[newNode]] are set to [[newNode]] as well.
1936  \end{enumerate}
1937  */
1938  oldNode->m_sibLeft = nullptr;
1939  oldNode->m_sibRight = nullptr;
1940  if (oldNode->m_parent == nullptr)
1941  {
1942  newNode->m_sibLeft = newNode;
1943  newNode->m_sibRight = newNode;
1944  }
1945  else
1946  {
1947  newNode->m_sibLeft = newNode;
1948  newNode->m_sibRight = newNode;
1949  }
1950  }
1951  else
1952  {
1953  OGDF_ASSERT(!(oldNode->m_sibLeft == oldNode));
1954  //sibling pointers of old node are not compatible
1955  OGDF_ASSERT(!(oldNode->m_sibRight == oldNode));
1956  //sibling pointers of old node are not compatible.
1957  }
1958  /*
1959  Manage the exchange of [[oldNode]] and [[newNode]] according to
1960  [[oldNode]]'s siblings. The chunk checks both siblings of
1961  [[oldNode]] and resets the sibling pointers of [[oldNode]]'s siblings
1962  as well as the sibling pointers of [[newNode]].
1963  */
1964  if (oldNode->m_sibLeft != nullptr)
1965  {
1966  if (oldNode->m_sibLeft->m_sibRight == oldNode)
1967  oldNode->m_sibLeft->m_sibRight = newNode;
1968  else {
1969  // Sibling was not connected to child?
1970  OGDF_ASSERT(oldNode->m_sibLeft->m_sibLeft == oldNode);
1971  oldNode->m_sibLeft->m_sibLeft = newNode;
1972  }
1973  newNode->m_sibLeft = oldNode->m_sibLeft;
1974  oldNode->m_sibLeft = nullptr;
1975  }
1976 
1977  if (oldNode->m_sibRight != nullptr)
1978  {
1979  if (oldNode->m_sibRight->m_sibLeft == oldNode)
1980  oldNode->m_sibRight->m_sibLeft = newNode;
1981  else {
1982  // Sibling was not connected to child?
1983  OGDF_ASSERT(oldNode->m_sibRight->m_sibRight == oldNode);
1984  oldNode->m_sibRight->m_sibRight = newNode;
1985  }
1986  newNode->m_sibRight = oldNode->m_sibRight;
1987  oldNode->m_sibRight = nullptr;
1988  }
1989 
1990  newNode->m_parentType = oldNode->m_parentType;
1991  newNode->m_parent = oldNode->m_parent;
1992 }
1993 
1994 template<class T,class X,class Y>
1996  PQNode<T,X,Y>* nodePtr,
1997  SListPure<PQLeafKey<T,X,Y>*> &leafKeys)
1998 {
1999  Queue<PQNode<T,X,Y>*> helpqueue;
2000  helpqueue.append(nodePtr);
2001 
2002  while (!helpqueue.empty())
2003  {
2004  PQNode<T,X,Y>* checkNode = helpqueue.pop();
2005 
2006  if (checkNode->type() == PQNodeRoot::PQNodeType::Leaf)
2007  leafKeys.pushBack(checkNode->getKey());
2008  else
2009  {
2010  PQNode<T,X,Y>* firstSon = nullptr;
2011  PQNode<T,X,Y>* nextSon = nullptr;
2012  PQNode<T,X,Y>* oldSib = nullptr;
2013  PQNode<T,X,Y>* holdSib = nullptr;
2014 
2015  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
2016  {
2017  OGDF_ASSERT(checkNode->m_referenceChild);
2018  firstSon = checkNode->m_referenceChild;
2019  }
2020  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
2021  {
2022  OGDF_ASSERT(checkNode->m_leftEndmost);
2023  firstSon = checkNode->m_leftEndmost;
2024  }
2025  helpqueue.append(firstSon);
2026  nextSon = firstSon->getNextSib(oldSib);
2027  oldSib = firstSon;
2028  while (nextSon && nextSon != firstSon)
2029  {
2030  helpqueue.append(nextSon);
2031  holdSib = nextSon->getNextSib(oldSib);
2032  oldSib = nextSon;
2033  nextSon = holdSib;
2034  }
2035  }
2036  }
2037 }
2038 
2039 template<class T,class X,class Y>
2041 {
2043 
2044  if (!leafKeys.empty())
2045  {
2047  m_pseudoRoot = newNode2;
2048 
2049  if (leafKeys.begin() != leafKeys.end()) // at least two elements
2050  {
2052  m_root = newNode;
2053  m_root->m_sibLeft = m_root;
2054  m_root->m_sibRight = m_root;
2055  return addNewLeavesToTree(newNode,leafKeys);
2056  }
2058  m_root = newLeaf;
2059  m_root->m_sibLeft = m_root;
2060  m_root->m_sibRight = m_root;
2061  return 1;
2062  }
2063 
2064  return 0;
2065 }
2066 
2067 template<class T,class X,class Y>
2069  PQNode<T,X,Y> *installed,
2070  PQNode<T,X,Y> *newChild)
2071 {
2072  if ((installed != nullptr) && (newChild != nullptr))
2073  {
2074  if (installed->m_sibLeft == nullptr)
2075  {
2076  installed->m_sibLeft = newChild;
2077  if (newChild->m_sibRight == nullptr)
2078  newChild->m_sibRight = installed;
2079  else
2080  newChild->m_sibLeft = installed;
2081  }
2082  else {
2083  // endmost child with 2 siblings encountered?
2084  OGDF_ASSERT(installed->m_sibRight == nullptr);
2085 
2086  installed->m_sibRight = newChild;
2087  if (newChild->m_sibLeft == nullptr)
2088  newChild->m_sibLeft = installed;
2089  else
2090  newChild->m_sibRight = installed;
2091  }
2092  }
2093 }
2094 
2095 template<class T,class X,class Y>
2096 void PQTree<T,X,Y>::writeGML(const char *fileName)
2097 {
2098  ofstream os(fileName);
2099  writeGML(os);
2100 }
2101 
2102 template<class T,class X,class Y>
2103 void PQTree<T,X,Y>::writeGML(ostream &os)
2104 {
2106  int nextId = 0;
2107 
2108  SListPure<PQNode<T,X,Y>*> helpQueue;
2109  SListPure<PQNode<T,X,Y>*> secondTrace;
2110 
2111  os.setf(ios::showpoint);
2112  os.precision(10);
2113 
2114  os << "Creator \"ogdf::PQTree::writeGML\"\n";
2115  os << "graph [\n";
2116  os << " directed 1\n";
2117 
2118  PQNode<T,X,Y>* checkNode = m_root;
2119  PQNode<T,X,Y>* firstSon = 0;
2120  PQNode<T,X,Y>* nextSon = 0;
2121  PQNode<T,X,Y>* lastSon = 0;
2122  PQNode<T,X,Y>* oldSib = 0;
2123  PQNode<T,X,Y>* holdSib = 0;
2124 
2125  if (checkNode->type() != PQNodeRoot::PQNodeType::Leaf)
2126  secondTrace.pushBack(checkNode);
2127 
2128  while (checkNode)
2129  {
2130  os << " node [\n";
2131 
2132  os << " id " << (id[checkNode->m_identificationNumber] = nextId++) << "\n";
2133 
2134  os << " label \"" << checkNode->m_identificationNumber;
2135  if (checkNode->getKey() != 0)
2136  checkNode->getKey()->print(os);
2137  os << "\"\n";
2138 
2139  os << " graphics [\n";
2140  if (m_root->status() == PQNodeRoot::PQNodeStatus::Full)
2141  {
2142  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
2143  os << " fill \"#FF0000\"\n";
2144  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
2145  os << " fill \"#0000A0\"\n";
2146  else if (checkNode->type() == PQNodeRoot::PQNodeType::Leaf)
2147  os << "fill \"#FFFFE6\"\n";
2148  }
2149  else if (m_root->status() == PQNodeRoot::PQNodeStatus::Empty)
2150  {
2151  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
2152  os << " fill \"#FF0000\"\n";
2153  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
2154  os << " fill \"#0000A0\"\n";
2155  else if (checkNode->type() == PQNodeRoot::PQNodeType::Leaf)
2156  os << " fill \"#00FF00\"\n";
2157  }
2158  else if (m_root->status() == PQNodeRoot::PQNodeStatus::Partial)
2159  {
2160  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
2161  os << " fill \"#FF0000\"\n";
2162  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
2163  os << " fill \"#0000A0\"\n";
2164  else if (checkNode->type() == PQNodeRoot::PQNodeType::Leaf)
2165  os << " fill \"#FFFFE6\"\n";
2166  }
2167  else if (m_root->status() == PQNodeRoot::PQNodeStatus::Pertinent)
2168  {
2169  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
2170  os << " fill \"#FF0000\"\n";
2171  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
2172  os << " fill \"#0000A0\"\n";
2173  else if (checkNode->type() == PQNodeRoot::PQNodeType::Leaf)
2174  os << " fill \"#FFFFE6\"\n";
2175  }
2176 
2177  os << " ]\n"; // graphics
2178  os << " ]\n"; // node
2179 
2180  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
2181  {
2182  if (checkNode->m_referenceChild != 0)
2183  {
2184  firstSon = checkNode->m_referenceChild;
2185  helpQueue.pushBack(firstSon);
2186 
2187  if (firstSon->m_sibRight != 0)
2188  nextSon = firstSon->m_sibRight;
2189  while (nextSon != firstSon)
2190  {
2191  helpQueue.pushBack(nextSon);
2192  nextSon = nextSon->m_sibRight;
2193  }
2194  }
2195  }
2196  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
2197  {
2198  oldSib = 0;
2199  holdSib = 0;
2200 
2201  firstSon = checkNode->m_leftEndmost;
2202  helpQueue.pushBack(firstSon);
2203 
2204  lastSon = checkNode->m_rightEndmost;
2205  if ( firstSon != lastSon)
2206  {
2207  helpQueue.pushBack(lastSon);
2208  nextSon = lastSon->getNextSib(oldSib);
2209  oldSib = lastSon;
2210  while (nextSon != firstSon)
2211  {
2212  helpQueue.pushBack(nextSon);
2213  holdSib = nextSon->getNextSib(oldSib);
2214  oldSib = nextSon;
2215  nextSon = holdSib;
2216  }
2217  }
2218  }
2219  if (!helpQueue.empty())
2220  {
2221  checkNode = helpQueue.popFrontRet();
2222  if (checkNode->type() != PQNodeRoot::PQNodeType::Leaf)
2223  secondTrace.pushBack(checkNode);
2224  }
2225  else
2226  checkNode = 0;
2227  }
2228 
2229 
2231 
2232  for (it = secondTrace.begin(); it.valid(); ++it)
2233  {
2234  checkNode = *it;
2235  if (checkNode->type() == PQNodeRoot::PQNodeType::PNode)
2236  {
2237  if (checkNode->m_referenceChild != 0)
2238  {
2239  firstSon = checkNode->m_referenceChild;
2240  os << " edge [\n";
2241  os << " source " << id[checkNode->m_identificationNumber] << "\n";
2242  os << " target " << id[firstSon->m_identificationNumber] << "\n";
2243  os << " ]\n"; // edge
2244 
2245  if (firstSon->m_sibRight != 0)
2246  nextSon = firstSon->m_sibRight;
2247  while (nextSon != firstSon)
2248  {
2249  os << " edge [\n";
2250  os << " source " << id[checkNode->m_identificationNumber] << "\n";
2251  os << " target " << id[nextSon->m_identificationNumber] << "\n";
2252  os << " ]\n"; // edge
2253  nextSon = nextSon->m_sibRight;
2254  }
2255  }
2256  }
2257  else if (checkNode->type() == PQNodeRoot::PQNodeType::QNode)
2258  {
2259  oldSib = 0;
2260  holdSib = 0;
2261 
2262  firstSon = checkNode->m_leftEndmost;
2263  lastSon = checkNode->m_rightEndmost;
2264 
2265  os << " edge [\n";
2266  os << " source " << id[checkNode->m_identificationNumber] << "\n";
2267  os << " target " << id[lastSon->m_identificationNumber] << "\n";
2268  os << " ]\n"; // edge
2269  if ( firstSon != lastSon)
2270  {
2271  nextSon = lastSon->getNextSib(oldSib);
2272  os << " edge [\n";
2273  os << " source " << id[checkNode->m_identificationNumber] << "\n";
2274  os << " target " << id[nextSon->m_identificationNumber] << "\n";
2275  os << " ]\n"; // edge
2276 
2277  oldSib = lastSon;
2278  while (nextSon != firstSon)
2279  {
2280  holdSib = nextSon->getNextSib(oldSib);
2281  oldSib = nextSon;
2282  nextSon = holdSib;
2283  os << " edge [\n";
2284  os << " source " << id[checkNode->m_identificationNumber] << "\n";
2285  os << " target " << id[nextSon->m_identificationNumber] << "\n";
2286  os << " ]\n"; // edge
2287  }
2288  }
2289  }
2290  }
2291  os << "]\n"; // graph
2292 }
2293 
2294 template<class T,class X,class Y>
2296 {
2297  /* Variables:
2298  * - \a checkLeaf is a pointer to a various PQLeaf of the set of
2299  * elements that has to be reduced.
2300  * - \a checkNode is a pointer to a various node of the pertinent
2301  * subtree.
2302  * - \a pertLeafCount counts the number of pertinent leaves in the
2303  * PQ-tree. Since Reduce() takes care that every node knows the
2304  * number of pertinent leaves in its frontier, the root of the
2305  * pertinent subtree can be identified with the help of \a pertLeafCount.
2306  * - \a processNodes is a queue storing nodes of the pertinent
2307  * subtree that are considered to be reduced next. A node may be
2308  * reduced (and therefore is pushed on to \a processNodes) as soon as
2309  * all its pertinent children have been reduced.
2310  */
2311  int pertLeafCount = 0;
2312  Queue<PQNode<T,X,Y>*> processNodes;
2313 
2314  /*
2315  * In a first step the pertinent leaves have to be identified in the tree
2316  * and are entered on to the queue [[processNodes]]. The identification of
2317  * the leaves can be done with the help of a pointer stored in every
2318  * [[PQLeafKey]] in constant time for every element.
2319  */
2321  for (it = leafKeys.begin(); it.valid(); ++it)
2322  {
2323  PQNode<T,X,Y>* checkLeaf = (*it)->nodePointer();
2325  checkLeaf->m_pertLeafCount = 1;
2326  processNodes.append(checkLeaf);
2327  pertLeafCount++;
2328  }
2329 
2330  PQNode<T,X,Y>* checkNode = processNodes.top();
2331  while ((checkNode != nullptr) && (processNodes.size() > 0))
2332  {
2333  checkNode = processNodes.pop();
2334 
2335  if (checkNode->m_pertLeafCount < pertLeafCount)
2336  {
2337  /*
2338  * Application of the template matchings to a pointer [[checkNode]]
2339  * storing the adress of a node that is <b>not the root</b> of the
2340  * pertinent subtree. Before applying the template matchings to
2341  * [[checkNode]], some values of the parent of [[checkNode]] are
2342  * updated. The number of the parents pertinent children stored in
2343  * [[pertChildCount]] is count down by one. In case that
2344  * [[checkNode->m_parent->m_pertChildCount == 0]], we know that all
2345  * pertinent children of the parent have been processed. Since the
2346  * parent then is allowed to be processed as well,
2347  * [[checkNode->m_parent]] is stored in the queue [[processNodes]].
2348  */
2349  checkNode->m_parent->m_pertLeafCount =
2350  checkNode->m_parent->m_pertLeafCount
2351  + checkNode->m_pertLeafCount;
2352 
2353  checkNode->m_parent->m_pertChildCount--;
2354  if (!checkNode->m_parent->m_pertChildCount)
2355  processNodes.append(checkNode->m_parent);
2356  if (!templateL1(checkNode, 0)
2357  && !templateP1(checkNode, 0)
2358  && !templateP3(checkNode)
2359  && !templateP5(checkNode)
2360  && !templateQ1(checkNode, 0)
2361  && !templateQ2(checkNode, 0)) {
2362  checkNode= nullptr;
2363  }
2364  }
2365  else
2366  {
2367  /*
2368  * application of the template matchings to a pointer [[checkNode]]
2369  * that stores the adress of a node that <b>is the root</b> of the
2370  * pertinent subtree. In a case that a template matching was
2371  * successfully applied, the pointer [[checkNode]] stores after the
2372  * application the adress of the root of pertinent subtree. This
2373  * includes nodes that have been newly introduced as root of the
2374  * pertinent subtree during the application. If no template matching
2375  * was successfully applied [[checkNode]] is a [[0]] pointer.
2376  */
2377  if (!templateL1(checkNode, 1)
2378  && !templateP1(checkNode, 1)
2379  && !templateP2(&checkNode)
2380  && !templateP4(&checkNode)
2381  && !templateP6(&checkNode)
2382  && !templateQ1(checkNode, 1)
2383  && !templateQ2(checkNode, 1)
2384  && !templateQ3(checkNode)) {
2385  checkNode = nullptr;
2386  }
2387  }
2388  }
2389 
2390  m_pertinentRoot = checkNode;
2391  return m_pertinentRoot != nullptr;
2392 }
2393 
2394 template<class T,class X,class Y>
2396 {
2397  /* Reduction() calls the procedure Bubble() and if Bubble() was
2398  * successful, Reduction() calls the function Reduce().
2399  */
2400  bool success = Bubble(leafKeys);
2401 
2402  if (!success)
2403  return false;
2404  else
2405  return Reduce(leafKeys);
2406 }
2407 
2408 template<class T,class X,class Y>
2409 void PQTree<T,X,Y>::removeBlock(PQNode<T,X,Y> *nodePtr,bool isRoot)
2410 {
2411  /*
2412  For every
2413  partial child we keep a set of pointers. Since there are at most
2414  two partial children, we initialize two sets. Every set contains
2415  besides a pointer to the partial child four pointers to its endmost
2416  children, sorted according to their full or empty labels and pointers
2417  to the immediate siblings of the partial child, also sorted according
2418  to their full or empty labels.
2419  */
2421  PQNode<T,X,Y> *partial_1 = nullptr;
2422 
2423  /*
2424  Pointer to the full endmost child (more
2425  precisely: to the endmost child appearing on the full side) of
2426  [[partial_1]]. In case that ignored nodes are used, this [[endfull_1]]
2427  may store the adress of an ignored node.
2428  */
2429  PQNode<T,X,Y> *endfull_1 = nullptr;
2430 
2431  /*
2432  Pointer to the empty endmost child (more
2433  precisely: to the endmost child appearing on the empty side) of
2434  [[partial_1]]. In case that ignored nodes are used, this [[endempty_1]]
2435  may store the adress of an ignored node.
2436  */
2437  PQNode<T,X,Y> *endempty_1 = nullptr;
2438 
2439  /*
2440  Pointer to the first <i>non ignored</i> node
2441  with full status. [[realfull_1]] is identical to [[endfull_1]] if no
2442  ignored nodes appear at the full end of the first partial child.
2443  */
2444  PQNode<T,X,Y> *realfull_1 = nullptr;
2445 
2446  /*
2447  Pointer to the first <i>non ignored</i> node
2448  with empty status. [[realempty_1]] is identical to [[endempty_1]] if no
2449  ignored nodes appear at the empty end of the first partial child.
2450  */
2451  PQNode<T,X,Y> *realempty_1 = nullptr;
2452 
2453  // Pointer to the second partial child.
2454  PQNode<T,X,Y> *partial_2 = nullptr;
2455 
2456  /*
2457  Pointer to the full endmost child (more
2458  precisely: to the endmost child appearing on the full side) of
2459  [[partial_2]]. In case that ignored nodes are used, this [[endfull_2]]
2460  may store the adress of an ignored node.
2461  */
2462  PQNode<T,X,Y> *endfull_2 = nullptr;
2463 
2464  /*
2465  Pointer to the empty endmost child (more
2466  precisely: to the endmost child appearing on the empty side) of
2467  [[partial_2]]. In case that ignored nodes are used, this [[endempty_2]]
2468  may store the adress of an ignored node.
2469  */
2470  PQNode<T,X,Y> *endempty_2 = nullptr;
2471 
2472  /*
2473  Pointer to the first <i>non ignored</i> node
2474  with empty status. [[realempty_2]] is identical to [[endempty_2]] if no
2475  ignored nodes appear at the empty end of the first partial child.
2476  */
2477  PQNode<T,X,Y> *realempty_2 = nullptr;
2478 
2479  /*
2480  Pointer to a full sibling of
2481  [[partial_1]], if it exists. In case that ignored nodes are used
2482  this [[sibfull_1]] stores the direct sibling of [[partial_1]]
2483  on the side where the full siblings are. Hence [[sibfull_1]] may
2484  store an ignored node.
2485  */
2486  PQNode<T,X,Y> *sibfull_1 = nullptr;
2487 
2488  /*
2489  Pointer to a partial sibling of
2490  [[partial_1]], if it exists. In case that ignored nodes are used
2491  this [[sibpartial_1]] stores the direct sibling of [[partial_1]]
2492  on the side where a partial sibling is. Hence [[sibpartial_1]] may
2493  store an ignored node.
2494  */
2495  PQNode<T,X,Y> *sibpartial_1 = nullptr;
2496 
2497  /*
2498  Pointer to an empty sibling of
2499  [[parial_1]], if it exists. In case that ignored nodes are used
2500  this [[sibempty_1]] stores the direct sibling of [[partial_1]]
2501  on the side where the empty siblings are. Hence [[sibempty_1]] may
2502  store an ignored node.
2503  */
2504  PQNode<T,X,Y> *sibempty_1 = nullptr;
2505 
2506  /*
2507  Pointer only used in case that [[partial_1]] has
2508  no non-ignored siblings on one side. [[partial_1]] then is endmost child
2509  of [[nodePtr]], but ignored children may appear between [[partial_1]]
2510  and the end of sequence of children of [[nodePtr]]. The
2511  [[nonstatussib_1]] then stores the adress of the endmost ignored child.
2512  Observe that it is not valid for a $Q$-node to have only one
2513  non-ignored child and several ignored children. Hence this situation
2514  is only allowed to appear <b>once</b> on one side of [[partial_1]].
2515  Every other situation results in an error message.
2516  */
2517  PQNode<T,X,Y> *nonstatussib_1 = nullptr;
2518 
2519  /*
2520  Pointer to a full sibling of
2521  [[partial_2]], if it exists. In case that ignored nodes are used
2522  this [[sibfull_2]] stores the direct sibling of [[partial_2]]
2523  on the side where the full siblings are. Hence [[sibfull_2]] may
2524  store an ignored node.
2525  */
2526  PQNode<T,X,Y> *sibfull_2 = nullptr;
2527 
2528  /*
2529  Pointer to a partial sibling of
2530  [[partial_2]], if it exists. In case that ignored nodes are used
2531  this [[sibpartial_2]] stores the direct sibling of [[partial_2]]
2532  on the side where a partial sibling is. Hence [[sibpartial_2]] may
2533  store an ignored node.
2534  */
2535  PQNode<T,X,Y> *sibpartial_2 = nullptr;
2536 
2537  /*
2538  Pointer to an empty sibling of
2539  [[parial_2]], if it exists. In case that ignored nodes are used
2540  this [[sibempty_2]] stores the direct sibling of [[partial_2]]
2541  on the side where the empty siblings are. Hence [[sibempty_2]] may
2542  store an ignored node.
2543  */
2544  PQNode<T,X,Y> *sibempty_2 = nullptr;
2545 
2546  /*
2547  Pointer only used in case that [[partial_2]] has
2548  no non-ignored siblings on one side. [[partial_2]] then is endmost child
2549  of [[nodePtr]], but ignored children may appear between [[partial_2]]
2550  and the end of sequence of children of [[nodePtr]]. The
2551  [[nonstatussib_2]] then stores the adress of the endmost ignored child.
2552  Observe that it is not valid for a $Q$-node to have only one
2553  non-ignored child and several ignored children. Hence this situation
2554  is only allowed to appear <b>once</b> on one side of [[partial_2]].
2555  Every other situation results in an error message.
2556  */
2557  PQNode<T,X,Y> *nonstatussib_2 = nullptr;
2558 
2559  PQNode<T,X,Y> *helpptr = nullptr;
2560 
2561  PQNode<T,X,Y> *checkVarLeft = nullptr;
2562 
2563  PQNode<T,X,Y> *checkVarRight = nullptr;
2564 
2565  /*
2566  [[endmostcheck]] is [[1]], if [[partial_1]] is the endmost
2567  child of [[nodePtr]]. Per default, [[endmostcheck]] is [[0]].
2568  */
2569  int endmostcheck = 0;
2570 
2571 
2573  if (!isRoot)
2574  nodePtr->m_parent->partialChildren->pushFront(nodePtr);
2575 
2576  if (!nodePtr->partialChildren->empty())
2577  // Get a partial child.
2578  {
2579  partial_1 = nodePtr->partialChildren->popFrontRet();
2580 
2581  // Get the full and empty
2582  // endmost children of the
2583  // partial child [[partial_1]].
2584  checkVarLeft = clientLeftEndmost(partial_1);
2585  checkVarRight = clientRightEndmost(partial_1);
2586  if (checkVarLeft->status() == PQNodeRoot::PQNodeStatus::Full)
2587  {
2588  endfull_1 = partial_1->m_leftEndmost;
2589  realfull_1 = checkVarLeft;
2590  }
2591  else {
2592  // partial child with no full endmost child detected?
2593  OGDF_ASSERT(checkVarRight->status() == PQNodeRoot::PQNodeStatus::Full);
2594 
2595  endfull_1 = partial_1->m_rightEndmost;
2596  realfull_1 = checkVarRight;
2597  }
2598 
2599  if (checkVarLeft->status() == PQNodeRoot::PQNodeStatus::Empty)
2600  {
2601  endempty_1 = partial_1->m_leftEndmost;
2602  realempty_1 = checkVarLeft;
2603  }
2604  else {
2605  // partial child with no empty endmost child detected?
2606  OGDF_ASSERT(checkVarRight->status() == PQNodeRoot::PQNodeStatus::Empty);
2607 
2608  endempty_1 = partial_1->m_rightEndmost;
2609  realempty_1 = checkVarRight;
2610  }
2611 
2612  // Get the immediate
2613  // siblings of the partial
2614  // child [[partial_1]].
2615  if (clientSibLeft(partial_1) != nullptr)
2616  {
2617  if (clientSibLeft(partial_1)->status() == PQNodeRoot::PQNodeStatus::Full)
2618  sibfull_1 = partial_1->m_sibLeft;
2619  else if (clientSibLeft(partial_1)->status() == PQNodeRoot::PQNodeStatus::Empty)
2620  sibempty_1 = partial_1->m_sibLeft;
2621  else if (clientSibLeft(partial_1)->status() == PQNodeRoot::PQNodeStatus::Partial)
2622  sibpartial_1 = partial_1->m_sibLeft;
2623  }
2624  else
2625  nonstatussib_1 = partial_1->m_sibLeft;
2626 
2627  if (clientSibRight(partial_1) != nullptr)
2628  {
2629  if (clientSibRight(partial_1)->status() == PQNodeRoot::PQNodeStatus::Full)
2630  sibfull_1 = partial_1->m_sibRight;
2631  else if (clientSibRight(partial_1)->status() == PQNodeRoot::PQNodeStatus::Empty)
2632  sibempty_1 = partial_1->m_sibRight;
2633  else if (clientSibRight(partial_1)->status() == PQNodeRoot::PQNodeStatus::Partial)
2634  sibpartial_1 = partial_1->m_sibRight;
2635  }
2636  else {
2637  // partial child detected with no siblings of valid status?
2638  OGDF_ASSERT(nonstatussib_1 == nullptr);
2639  nonstatussib_1 = partial_1->m_sibRight;
2640  }
2641  }
2642 
2643 
2644  if (!nodePtr->partialChildren->empty())
2645  // There is a second partial child.
2646  {
2647  partial_2 = nodePtr->partialChildren->popFrontRet();
2648  // Get the full and empty endmost
2649  // children of the partial
2650  // child [[partial_2]].
2651 
2652  checkVarLeft = clientLeftEndmost(partial_2);
2653  checkVarRight = clientRightEndmost(partial_2);
2654  if (checkVarLeft->status() == PQNodeRoot::PQNodeStatus::Full)
2655  {
2656  endfull_2 = partial_2->m_leftEndmost;
2657  }
2658  else {
2659  // partial child with no full endmost child detected?
2660  OGDF_ASSERT(checkVarRight->status() == PQNodeRoot::PQNodeStatus::Full);
2661 
2662  endfull_2 = partial_2->m_rightEndmost;
2663  }
2664 
2665  if (checkVarLeft->status() == PQNodeRoot::PQNodeStatus::Empty)
2666  {
2667  endempty_2 = partial_2->m_leftEndmost;
2668  realempty_2 = checkVarLeft;
2669  }
2670  else {
2671  // partial child with no empty endmost child detected?
2672  OGDF_ASSERT(checkVarRight->status() == PQNodeRoot::PQNodeStatus::Empty);
2673 
2674  endempty_2 = partial_2->m_rightEndmost;
2675  realempty_2 = checkVarRight;
2676  }
2677  // Get the immediate siblings
2678  // of the partial child
2679  // [[partial_2]].
2680  if (clientSibLeft(partial_2) != nullptr)
2681  {
2682  if (clientSibLeft(partial_2)->status() == PQNodeRoot::PQNodeStatus::Full)
2683  sibfull_2 = partial_2->m_sibLeft;
2684  else if (clientSibLeft(partial_2)->status() == PQNodeRoot::PQNodeStatus::Empty)
2685  sibempty_2 = partial_2->m_sibLeft;
2686  else if (clientSibLeft(partial_2)->status() == PQNodeRoot::PQNodeStatus::Partial)
2687  sibpartial_2 = partial_2->m_sibLeft;
2688  }
2689  else
2690  nonstatussib_2 = partial_2->m_sibLeft;
2691 
2692 
2693  if (clientSibRight(partial_2) != nullptr)
2694  {
2695  if (clientSibRight(partial_2)->status() == PQNodeRoot::PQNodeStatus::Full)
2696  sibfull_2 = partial_2->m_sibRight;
2697  else if (clientSibRight(partial_2)->status() == PQNodeRoot::PQNodeStatus::Empty)
2698  sibempty_2 = partial_2->m_sibRight;
2699  else if (clientSibRight(partial_2)->status() == PQNodeRoot::PQNodeStatus::Partial)
2700  sibpartial_2 = partial_2->m_sibRight;
2701  }
2702  else {
2703  OGDF_ASSERT(nonstatussib_2 == nullptr);
2704  nonstatussib_2 = partial_2->m_sibRight;
2705  }
2706  }
2707 
2708 
2709  if (partial_1 != nullptr && partial_2 != nullptr)
2710 
2711  /*
2712  Connect the endmost
2713  children of the partial children [[partial_1]] and [[partial_2]] correctly
2714  with their new siblings. In doing this, all children of the partial
2715  children become children of [[nodePtr]]. The reader should observe that
2716  the parent pointers of the interior children of [[partial_1]] and
2717  [[partial_2]] are not updated in order to hit the linear time complexity.
2718 
2719  When including the children of the partial children to the children
2720  of [[nodePtr]], it is taken care that all full children
2721  form a consecutive sequence afterwards. If neccessary the pointers to the
2722  endmost children of [[nodePtr]] are updated.
2723  */
2724  {
2725  if (sibfull_1 != nullptr && sibfull_2 != nullptr)
2726  // There are full children between
2727  // the 2 partial nodes.
2728  // Connect the full children
2729  // between the 2 partial children
2730  // with the full endmost children
2731  // of the 2 partial nodes.
2732  {
2733  sibfull_1->changeSiblings(partial_1,endfull_1);
2734  endfull_1->putSibling(sibfull_1);
2735  sibfull_2->changeSiblings(partial_2,endfull_2);
2736  endfull_2->putSibling(sibfull_2);
2737  }
2738 
2739  else if (sibpartial_1 != nullptr && sibpartial_2 != nullptr)
2740  // There are no full children between
2741  // the 2 partial nodes. Connect the
2742  // full endmost children of the
2743  // partial nodes as siblings.
2744  {
2745  if (partial_1 == sibpartial_2 && partial_2 == sibpartial_1)
2746  // Regular Case.
2747  {
2748  endfull_1->putSibling(endfull_2);
2749  endfull_2->putSibling(endfull_1);
2750  }
2751  // Only ignored children between
2752  // partial_1 and partial_2.
2753  else
2754  {
2755  endfull_1->putSibling(sibpartial_1);
2756  sibpartial_1->changeSiblings(partial_1,endfull_1);
2757  endfull_2->putSibling(sibpartial_2);
2758  sibpartial_2->changeSiblings(partial_2,endfull_2);
2759  }
2760 
2761  }
2762  // Include the children of the
2763  // partial children with their
2764  // full nodes inbetween into
2765  // the sequence of the children of
2766  // Q-node nodePtr.
2767  if (sibempty_1 == nullptr)
2768  // partial_1 is endmost child of
2769  // nodePtr. Make the empty endmost
2770  // child of partial_1 be the new
2771  // endmost child of nodePtr.
2772  {
2773  if (nonstatussib_1 == nullptr)
2774  // Regular case.
2775  {
2776  nodePtr->changeEndmost(partial_1,endempty_1);
2777  }
2778  else
2779  // Only ignored children between
2780  // partial_1 and one end of nodePtr.
2781  {
2782  nonstatussib_1->changeSiblings(partial_1,endempty_1);
2783  endempty_1->putSibling(nonstatussib_1);
2784  }
2785  endempty_1->m_parent = nodePtr;
2786  realempty_1->m_parent = nodePtr;
2787  }
2788 
2789  else
2790  // partial_1 is not endmost child.
2791  {
2792  sibempty_1->changeSiblings(partial_1,endempty_1);
2793  endempty_1->putSibling(sibempty_1);
2794  }
2795 
2796 
2797  if (sibempty_2 == nullptr)
2798  // partial_2 is endmost child of
2799  // nodePtr. Make the empty endmost
2800  // child of partial_2 be the new
2801  // endmost child of nodePtr.
2802  {
2803  if (nonstatussib_2 == nullptr)
2804  // Regular case.
2805  {
2806  nodePtr->changeEndmost(partial_2,endempty_2);
2807  }
2808  else
2809  // Only ignored children between
2810  // partial_1 and one end of
2811  // nodePtr.
2812  {
2813  nonstatussib_2->changeSiblings(partial_2,endempty_2);
2814  endempty_2->putSibling(nonstatussib_2);
2815  }
2816  endempty_2->m_parent = nodePtr;
2817  realempty_2->m_parent = nodePtr;
2818  }
2819 
2820  else
2821  // partial_2 is not endmost child.
2822  {
2823  sibempty_2->changeSiblings(partial_2,endempty_2);
2824  endempty_2->putSibling(sibempty_2);
2825  }
2826 
2827  // Copy the full children of
2828  // partial_1 and partial_2 to
2829  // nodePtr.
2830  while (!partial_2->fullChildren->empty())
2831  {
2832  helpptr = partial_2->fullChildren->popFrontRet();
2833  nodePtr->fullChildren->pushFront(helpptr);
2834  }
2835  nodePtr->m_childCount = nodePtr->m_childCount +partial_2->m_childCount - 1;
2836 
2837  destroyNode(partial_2);
2838 
2839  while (!partial_1->fullChildren->empty())
2840  {
2841  helpptr = partial_1->fullChildren->popFrontRet();
2842  nodePtr->fullChildren->pushFront(helpptr);
2843  }
2844  nodePtr->m_childCount = nodePtr->m_childCount +partial_1->m_childCount - 1;
2845 
2846  destroyNode(partial_1);
2847  }
2848 
2849 
2850  else if (partial_1 != nullptr)
2851 
2852  /*
2853  Connect the endmost
2854  children of the partial child [[partial_1]] correctly
2855  with their new siblings. In doing this, all children of the partial
2856  child become children of [[nodePtr]]. The reader should observe that
2857  the parent pointers of the interior children of [[partial_1]]
2858  are not updated in order to hit the linear time complexity.
2859 
2860  When including the children of [[partial_1]] to the children
2861  of [[nodePtr]], it is taken care that all full children
2862  form a consecutive sequence afterwards. If necessary the pointers to the
2863  endmost children of [[nodePtr]] are updated.
2864  */
2865  {
2866  if ((clientLeftEndmost(nodePtr) == partial_1) ||
2867  (clientRightEndmost(nodePtr) == partial_1))
2868  // partial_1 is endmost child.
2869  endmostcheck = 1;
2870 
2871  if (sibfull_1 != nullptr)
2872  // There are full children on one
2873  // side of the partial node.
2874  // Connect the full children with
2875  // the full endmost child of
2876  // partial_1.
2877  {
2878  sibfull_1->changeSiblings(partial_1,endfull_1);
2879  endfull_1->putSibling(sibfull_1);
2880  }
2881 
2882  else if (!endmostcheck)
2883  // There are not any full children
2884  // and partial_1 is not endmost.
2885  // So get the 2nd empty sibling
2886  // of partial_1 and connect it
2887  // to the full endmost child
2888  // of partial_1.
2889  {
2890  if (partial_1->m_sibLeft != sibempty_1)
2891  sibempty_2 = partial_1->m_sibLeft;
2892  else
2893  sibempty_2 = partial_1->m_sibRight;
2894 
2895  sibempty_2->changeSiblings(partial_1,endfull_1);
2896  endfull_1->putSibling(sibempty_2);
2897  }
2898 
2899  else
2900  // partial_1 is endmost child
2901  // and there are no full children.
2902  // Make the full endmost child of
2903  // partial_1 be the endmostchild
2904  // of nodePtr.
2905  {
2906 
2907  if (nonstatussib_1 == nullptr)
2908  // Regular case.
2909  {
2910  nodePtr->changeEndmost(partial_1,endfull_1);
2911  }
2912  else
2913  // Only ignored children between
2914  // partial_1 and one end of
2915  // nodePtr.
2916  {
2917  nonstatussib_1->changeSiblings(partial_1,endfull_1);
2918  endfull_1->putSibling(nonstatussib_1);
2919  }
2920  endfull_1->m_parent = nodePtr;
2921  realfull_1->m_parent = nodePtr;
2922 
2923  }
2924 
2925  if (sibempty_1 == nullptr)
2926  // There are no empty children.
2927  // partial_1 is endmost child of
2928  // nodePtr. Make the empty endmost
2929  // child of partial_1 be the new
2930  // endmost child of nodePtr.
2931  {
2932  if (nonstatussib_1 == nullptr)
2933  // Regular case.
2934  {
2935  nodePtr->changeEndmost(partial_1,endempty_1);
2936  }
2937  else
2938  // Only ignored children between
2939  // partial_1 and one end of
2940  // nodePtr.
2941  {
2942  nonstatussib_1->changeSiblings(partial_1,endempty_1);
2943  endempty_1->putSibling(nonstatussib_1);
2944  }
2945  endempty_1->m_parent = nodePtr;
2946  realempty_1->m_parent = nodePtr;
2947  }
2948 
2949  else
2950  // There are empty children. So
2951  // connect the empty endmost child
2952  // of partial_1 with sibempty_1.
2953  {
2954  sibempty_1->changeSiblings(partial_1,endempty_1);
2955  endempty_1->putSibling(sibempty_1);
2956  }
2957 
2958  while (!partial_1->fullChildren->empty())
2959  {
2960  helpptr = partial_1->fullChildren->popFrontRet();
2961  nodePtr->fullChildren->pushFront(helpptr);
2962  }
2963 
2964  nodePtr->m_childCount = nodePtr->m_childCount + partial_1->m_childCount - 1;
2965  destroyNode(partial_1);
2966 
2967  }
2968  // else nodePtr does not have partial children. Then nothing is to do.
2969 }
2970 
2971 template<class T,class X,class Y>
2973 {
2974  if (nodePtr->m_referenceParent != nullptr)
2975  {
2976  /*
2977  Checksif [[nodePtr]] is connected with its parent via the reference
2978  pointers (see \ref{PQNode}). If so, the next sibling of [[nodePtr]]
2979  will be the the new reference child.
2980  */
2981  nodePtr->m_referenceParent->m_referenceChild = nodePtr->m_sibRight;
2982  nodePtr->m_sibRight->m_referenceParent = nodePtr->m_referenceParent;
2983  if (nodePtr->m_referenceParent->m_referenceChild == nodePtr)
2984  nodePtr->m_referenceParent->m_referenceChild = nullptr;
2985  nodePtr->m_referenceParent = nullptr;
2986  }
2987  else if (nodePtr->endmostChild())
2988  {
2989  /*
2990  Check if [[nodePtr]] is the endmost child of a $Q$-node.
2991  If so, the next sibling of [[nodePtr]] will be the the new endmost
2992  child of the $Q$-node. Observe that the sibling then gets a valid
2993  pointer to its parent.
2994  */
2995  PQNode<T,X,Y> *sibling = nodePtr->getNextSib(nullptr);
2996  if (nodePtr->m_parent->m_leftEndmost == nodePtr)
2997  nodePtr->m_parent->m_leftEndmost = sibling;
2998  else if (nodePtr->m_parent->m_rightEndmost == nodePtr)
2999  nodePtr->m_parent->m_rightEndmost = sibling;
3000  if (sibling != nullptr)
3001  sibling->m_parent = nodePtr->m_parent;
3002  }
3003 
3004  /*
3005  Remove [[nodePtr]] from its immediate siblings and links the
3006  siblings via the [[sibRight]] and [[sibLeft]] pointers.
3007  */
3008  if ((nodePtr->m_sibRight != nullptr) && (nodePtr->m_sibRight != nodePtr))
3009  {
3010  if (nodePtr->m_sibRight->m_sibLeft == nodePtr)
3011  nodePtr->m_sibRight->m_sibLeft = nodePtr->m_sibLeft;
3012  else {
3013  // Sibling was not connected to child?
3014  OGDF_ASSERT(nodePtr->m_sibRight->m_sibRight == nodePtr);
3015  nodePtr->m_sibRight->m_sibRight = nodePtr->m_sibLeft;
3016  }
3017  }
3018  if ((nodePtr->m_sibLeft != nullptr) && (nodePtr->m_sibLeft != nodePtr))
3019  {
3020  if (nodePtr->m_sibLeft->m_sibRight == nodePtr)
3021  nodePtr->m_sibLeft->m_sibRight = nodePtr->m_sibRight;
3022  else {
3023  // Sibling was not connected to child?
3024  OGDF_ASSERT(nodePtr->m_sibLeft->m_sibLeft == nodePtr);
3025  nodePtr->m_sibLeft->m_sibLeft = nodePtr->m_sibRight;
3026  }
3027  }
3028 
3029  nodePtr->m_sibRight = nullptr;
3030  nodePtr->m_sibLeft = nullptr;
3031 }
3032 
3033 template<class T,class X,class Y>
3035 {
3036  if (parent !=nullptr)
3037  {
3038  removeChildFromSiblings(child);
3039  parent->m_childCount--;
3041  parent->m_pertChildCount--;
3042  return parent->m_childCount;
3043  }
3044  else
3045  {
3046  //parent is invalid 0-pointer.
3047  return -1;
3048  }
3049 }
3050 
3051 template<class T,class X,class Y>
3052 void PQTree<T,X,Y>::sortExceptions(int Exceptions[],int arraySize)
3053 {
3054  bool changed = true;
3055  while (changed)
3056  {
3057  changed = false;
3058  for (int i = 0; i < (arraySize-1); i++)
3059  {
3060  if (Exceptions[i] > Exceptions[i+1])
3061  {
3062  swap(Exceptions[i],Exceptions[i+1]);
3063  changed = true;
3064  }
3065  }
3066  }
3067 }
3068 
3069 template<class T,class X,class Y>
3070 bool PQTree<T,X,Y>::templateL1(PQNode<T,X,Y> *nodePtr, bool isRoot)
3071 {
3072  if ((nodePtr->type() == PQNodeRoot::PQNodeType::Leaf) && (nodePtr->status() == PQNodeRoot::PQNodeStatus::Full))
3073  {
3074  if (!isRoot)
3075  nodePtr->m_parent->fullChildren->pushFront(nodePtr);
3076  return true;
3077  }
3078 
3079  return false;
3080 }
3081 
3082 template<class T,class X,class Y>
3083 bool PQTree<T,X,Y>::templateP1(PQNode<T,X,Y> *nodePtr, bool isRoot)
3084 {
3085  if (nodePtr->type() != PQNodeRoot::PQNodeType::PNode ||
3086  nodePtr->fullChildren->size() != nodePtr->m_childCount)
3087  return false;
3088  else
3089  {
3091  if (!isRoot)
3092  nodePtr->m_parent->fullChildren->pushFront(nodePtr);
3093 
3094  return true;
3095  }
3096 }
3097 
3098 template<class T,class X,class Y>
3100 {
3101  if ((*nodePtr)->type() != PQNodeRoot::PQNodeType::PNode ||
3102  (*nodePtr)->partialChildren->size() > 0)
3103  return false;
3104 
3105  (*nodePtr)->m_childCount =
3106  (*nodePtr)->m_childCount - (*nodePtr)->fullChildren->size() + 1;
3107  // Gather all full children of nodePtr
3108  // as children of the new P-node.
3109  // Delete them from nodePtr.
3110 
3111  PQNode<T,X,Y> *newNode = createNodeAndCopyFullChildren((*nodePtr)->fullChildren);
3112  // Correct parent-pointer and
3113  // sibling-pointers of the new P-node.
3114 
3115  newNode->m_parent = (*nodePtr);
3116  newNode->m_sibRight = (*nodePtr)->m_referenceChild->m_sibRight;
3117  newNode->m_sibLeft = newNode->m_sibRight->m_sibLeft;
3118  newNode->m_sibLeft->m_sibRight = newNode;
3119  newNode->m_sibRight->m_sibLeft = newNode;
3121  // The new P-node now is the root of
3122  // the pertinent subtree.
3123  (*nodePtr) = newNode;
3124 
3125  return true;
3126 }
3127 
3128 template<class T,class X,class Y>
3130 {
3131  /* Variables:
3132  * - \p newPnode is the pointer to the new P-node, or, in case
3133  * that \p nodePtr has only one full child, is the pointer of this child.
3134  * - \p newQnode is the pointer to the new Q-node.
3135  * - \p newNode is used for the proper allocation of the new
3136  * Q-node.
3137  * - \p emptyNode is a pointer to any empty child of \p nodePtr.
3138  */
3139  if (nodePtr->type() != PQNodeRoot::PQNodeType::PNode || nodePtr->partialChildren->size() > 0)
3140  return false;
3141 
3142  /*
3143  Create a new partial $Q$-node stored in [[newQnode]].
3144  It replaces [[nodePtr]] by the Q-node [[newQnode]] in the $PQ$-tree
3145  and makes [[nodePtr]] endmost child of [[newQnode]].
3146  This is done by updating parent-pointers and sibling-pointers.
3147  */
3149  PQNode<T,X,Y> *newQnode = newNode;
3150  m_pertinentNodes->pushFront(newQnode);
3151 
3152  exchangeNodes(nodePtr,newQnode);
3153  nodePtr->m_parent = newQnode;
3155 
3156  newQnode->m_leftEndmost = (nodePtr);
3157  newQnode->m_childCount = 1;
3158 
3159  /*
3160  Create a new full $P$-node stored in [[newPnode]].
3161  It copies the full children of [[nodePtr]] to [[newPnode]].
3162  The new $P$-node will then be included into the tree as child of
3163  the new $Q$-node [[newQnode]].
3164  */
3165  if (nodePtr->fullChildren->size() > 0)
3166  {
3167  nodePtr->m_childCount = nodePtr->m_childCount -
3168  nodePtr->fullChildren->size();
3169 
3172 
3173  // Update newQnode.
3174  newQnode->m_childCount++;
3175  newQnode->fullChildren->pushFront(newPnode);
3176  // Update sibling pointers.
3177  nodePtr->m_sibRight = newPnode;
3178  newPnode->m_sibLeft = nodePtr;
3179  newQnode->m_rightEndmost = newPnode;
3180  newPnode->m_parent = newQnode;
3181  }
3182 
3183  // Check if nodePtr contains
3184  // only one son. If so, nodePtr
3185  // will be deleted from the tree.
3187  checkIfOnlyChild(emptyNode,nodePtr);
3188  // Update partialchildren stack of
3189  // the parent of the new Q-node.
3190  newQnode->m_parent->partialChildren->pushFront(newQnode);
3191 
3192  return true;
3193 }
3194 
3195 template<class T,class X,class Y>
3197 {
3198  if ((*nodePtr)->type() != PQNodeRoot::PQNodeType::PNode ||
3199  (*nodePtr)->partialChildren->size() != 1)
3200  return false;
3201 
3202  PQNode<T,X,Y> *partialChild = (*nodePtr)->partialChildren->popFrontRet();
3203  copyFullChildrenToPartial(*nodePtr,partialChild);
3204  // If nodePtr does not have any
3205  // empty children, then it has to
3206  // be deleted and the partial node
3207  // is occupying its place in the tree.
3208  checkIfOnlyChild(partialChild,*nodePtr);
3209  // The partial child now is
3210  // root of the pertinent subtree.
3211  *nodePtr = partialChild;
3212 
3213  return true;
3214 }
3215 
3216 template<class T,class X,class Y>
3218 {
3219  /* Variables:
3220  * - \a partialChild is a pointer to the partial child of
3221  * \a nodePtr.
3222  * - \a checkNode is a pointer to the endmost empty child of
3223  * \a partialChild.
3224  * - \a emptyNode is a pointer to the empty node that is copied as
3225  * endmost child to \a partialChild.
3226  * - \a emptyChildCount stores the number of empty children of
3227  * \a nodePtr.
3228  *
3229  * If neccessary, the function templateP5() creates a new full P-node
3230  * and copies all full children of \p nodePtr to this new full P-node.
3231  * All empty children of \p nodePtr stay empty children of \p nodePtr.
3232  *
3233  * The new full P-node and \p nodePtr will be the new endmost children of
3234  * \a partialChild. The \a partialChild then occupies the position
3235  * of \p nodePtr in the PQ-tree.
3236  */
3237  if ((nodePtr->type() != PQNodeRoot::PQNodeType::PNode) ||
3238  (nodePtr->partialChildren->size() != 1))
3239  return false;
3240 
3241  /*
3242  Remove [[partialChild]] from the children of [[nodePtr]]. The node
3243  [[partialChild]] then occupies the position of [[nodePtr]] in the
3244  $PQ$-tree which is done in the function call [[exchangeNodes]]
3245  (\ref{exchangeNodes}). The chunk then removes all full children from
3246  [[nodePtr]] and adds them as children of a new $P$-node as endmost
3247  child of [[partialChild]]. This is done in the function call
3248  [[copyFullChildrenToPartial]] (\ref{copyFullChildrenToPartial}).
3249  When this chunk has finished, [[nodePtr]] has only empty children.
3250  */
3251  int emptyChildCount = nodePtr->m_childCount -
3252  nodePtr->fullChildren->size() - 1;
3253  PQNode<T,X,Y> *partialChild = nodePtr->partialChildren->popFrontRet();
3254  nodePtr->m_parent->partialChildren->pushFront(partialChild);
3255  removeChildFromSiblings(partialChild);
3256  exchangeNodes(nodePtr,partialChild);
3257  copyFullChildrenToPartial(nodePtr,partialChild);
3258 
3259  if (emptyChildCount > 0)
3260  {
3261  /*
3262  Check if [[nodePtr]] has just one empty child. If so, the child
3263  is stored in [[emptyNode]] in order to be added to the empty
3264  side of the partial $Q$-node [[partialChild]]. If [[nodePtr]]
3265  has more than one empty child, [[nodePtr]] is stored in
3266  [[emptyNode]] in order to be added to the empty
3267  side of the partial $Q$-node [[partialChild]].
3268  */
3270  if (emptyChildCount == 1)
3271  {
3272  emptyNode = nodePtr->m_referenceChild;
3273  removeChildFromSiblings(emptyNode);
3274 
3275  } else {
3276  emptyNode = nodePtr;
3277  emptyNode->m_childCount = emptyChildCount;
3278  }
3279 
3280  /*
3281  Check at which side of [[partialChild]]
3282  the empty children hide. [[emptyNode]] stores the empty node
3283  that is added to the empty side of [[partialChild]].
3284  */
3285  PQNode<T,X,Y> *checkNode;
3286  if (clientLeftEndmost(partialChild)->status() == PQNodeRoot::PQNodeStatus::Empty)
3287  {
3288  checkNode = partialChild->m_leftEndmost;
3289  partialChild->m_leftEndmost = emptyNode;
3290  }
3291  else {
3292  // Endmostchild not found?
3294 
3295  checkNode = partialChild->m_rightEndmost;
3296  partialChild->m_rightEndmost = emptyNode;
3297  }
3298 
3299  linkChildrenOfQnode(checkNode,emptyNode);
3300  emptyNode->m_parent = partialChild;
3302  partialChild->m_childCount++;
3303  }
3304  // If nodePtr did not have any empty
3305  // children it has to be deleted.
3306  if (emptyChildCount <= 1)
3307  destroyNode(nodePtr);
3308 
3309  return true;
3310 }
3311 
3312 template<class T,class X,class Y>
3314 {
3315  /* Variables:
3316  * - \a partial_1 is a pointer to the first partial child of
3317  * \p nodePtr.
3318  * - \a partial_2 is a pointer to the second partial child of
3319  * \p nodePtr.
3320  * - \a fullEnd_1 is a pointer to a full endmost child of \a partial_1.
3321  * - \a fullEnd_2 is a pointer to a full endmost child of \a partial_2.
3322  * - \a emptyEnd_2 is a pointer to the empty endmost child (more
3323  * precisely: to the endmost child appearing on the empty side) of
3324  * \a partial_2. In case that <em>ignored nodes</em> are used, this
3325  * \a emptyEnd_2 may store the adress of an ignored node.
3326  * - \a realEmptyEnd_2 is a pointer to the first <em>non ignored</em>
3327  * node with empty status on the empty side of \a partial_2. In case
3328  * that no ignored nodes are used, \a realEmpty_2 is identical to
3329  * \a endEmpty_2.
3330  */
3331 #if 0
3332  PQNode<T,X,Y> *partial_1 = 0;
3333  PQNode<T,X,Y> *partial_2 = 0;
3334  PQNode<T,X,Y> *fullEnd_1 = 0;
3335  PQNode<T,X,Y> *fullEnd_2 = 0;
3336  PQNode<T,X,Y> *emptyEnd_2 = 0;
3337  PQNode<T,X,Y> *realEmptyEnd_2 = 0;
3338 #endif
3339 
3340  if ((*nodePtr)->type() != PQNodeRoot::PQNodeType::PNode ||
3341  (*nodePtr)->partialChildren->size() != 2)
3342  return false;
3343 
3344  /*
3345  Get the partial children of [[nodePtr]] and removes the second
3346  partial child stored in [[partial_2]] from the children of
3347  [[nodePtr]]. If there are any full children of [[nodePtr]], the
3348  chunk removes them from the children of [[nodePtr]] and copies them
3349  as children to a new $P$-node. This new $P$-node is then made
3350  endmost child of [[partial_1]].
3351  */
3352  PQNode<T,X,Y> *partial_1 = (*nodePtr)->partialChildren->popFrontRet();
3353  PQNode<T,X,Y> *partial_2 = (*nodePtr)->partialChildren->popFrontRet();
3354 
3355  removeChildFromSiblings(partial_2);
3356  (*nodePtr)->m_childCount--;
3357  copyFullChildrenToPartial(*nodePtr,partial_1);
3358 
3359  /*
3360  Check the endmost children of the two partial children of [[nodePtr]]
3361  and stores them at approriate places, remembering what kind of type
3362  the endmost children are.
3363  */
3364  PQNode<T,X,Y> *fullEnd_1;
3365  if (clientLeftEndmost(partial_1)->status() == PQNodeRoot::PQNodeStatus::Full)
3366  fullEnd_1 = partial_1->m_leftEndmost;
3367  else {
3368  // partial child with no Full endmost child detected?
3370  fullEnd_1 = partial_1->m_rightEndmost;
3371  }
3372 
3373  PQNode<T,X,Y> *fullEnd_2 = nullptr;
3374  PQNode<T,X,Y> *emptyEnd_2 = nullptr;
3375  PQNode<T,X,Y> *realEmptyEnd_2 = nullptr;
3376  if (clientLeftEndmost(partial_2)->status() == PQNodeRoot::PQNodeStatus::Full)
3377  fullEnd_2 = partial_2->m_leftEndmost;
3378  else {
3379  // partial child with no Full or Empty endmost child detected?
3381 
3382  emptyEnd_2 = partial_2->m_leftEndmost;
3383  realEmptyEnd_2 = clientLeftEndmost(partial_2);
3384  }
3385 
3386  if (clientRightEndmost(partial_2)->status() == PQNodeRoot::PQNodeStatus::Full)
3387  fullEnd_2 = partial_2->m_rightEndmost;
3388  else {
3389  // partial child with no Full or Empty endmost child detected?
3391 
3392  emptyEnd_2 = partial_2->m_rightEndmost;
3393  realEmptyEnd_2 = clientRightEndmost(partial_2);
3394  }
3395 
3396  OGDF_ASSERT(fullEnd_2 != emptyEnd_2);
3397  //partial child with same type of endmost child detected
3398 
3399  /*
3400  The children of [[partial_2]] are removed from their parent and
3401  added as children to [[partial_1]]. This is done by resetting the
3402  sibling pointers of the two endmost children of [[partial_1]] and
3403  [[partial_2]] and the endmost child pointers of [[partial_1]].
3404  Observe that the parent pointers are not updated. The node
3405  [[partial_2]] is deleted.
3406  */
3407  while (!partial_2->fullChildren->empty())
3408  partial_1->fullChildren->pushFront(partial_2->fullChildren->popFrontRet());
3409  linkChildrenOfQnode(fullEnd_1,fullEnd_2);
3410  if (partial_1->m_leftEndmost == fullEnd_1)
3411  partial_1->m_leftEndmost = emptyEnd_2;
3412  else
3413  partial_1->m_rightEndmost = emptyEnd_2;
3414 
3415  emptyEnd_2->m_parent = partial_1;
3417 
3418  realEmptyEnd_2->m_parent = partial_1;
3419  realEmptyEnd_2->m_parentType = PQNodeRoot::PQNodeType::QNode;
3420 
3421  partial_1->m_childCount = partial_1->m_childCount +
3422  partial_2->m_childCount;
3423  destroyNode(partial_2);
3424 
3425  // If nodePtr does not have any
3426  // empty children, then it has to
3427  // be deleted and the partial node
3428  // is occupying its place in the tree.
3429  checkIfOnlyChild(partial_1,*nodePtr);
3430  // partial_1 is now root of the
3431  // pertinent subtree.
3432  *nodePtr = partial_1;
3433 
3434  return true;
3435 }
3436 
3437 template<class T,class X,class Y>
3438 bool PQTree<T,X,Y>::templateQ1(PQNode<T,X,Y> *nodePtr, bool isRoot)
3439 {
3440  if (nodePtr->type() == PQNodeRoot::PQNodeType::QNode &&
3441  nodePtr != m_pseudoRoot &&
3442  clientLeftEndmost(nodePtr)->status() == PQNodeRoot::PQNodeStatus::Full &&
3443  clientRightEndmost(nodePtr)->status() == PQNodeRoot::PQNodeStatus::Full)
3444  {
3445  PQNode<T,X,Y>* seqStart = nullptr;
3446  PQNode<T,X,Y>* seqEnd = nullptr;
3447  if (checkChain(nodePtr,clientLeftEndmost(nodePtr),&seqStart,&seqEnd))
3448  {
3450  if (!isRoot)
3451  nodePtr->m_parent->fullChildren->pushFront(nodePtr);
3452  return true;
3453  }
3454  }
3455 
3456  return false;
3457 }
3458 
3459 template<class T,class X,class Y>
3460 bool PQTree<T,X,Y>::templateQ2(PQNode<T,X,Y> *nodePtr,bool isRoot)
3461 {
3462  /* Variables:
3463  * - \a fullNode is a pointer to the full endmost child of \p nodePtr.
3464  * - \a sequenceBegin is a pointer to the first node of the
3465  * sequence of full children. Identical to the node fullNode and
3466  * mainly needed by the function checkChain().
3467  * - \a sequenceEnd is a pointer to the last node of the sequence
3468  * of full children. Is set by the function checkChain().
3469  * - \a partialChild is a pointer to the partial child of \p nodePtr.
3470  * - \a sequenceCons is 1 if all full children of
3471  * \p nodePtr form a consecutive sequence with one full child beeing
3472  * an endmost child of \p nodePtr \b and the partial child is
3473  * adjacent to the sequence.
3474  *
3475  * templateQ2() first checks if one of the above mentioned cases
3476  * occures and
3477  * then applies the necessary template matching.
3478  * No special action has to be performed for the full nodes. If there exists
3479  * a partial child that will be stored in \a partialChild, its children
3480  * are made children of \p nodePtr. So to say, \a partialChild is lifted
3481  * up to the Q-node \p nodePtr and the occurance of the children
3482  * of \a partialChild is fixed within the children of \p nodePtr.
3483  * (Remember that a partial child is also a Q-node).
3484  */
3485  if (nodePtr->type() != PQNodeRoot::PQNodeType::QNode ||
3486  nodePtr->partialChildren->size() > 1)
3487  return false;
3488 
3489  bool sequenceCons = false;
3490  if (nodePtr->fullChildren->size() > 0)
3491  {
3492  /*
3493  Get a full endmost child of
3494  the $Q$-node [[nodePtr]] if there exists one.
3495  */
3496  PQNode<T,X,Y> *fullNode = nullptr;
3497  if (nodePtr->m_leftEndmost != nullptr)
3498  {
3499  fullNode = clientLeftEndmost(nodePtr);
3500  if (fullNode->status() != PQNodeRoot::PQNodeStatus::Full)
3501  fullNode = nullptr;
3502  }
3503  if (nodePtr->m_rightEndmost != nullptr && fullNode == nullptr)
3504  {
3505  fullNode = clientRightEndmost(nodePtr);
3506  if (fullNode->status() != PQNodeRoot::PQNodeStatus::Full)
3507  fullNode = nullptr;
3508  }
3509 
3510  /*
3511  In case that a full endmost child of [[nodePtr]] exists, this
3512  child has been stored in [[fullNode]] and the chunk checks by
3513  calling the function [[checkChain]] (\ref{checkChain}), if all
3514  full children of [[nodePtr]] form a consecutive sequence.
3515  In case that the full children
3516  of [[nodePtr]] form a consecutive sequence the
3517  return value of [[checkChain]] is [[1]]. If a partial child
3518  stored in [[partialChild]] exists, the chunk checks if
3519  [[partialChild]] is adjacent to the sequence of full children.
3520  If the latter case is [[1]], the flag [[sequenceCons]] is
3521  set to [[1]] and the function [[templateQ2]] is allowed to
3522  reduce the pertient children of [[nodePtr]].
3523  */
3524  PQNode<T,X,Y> *sequenceBegin = nullptr;
3525  PQNode<T,X,Y> *sequenceEnd = nullptr;
3526  if (fullNode != nullptr)
3527  sequenceCons = checkChain(nodePtr,fullNode,&sequenceBegin,&sequenceEnd);
3528 
3529  if (sequenceCons && (nodePtr->partialChildren->size() == 1))
3530  {
3531  PQNode<T,X,Y> *partialChild = nodePtr->partialChildren->front();
3532  sequenceCons = false;
3533 
3534  if (clientSibLeft(sequenceEnd) == partialChild ||
3535  clientSibRight(sequenceEnd) == partialChild)
3536  sequenceCons = true;
3537  }
3538  }
3539  else
3540  {
3541  if (!nodePtr->partialChildren->empty())
3542  {
3543  /*
3544  If the $Q$-node [[nodePtr]] has no full children but one
3545  partial child this chunk checks, if the partial child is
3546  endmost child of the [[nodePtr]]. If this is not the case,
3547  [[nodePtr]] cannot be reduced by the template matching
3548  <b>Q2</b>.
3549  */
3550 #if 0
3551  nodePtr->partialChildren->startAtBottom();
3552  partialChild = nodePtr->partialChildren->readNext();
3553 #endif
3554  PQNode<T,X,Y> *partialChild = nodePtr->partialChildren->front();
3555  if ((clientLeftEndmost(nodePtr) == partialChild) ||
3556  (clientRightEndmost(nodePtr) == partialChild))
3557  sequenceCons = true;
3558  }
3559  }
3560 
3561  if (sequenceCons)
3562  removeBlock(nodePtr,isRoot);
3563 
3564  return sequenceCons;
3565 }
3566 
3567 template<class T,class X,class Y>
3569 {
3570  /* Variables:
3571  * - \a fullChild is a pointer to an arbitrary full child of \p nodePtr.
3572  * - \a fullStart is a pointer to the first full child of a
3573  * consecutive sequence of full children.
3574  * - \a fullEnd is a pointer to the last full child of a
3575  * consecutive sequence of full children.
3576  * - \a partial_1 is a pointer to the first partial child of \p nodePtr.
3577  * - \a partial_2 is a pointer to the second partial child of \p nodePtr.
3578  * - \a conssequence is 1 if the pertinent children of
3579  * \p nodePtr form a consecutive sequence with at most one partial
3580  * child at every end of the sequence.
3581  * - \a found is a help variable.
3582  *
3583  * templateQ3() first checks if one of the above mentioned cases
3584  * occures and then applies the neccessary template matching.
3585  * No special action has to be performed for the full nodes. If there exist
3586  * one or two partial children which will be stored in \a partial_1
3587  * or \a partial_2, their children
3588  * are made children of \p nodePtr. So to say, \a partial_1 and
3589  * partial_2 are lifted up to the Q-node \p nodePtr
3590  * and the occurance of their children
3591  * is fixed within the children of \p nodePtr.
3592  */
3593  if (nodePtr->type() != PQNodeRoot::PQNodeType::QNode || nodePtr->partialChildren->size() >= 3)
3594  return false;
3595 
3596  bool conssequence = false;
3597  bool found = false;
3598 
3599  /*
3600  Check ifthe
3601  pertinent children of [[nodePtr]] form a consecutive sequence. We
3602  differ between two cases:
3603  \begin{enumerate}
3604  \item There exist full children of [[nodePtr]]. First check with
3605  the function [[checkChain]] (\ref{checkChain}) if the full children
3606  form a consecutive sequence. In case that the check was
3607  successful, check if each partial child is adjacent to a full child.
3608  If both checks were successful, the pertient children form a
3609  consecutive sequence.
3610  \item There do not exist full children. Check if the partial
3611  children (there are at most two of them) form a consecutive sequence.
3612  If the test was successful, the pertinent children form a
3613  consecutive sequence.
3614  */
3615  if (!nodePtr->fullChildren->empty())
3616  {
3617  /*
3618  A consecutive
3619  sequence of full children has been detected, containing all full
3620  children of [[nodePtr]]. The chunk checks if each partial child
3621  of [[nodePtr]] is adjacent to a full child. Observe that the
3622  function [[templateQ3]] only reaches this chunk when [[nodePtr]]
3623  has less than three partial children.
3624  */
3625  PQNode<T,X,Y> *fullChild = nodePtr->fullChildren->front();
3626  PQNode<T,X,Y> *fullStart = nullptr;
3627  PQNode<T,X,Y> *fullEnd = nullptr;
3628  conssequence = checkChain(nodePtr,fullChild,&fullStart,&fullEnd);
3629  if (conssequence)
3630  {
3632  for (it = nodePtr->partialChildren->begin(); it.valid(); ++it)
3633  {
3634  PQNode<T,X,Y> *partial_1 = *it;
3635  found = false;
3636  if ((clientSibLeft(fullStart) == partial_1) ||
3637  (clientSibRight(fullStart) == partial_1) ||
3638  (clientSibLeft(fullEnd) == partial_1) ||
3639  (clientSibRight(fullEnd) == partial_1) )
3640  found = true;
3641  if (!found)
3642  conssequence = found;
3643  }
3644  }
3645  }
3646 
3647  else if (nodePtr->partialChildren->size() == 2)
3648  {
3649  /*
3650  In case that the node [[nodePtr]] does not have any full children,
3651  this chunk checks if the partial children are adjacent.
3652  */
3653  PQNode<T,X,Y> *partial_1 = nodePtr->partialChildren->front();
3654  PQNode<T,X,Y> *partial_2 = nodePtr->partialChildren->back();
3655  if ((clientSibLeft(partial_1) == partial_2) ||
3656  (clientSibRight(partial_1) == partial_2) )
3657  found = true;
3658  conssequence = found;
3659  }
3660 
3661  if (conssequence)
3662  removeBlock(nodePtr,true);
3663 
3664  return conssequence;
3665 }
3666 
3667 }
int m_pertChildCount
Stores the number of pertinent children of the node.
Definition: PQNode.h:443
List< PQNode< T, X, Y > * > * partialChildren
Stores all partial children of a node during a reduction.
Definition: PQNode.h:505
int size() const
Returns the number of elements in the queue.
Definition: Queue.h:254
virtual int removeNodeFromTree(PQNode< T, X, Y > *parent, PQNode< T, X, Y > *child)
The objective is to remove a node child from the PQ-tree.
Definition: PQTree.h:3034
void copyFullChildrenToPartial(PQNode< T, X, Y > *nodePtr, PQNode< T, X, Y > *partialChild)
Copies all full children of nodePtr to a new P-node The node nodePtr has to be a P-node.
Definition: PQTree.h:1720
The parameterized class Queue<E> implements list-based queues.
Definition: Queue.h:213
bool valid() const
Returns true iff the iterator points to an element.
Definition: SList.h:113
PQNode< T, X, Y > * root() const
Returns a pointer of the root node of the PQTree.
Definition: PQTree.h:170
int m_pertLeafCount
Stores the number of pertinent leaves in the frontier of the node.
Definition: PQNode.h:446
iterator append(const E &x)
Adds x at the end of queue.
Definition: Queue.h:343
bool addNewLeavesToTree(PQInternalNode< T, X, Y > *father, SListPure< PQLeafKey< T, X, Y > *> &leafKeys)
Adds a set of elements to the already existing set of elements of a PQ-tree.
Definition: PQTree.h:818
bool empty() const
Returns true iff the list is empty.
Definition: SList.h:220
List< PQNode< T, X, Y > * > * fullChildren
Stores all full children of a node during a reduction.
Definition: PQNode.h:502
Declaration and implementation of the class PQLeafKey.
virtual bool Bubble(SListPure< PQLeafKey< T, X, Y > *> &leafKeys)
Realizes a function described in [Booth].
Definition: PQTree.h:1099
PQNode< T, X, Y > * m_referenceParent
Is a pointer to the parent, in case that the parent is a P-node and the node itself is its reference ...
Definition: PQNode.h:474
virtual void destroyNode(PQNode< T, X, Y > *nodePtr)
Marks a node as PQNodeRoot::PQNodeStatus::ToBeDeleted.
Definition: PQTree.h:577
Singly linked lists.
Definition: SList.h:38
virtual void Cleanup()
Removes the entire PQ-tree.
Definition: PQTree.h:1550
virtual bool Reduction(SListPure< PQLeafKey< T, X, Y > *> &leafKeys)
Tests whether permissible permutations of the elements of U exist such that the elements of a subset ...
Definition: PQTree.h:2395
virtual bool templateQ3(PQNode< T, X, Y > *nodePtr)
Definition: PQTree.h:3568
PQNode< T, X, Y > * createNodeAndCopyFullChildren(List< PQNode< T, X, Y > *> *fullNodes)
Copies the full children of a P-node that are stored in the stack fullNodes to a new P-node...
Definition: PQTree.h:1760
virtual bool templateQ1(PQNode< T, X, Y > *nodePtr, bool isRoot)
Template matching for Q-nodes with only full children.
Definition: PQTree.h:3438
virtual int Initialize(SListPure< PQLeafKey< T, X, Y > *> &leafKeys)
Initializes the PQ-tree with a set of elements.
Definition: PQTree.h:2040
Encapsulates a pointer to an ogdf::SList element.
Definition: SList.h:40
int m_childCount
Definition: PQNode.h:416
PQNodeType m_parentType
Stores the type of the parent which can be either a P- or Q-node.
Definition: PQNode.h:440
virtual PQLeafKey< T, X, Y > * getKey() const =0
getKey() returns a pointer to the PQLeafKeyof a node, in case that the node is supposed to have a key...
virtual PQNode< T, X, Y > * clientSibRight(PQNode< T, X, Y > *nodePtr) const
Definition: PQTree.h:701
virtual bool Reduce(SListPure< PQLeafKey< T, X, Y > *> &leafKeys)
Performs the reduction of the pertinent leaves with the help of the template matchings, designed by Booth and Lueker.
Definition: PQTree.h:2295
virtual void removeChildFromSiblings(PQNode< T, X, Y > *nodePtr)
Removes the node nodePtr from the doubly linked list of its parent.
Definition: PQTree.h:2972
PQNode< T, X, Y > * m_pertinentRoot
a pointer to the root of the pertinent subtree.
Definition: PQTree.h:185
bool empty() const
Returns true iff the stack is empty.
Definition: Stack.h:222
virtual PQNodeMark mark() const =0
mark() returns the variable PQLeaf::m_mark in the derived class PQLeaf and PQInternalNode.
bool endmostChild()
The function endmostChild() checks if a node is endmost child of a Q-node.
Definition: PQNode.h:121
void emptyNode(PQNode< T, X, Y > *nodePtr)
Cleans up all stacks, flags and pointers of a pertinent node that has been visited during the reducti...
Definition: PQTree.h:1881
Declaration and implementation of Array class and Array algorithms.
PQNode< T, X, Y > * m_root
a pointer to the root of the $PQ$-tree.
Definition: PQTree.h:182
virtual void linkChildrenOfQnode(PQNode< T, X, Y > *installed, PQNode< T, X, Y > *newChild)
Links the two endmost children of two different Q-nodes via their sibling pointers together...
Definition: PQTree.h:2068
virtual void exchangeNodes(PQNode< T, X, Y > *oldNode, PQNode< T, X, Y > *newNode)
Replaces the oldNode by the newNode in the tree.
Definition: PQTree.h:1892
SibDirection putSibling(PQNode< T, X, Y > *newSib)
The default function putSibling() stores a new sibling at a free sibling pointer of the node...
Definition: PQNode.h:251
virtual PQNodeStatus status() const =0
Returns the variable PQLeaf::m_status in the derived class PQLeaf and PQInternalNode.
virtual bool addNodeToNewParent(PQNode< T, X, Y > *parent, PQNode< T, X, Y > *child)
Adds a node child as a child to another node specified in parent.
Definition: PQTree.h:871
PQNode< T, X, Y > * m_rightEndmost
Stores the right endmost child of a Q-node.
Definition: PQNode.h:477
List< PQNode< T, X, Y > * > * partialChildren(PQNode< T, X, Y > *nodePtr)
Definition: PQTree.h:680
virtual bool templateL1(PQNode< T, X, Y > *nodePtr, bool isRoot)
Template matching for leaves.
Definition: PQTree.h:3070
virtual void front(PQNode< T, X, Y > *nodePtr, SListPure< PQLeafKey< T, X, Y > *> &leafKeys)
Returns the keys stored in the leaves of the front of nodePtr.
Definition: PQTree.h:1995
virtual bool checkIfOnlyChild(PQNode< T, X, Y > *child, PQNode< T, X, Y > *parent)
Checks if child is the only child of parent.
Definition: PQTree.h:1525
virtual void clientDefinedEmptyNode(PQNode< T, X, Y > *nodePtr)
If the user wishes to use different flags in a derived class of PQTree that are not available in this...
Definition: PQTree.h:123
virtual PQNode< T, X, Y > * clientRightEndmost(PQNode< T, X, Y > *nodePtr) const
Definition: PQTree.h:688
virtual const char * clientPrintStatus(PQNode< T, X, Y > *nodePtr)
If the user wishes to use different status in a derived class of PQTree that are not available in thi...
Definition: PQTree.h:1696
bool changeSiblings(PQNode< T, X, Y > *oldSib, PQNode< T, X, Y > *newSib)
The function changeSiblings() replaces the old sibling oldSib of the node by a new sibling newSib...
Definition: PQNode.h:549
List< PQNode< T, X, Y > * > * m_pertinentNodes
Stores all nodes that have been marked PQNodeRoot::PQNodeStatus::Full or PQNodeRoot::PQNodeStatus::Pa...
Definition: PQTree.h:208
iterator begin()
Returns an iterator to the first element of the list.
Definition: SList.h:316
E pop()
Removes the top-most element from the stack and returns it.
Definition: Stack.h:272
virtual PQNode< T, X, Y > * clientSibLeft(PQNode< T, X, Y > *nodePtr) const
Definition: PQTree.h:697
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: SList.h:455
bool checkChain(PQNode< T, X, Y > *nodePtr, PQNode< T, X, Y > *firstFull, PQNode< T, X, Y > **seqStart, PQNode< T, X, Y > **seqEnd)
Checks whether all full children of a Q-node nodePtr form a consecutive sequence. ...
Definition: PQTree.h:1391
Declaration and implementation of the class PQleaf.
int m_identificationNumber
Each node that has been introduced once into the tree gets a unique number.
Definition: PQNode.h:437
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
PQNode< T, X, Y > * m_referenceChild
Stores a pointer to one child, the reference child of the doubly linked cirkular list of children of ...
Definition: PQNode.h:466
PQNode< T, X, Y > * getNextSib(PQNode< T, X, Y > *other) const
The function getNextSib() returns one of the siblings of the node.
Definition: PQNode.h:183
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
void push(const E &x)
Adds element x as top-most element to the stack.
Definition: Stack.h:256
void writeGML(const char *fileName)
The function writeGML() prints the PQ-tree in the GML fileformat.
Definition: PQTree.h:2096
virtual bool templateP3(PQNode< T, X, Y > *nodePtr)
Template matching for a P-node with full and empty children that is not the root of the pertinent sub...
Definition: PQTree.h:3129
virtual bool templateP5(PQNode< T, X, Y > *nodePtr)
Template matching for a P-node with full, empty children and exactly one partial child.
Definition: PQTree.h:3217
virtual bool templateP4(PQNode< T, X, Y > **nodePtr)
Template matching for a P-node with full, empty and exactly one partial children. ...
Definition: PQTree.h:3196
virtual void emptyAllPertinentNodes()
Cleans up all flags that have been set in the pertinent nodes during the reduction process...
Definition: PQTree.h:1844
PQNode< T, X, Y > * m_pseudoRoot
a pointer to the virtual root of the pertinent subtree, in case that the pertinent root cannot be det...
Definition: PQTree.h:188
bool empty() const
Returns true iff the queue is empty.
Definition: Queue.h:251
Encapsulates a pointer to a list element.
Definition: List.h:41
PQNode< T, X, Y > * m_leftEndmost
Definition: PQNode.h:451
virtual PQNode< T, X, Y > * clientLeftEndmost(PQNode< T, X, Y > *nodePtr) const
Definition: PQTree.h:684
PQNode< T, X, Y > * m_parent
Is a pointer to the parent.
Definition: PQNode.h:458
virtual bool templateP1(PQNode< T, X, Y > *nodePtr, bool isRoot)
Template matching for P-nodes with only full children.
Definition: PQTree.h:3083
Declaration and implementation of the class PQNodeKey.
void removeBlock(PQNode< T, X, Y > *nodePtr, bool isRoot)
Of every partial node that is found in the sequence of children of nodePtr, all children are removed ...
Definition: PQTree.h:2409
E popFrontRet()
Removes the first element from the list and returns it.
Definition: SList.h:512
The class template PQLeafKey is a derived class of class template PQBasicKey.
Definition: PQLeafKey.h:86
The class template PQBasicKey is an abstract base class.
Definition: PQBasicKey.h:108
virtual int clientPrintNodeCategorie(PQNode< T, X, Y > *nodePtr)
If the user wishes to use different flags in a derived class of PQTree that are not available in this...
Definition: PQTree.h:1689
The class template PQInternalNode is used to present P-nodes and Q-nodes in the PQ-Tree.
virtual void CleanNode(PQNode< T, X, Y > *)
Definition: PQTree.h:102
virtual bool templateP6(PQNode< T, X, Y > **nodePtr)
Template matching for a P-node with full, empty and exactly two partial children. ...
Definition: PQTree.h:3313
Declaration and implementation of the class PQInternalKey.
PQNode< T, X, Y > * m_sibRight
Stores a pointer ot the right sibling of PQNode.
Definition: PQNode.h:495
Declaration and implementation of the class PQInternalNode.
The datastructure PQ-tree was designed to present a set of permutations on an arbitrary set of elemen...
Definition: PQLeaf.h:48
void sortExceptions(int Exceptions[], int arraySize)
Sorts the exceptions before frontExcept() scans the frontier.
Definition: PQTree.h:3052
List< PQNode< T, X, Y > * > * fullChildren(PQNode< T, X, Y > *nodePtr)
Definition: PQTree.h:675
virtual PQNodeRoot::PQNodeType type() const
Returns the variable m_type in the derived class PQInternalNode.
virtual PQNode< T, X, Y > * clientNextSib(PQNode< T, X, Y > *nodePtr, PQNode< T, X, Y > *other) const
Definition: PQTree.h:692
virtual bool templateP2(PQNode< T, X, Y > **nodePtr)
Template matching for a P-node with full and empty children that is the root of the pertinent subtree...
Definition: PQTree.h:3099
virtual const char * clientPrintType(PQNode< T, X, Y > *nodePtr)
If the user wishes to use different types in a derived class of PQTree that are not available in this...
Definition: PQTree.h:1702
The namespace for all OGDF objects.
virtual ~PQTree()
Destructor.
Definition: PQTree.h:62
Declaration and implementation of the class PQNode.
E pop()
Removes front element and returns it.
Definition: Queue.h:357
PQNode< T, X, Y > * m_sibLeft
Stores a pointer ot the left sibling of PQNode.
Definition: PQNode.h:486
bool changeEndmost(PQNode< T, X, Y > *oldEnd, PQNode< T, X, Y > *newEnd)
The function changeEndmost() replaces the old endmost child oldEnd of the node by a new child newEnd...
Definition: PQNode.h:521
int m_numberOfLeaves
Stores the number of leaves.
Definition: PQTree.h:198
bool valid() const
Returns true iff the iterator points to an element.
Definition: List.h:112
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
virtual bool templateQ2(PQNode< T, X, Y > *nodePtr, bool isRoot)
Template matching for Q-nodes with a pertinent sequence of children on one side of the Q-node...
Definition: PQTree.h:3460
virtual PQNodeType type() const =0
Returns the variable PQInternalNode::m_type in the derived class PQLeaf and PQInternalNode.
The parameterized class Stack<E> implements list-based stacks.
Definition: Stack.h:192
Declaration and implementation of list-based stacks (StackPure<E> and Stack<E>).
const_reference top() const
Returns a reference to the front element.
Definition: Queue.h:257
int m_identificationNumber
Stores the total number of nodes that have been allocated.
Definition: PQTree.h:195