2016-11-22 68 views
0

使用OGDF我已經創建和初始化一個PQTree。初始化用3條邊完成,其中節點a是根,b,c和d是a的葉。現在,經過計算,我需要將葉子e,d和f添加爲b作爲葉子。但問題是,b是一片葉子,所以既不接受孩子也不接受葉子。代碼在這裏。作爲std :: cout,我已經知道它們是被添加的,但是如果使用writeGML將它寫入GML文件,添加節點之前和之後沒有區別,它們不在樹中。我認爲,這是因爲PQLeafKey,對於非葉邊/節點,它應該是PQNodeKey。根據該文件,ablk-> nodePointer()應返回PQLeaf,它從PQNode衍生和不與PQInternelNode,這也衍生PQNode「兼容」。但我不知道,如何以不同的方式添加。代碼:OGDF PQTree:如何添加葉子?

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"); 

回答

0

OKI,DOKI,

我知道了,也做了,完美的作品。抱歉回覆晚了。直接將葉子添加到現有葉子將無法工作。我使用的方法是在課堂上PQTree保護exchangeNodes(PQNode * oldNode,PQNode * newNode)。首先,我提取葉子的ID,然後創建一個新的PQInternalNode * newNode,它依次是EMPTY和P-Node以及提取的ID。提取和使用相同的ID不是必須的,但它看起來更具可讀性。交換葉的節點到* newNode會影響葉中的節點類型,並欺騙pqtree處理葉子,就像在交換之後開始的p節點一樣,這反過來又允許我將新樹葉添加到* newNode ,這不再是一片葉子。