在树中创建族树和记住父节点

时间:2014-04-23 13:50:03

标签: c# asp.net algorithm gridview

尽我所能尽可能清晰简洁......

我试图制作一个像动物一样的家谱,有父亲和母亲,父亲和母亲,父亲(那个动物的大父亲)和母亲(那个动物的大母亲)。我想将树表示存储在任何表格中,如结构。 我可能犯了一个错误,但这里是我想要的输入和输出的一般概念

enter image description here

为此,我开发了以下算法

Table table = new Table();
foreach(row in resultSet)
{
   int i = 1; int r = 1;
   AddAncestersOf(row.Cells[1], resultSet, table.Row[r], i);
   r++;
}

AddAncestersOf

的定义
void AddAncestersOf(String A, ResultSet set, TableRow tableRow, int level)
{
   Select aRow From set Where setCol1 = A;//select row where animalID is A
   if(aRow.Col[2] and aRow.Col[3] are not empty)//if animal has father and mother
   {
      tableRow.Col[(2*i)]=aRow.Col[2];//store father of that animal
      tableRow.Col[(2*i)+1]=aRow.Col[3];//store mother of that animal
      AddAncestersOf(row.Col[2], set, tableRow, level+1);
      AddAncestersOf(row.Col[3], set, tableRow, level+1);
   }
}

首先,如果有人能告诉我更好的解决方案,请帮助我......

第二个也是更重要的问题是,我想要记住层次结构,因为我在树中参考根中的动物,如果S代表父亲,D代表母亲,那么我想要我的表格专栏标题像

S D SS SD SSS SSD SSSS SSSD SDS SDD SDSS等。

由于这些标题是根据递归结构我可以在 AddAncestersOf

中的这些行之前做到这一点
      //set appropriate heading of tableRow.Col[(2*i)]
      tableRow.Col[(2*i)]=aRow.Col[2];//store father of that animal
      //set appropriate heading of tableRow.Col[(2*i)+1]
      tableRow.Col[(2*i)+1]=aRow.Col[3];//store mother of that animal

我正在考虑将S和D存储在堆栈或队列等结构中,因为我深入树中并通过从该结构中删除所有S和D并将其附加到标题然后追加来设置标题最后还有一个S或D。

算法最接近实现会更好..对于实现我会在ASP.net中使用C#而对于表我需要填充GridView。如果有人知道如何帮助请帮助!而且任何更好的想法都会很棒!

2 个答案:

答案 0 :(得分:1)

我建议您使用散列从ResultRet数据构建二叉树。散列键是A列,散列数据是带S和D的双元素数组,或者是nil类型的值,表示没有父数据。

鉴于您对输出的要求,我认为您的递归方法是一个不错的选择(我稍微重写了一下,但逻辑应该与您的算法相同)。

标题是根据列索引的二进制表示构建的,注意0和1对应于S和D,忽略最高有效数字。

遗憾的是,我不能给你一个c#的答案,但我希望这个ruby代码能够提供足够好的伪代码:

# This hash should be created from the ResultRet data.
# One entry for each row.
$h=Hash[
       "A03",["A02","A01"],
       "A02",["A04","A11"],
       "A05",["A02","A01"],
       "A06",nil,
       "A07",["A08","A09"],
       "A08",["A05","A06"],
       "A11",nil,
]

# Make a 2d array:
# (initialized with dashes (to make output more readable))
# Select appropriate dimensions if your grid is not dynamic.
grid=Array.new(8)
(0..8).each{|i| grid[i]=Array.new(20,"-")}

# Fill out one line/row with ancestors of animal given by id.
# The row will stay constant throughout the recursion.
def recurse(row,id,pos)
  # Avoid going too far back:
  return if pos>=2**(4+1)  # max 4 generations back. 
  # store this animal at the given pos:
  row[pos]=id
  # Stop recursing if no parents are defined for this id:
  return unless $h[id]
  # We get parents of id directly by looking up in the hash
  sire,dam = $h[id]
  # Each generation back doubles in size.
  # This calculates the starting column for the parents of id:
  pos*=2
  # continue recursing for the parents
  recurse(row,sire,pos)   
  recurse(row,dam ,pos+1)
end

# Fill inn the result grid, one row at a time:
# This iterates over the keys in the hash.
# (Could also iterate over first columnt in the source data.)
$h.keys.each_with_index{|key,i|
  recurse(grid[i],key,1)
}

# headings:
print "           A"
(2..19).each{|i|
  head=i.to_s(2)[1..-1].gsub("0","S").gsub("1","D")
  print "%6s"%head
}
puts

# print the grid
grid.each{|row|
  row.each{|cell|
    print "%6s"%cell

  }
  puts
}

输出: (由于我的网格从零开始的索引而产生的额外列)

       A     S     D    SS    SD    DS    DD   SSS   SSD   SDS   SDD   DSS   DSD   DDS   DDD  SSSS  SSSD  SSDS  SSDD
 -   A03   A02   A01   A04   A11     -     -     -     -     -     -     -     -     -     -     -     -     -     -
 -   A02   A04   A11     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -
 -   A05   A02   A01   A04   A11     -     -     -     -     -     -     -     -     -     -     -     -     -     -
 -   A06     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -
 -   A07   A08   A09   A05   A06     -     -   A02   A01     -     -     -     -     -     -   A04   A11     -     -
 -   A08   A05   A06   A02   A01     -     -   A04   A11     -     -     -     -     -     -     -     -     -     -
 -   A11     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -
 -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -
 -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -

答案 1 :(得分:0)

根据数据环境的不同,我会考虑一个上下都有链接的树形结构。数据中可能存在多个单独的树,因此最好保留没有后代的节点列表,也可以是没有祖先的节点列表。