1

使用 OGDF 我创建并初始化了一个 PQTree。初始化是用 3 条边完成的,其中节点 a 是根,b、c 和 d 是 a 的叶子。现在,经过计算,我需要将叶子 e、d 和 f 添加到 b 作为叶子。但问题是,b 是叶子,所以既不接受孩子也不接受叶子。代码在这里。作为 std::cout,我已经添加了它们,但是如果我使用 writeGML 将其写入 GML 文件,则添加节点之前和之后没有区别,它们不在树中。我认为,这是因为 PQLeafKey,对于非叶边/节点,它应该是 PQNodeKey。根据文档,ablk->nodePointer() 应该返回 PQLeaf,它派生自 PQNode,并且与同样派生 PQNode 的 PQInternelNode 不“兼容”。但我不知道,如何以不同的方式添加。代码:

G = new Graph();
GA = new GraphAttributes(*G, GraphAttributes::nodeGraphics |
                         GraphAttributes::edgeGraphics |
                         GraphAttributes::nodeStyle |
                         GraphAttributes::nodeId |
                         GraphAttributes::edgeType |
                         GraphAttributes::edgeArrow |
                         GraphAttributes::edgeStyle);
node a = G->newNode();
node b = G->newNode();
node c = G->newNode();
node d = G->newNode();
edge ab = G->newEdge(a, b);
edge ac = G->newEdge(a, c);
edge ad = G->newEdge(a, d);

PQLeafKey<edge, IndInfo *, bool> *ablk = new PQLeafKey<edge, IndInfo *, bool>(ab);
PQLeafKey<edge, IndInfo *, bool> *aclk = new PQLeafKey<edge, IndInfo *, bool>(ac);
PQLeafKey<edge, IndInfo *, bool> *adlk = new PQLeafKey<edge, IndInfo *, bool>(ad);

SListPure<PQLeafKey<edge, IndInfo *, bool> *> *lkl = new SListPure<PQLeafKey<edge, IndInfo *, bool> *>();
lkl->pushBack(ablk);
lkl->pushBack(aclk);
lkl->pushBack(adlk);

pqtree = new PQTree<edge, IndInfo *, bool>();
pqtree->Initialize(*lkl);
pqtree->writeGML("/home/LPT/graph_qtree_MOC_after_initialization.gml");

node e = G->newNode();
node f = G->newNode();
node g = G->newNode();
edge be = G->newEdge(b, e);
edge bf = G->newEdge(b, f);
edge bg = G->newEdge(b, g);
PQLeafKey<edge, IndInfo *, bool> *belk = new PQLeafKey<edge, IndInfo *, bool>(be);
PQLeafKey<edge, IndInfo *, bool> *bflk = new PQLeafKey<edge, IndInfo *, bool>(bf);
PQLeafKey<edge, IndInfo *, bool> *bglk = new PQLeafKey<edge, IndInfo *, bool>(bg);
SListPure<PQLeafKey<edge, IndInfo *, bool> *> *lkl4 = new SListPure<PQLeafKey<edge, IndInfo *, bool> *>();
lkl4->pushBack(belk);
lkl4->pushBack(bflk);
lkl4->pushBack(bglk);

PQInternalNode<edge, IndInfo *, bool> *father = (PQInternalNode<edge, IndInfo *, bool> *) (ablk->nodePointer());
father->type(PQNodeRoot::PNode);
bool r = pqtree->addNewLeavesToTree(father, *lkl4);
QString res = r ? "done." : "failed.";
std::cout << "Adding leaves to the tree for MOC has " << res.toStdString() << std::endl;
pqtree->writeGML("/home/LPT/graph_qtree_MOC_after_addition_be_bf_bg.gml");
4

1 回答 1

0

oki, doki,

我得到它并且已经完成了,完美的工作。抱歉回复晚了。直接将叶子添加到现有叶子将不起作用。我使用的方法是 PQTree 类中受保护的 exchangeNodes(PQNode *oldNode, PQNode *newNode)。首先,我提取叶子的 ID,然后创建一个新的 PQInternalNode *newNode,它又是 EMPTY 和具有提取 ID 的 P-Node。提取和使用相同的 ID 不是必须的,但这样看起来更易读。将叶子节点交换到 *newNode 会影响叶子中的节点类型并欺骗 pqtree 像在交换之后开始的 p 节点一样处理叶子,这反过来又允许我将新叶子添加到 *newNode ,它不再是一片叶子了。

于 2016-12-03T12:47:39.793 回答