2012-05-02 46 views
0

我如何使用tree.hh: an STL-like C++ tree class填充我的树并获取下面的树。一些帮助将不胜感激。 a和g是根节点 谢的使用tree.hh填充n-tree tree STL Like

  A   G 
     ______|____  | 
    / | \  | 
    B  C  D  H 
    |  |  |  | 
    |  E  |  | 
    \_____/  |  | 
     |   |  | 
     F   |  | 
     |_________|______| 
      | 
      I 
      | 
      J 

在然后上面的代码中,我使用深度优先搜索列表中的项目enumarate。我已经格式化这样

typedef tree<std::string> TreeNode; 
typedef struct 
{ 
    int nBases; 
    char * name; 
} BASES; 
BASES rgbases[] = 
{ 
    {0xB, "J"}, 
    {0xA, "I"}, 
    {0x1, "H"},{0x0, "G"}, 
    {0x5, "F"},{0x2, "E"},{0x1, "C"},{0x0, "A"}, 
    {0x1, "D"},{0x0, "A"}, 
    {0x1, "B"},{0x0, "A"} 
}; 

//here i'm trying to populate my tree 
void populateTree(TreeNode &tr, BASES *pBaseArray, int numBase) 
{ 
    int n = 0; 
    while (n < numBase) 
    { 
     BASES *pBase = &pBaseArray[n]; 
     if (pBase->nBases > 0) // Check for children of the new node 
      populateTree(tr, pBaseArray + (n + 1),pBase->nBases); 
     // i suppose i need to insert tree code part here 
     n += pBase->nBases + 1; 
    } 
} 
void BuildTree(TreeNode &tr) 
{ 
    populateTree(tr, rgBases, _countof(rgBases)); 
} 
+0

据我所知,tree.hh不能给你图。你可以考虑使用[boost图表](http://www.boost.org/libs/graph/)。 – user2k5

+0

感谢您的回复,我希望先创建我的树,如果有必要,我可以使用图表。 – htk59

+0

我很困惑。在[tree](http://en.wikipedia.org/wiki/Tree_(data_structure))中,每个节点最多只有一个父节点。但是你的'A'节点似乎有三个。 –

回答

0

树少数数据,跨越所呈现的原始图,有可能在除去连接节点A与B和d(或可能联用C和d节点A上的边缘)的边缘而获得。然后,树类将适用:在上述树引起环路

  A   G         A   G 
      |   |        ______|   | 
      |   |       /     | 
    B  C  D  H       B  C  D  H 
    |  |  |  |       |  |  |  | 
    |  E  |  |       |  E  |  | 
    \______/  |  |       \______/  |  | 
     |   |  |        |   |  | 
     F   |  |        F   |  | 
     |_________|______|        |__________|______| 
      |            | 
      I            I 
      |            | 
      J            J 

边线,可以分开记录,即AB和AD,可以在结构中从树类应注意,除了。合并具有这种结构的树将恢复原始图。

+0

我画的树只是我所有数据的一部分。但都具有萨马格式。你能用我的代码告诉我我该如何处理它。我不知道它是否是递归的,我可以处理继承。谢谢 – htk59