Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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