/// <summary>
/// 广度遍历树,并给其编码
/// </summary>
/// <param name="T"></param>
void BFS(TreeNode T)
{
//调用先序遍历方法给每个节点的过渡节点个数信息赋初值
preOrderTraverse(T);
Queue queue = new Queue();
queue.Enqueue(T);
//所有节点应该不会超过20层吧哈哈哈
int[] count = new int[20] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
while (queue.Count > 0)
{
TreeNode node = (TreeNode)queue.Dequeue();
//关键步骤:计算当前节点所在层 在当前节点左边有多少个节点
count[node.Level]++;
for (int i = 0; i < node.Nodes.Count; i++)
{
queue.Enqueue(node.Nodes[i]);
if ((node.Tag as document_node_t).node_type_id > 20)
node.Nodes[i].ToolTipText = (int.Parse(node.Nodes[i].ToolTipText) + int.Parse(node.ToolTipText) + 1).ToString();
else
node.Nodes[i].ToolTipText = int.Parse(node.ToolTipText).ToString();
}
(node.Tag as document_node_t).node_number = (node.Level+1).ToString()+"-"+node.ToolTipText+"-"+(count[node.Level]).ToString();
node.Text= (node.Level + 1).ToString() + "-" + node.ToolTipText + "-" + count[node.Level].ToString();
//reportDao.updateNodeNumber((node.Tag as document_node_t).node_name, (node.Tag as document_node_t).node_number);
}
}
void preOrderTraverse(TreeNode T)
{
Stack stack = new Stack();
TreeNode p;
stack.Push(T);
//string s = "";
while (stack.Count != 0)
{
p = (TreeNode)stack.Pop();
//关键步骤:给每个节点的ToolTipText赋初值
p.ToolTipText = "0";
//s += (p.ToolTipText + "
");
if (p.NextNode != null)
stack.Push(p.NextNode);
if (p.Nodes.Count > 0)
stack.Push(p.Nodes[0]);
}
//MessageBox.Show(s);
}