treenode路径搜索问题

.Net技术 码拜 7年前 (2017-04-20) 1100次浏览
本人用treenode存储本人的树,例如根节点中node.text=”a”;内容如下
treenode路径搜索问题

本人现在想给定一个text的内容,如”K”,则返回如下结果a->b->I->K.
有一个treenode.find()方法但是要求用node.name去搜索。
求各位高手帮忙,在线给分。

解决方案

60

引用:
Quote: 引用:

根据K获取Node 之后 使用 Parent 属性 向上找值就行了,例如:
if(treenode!=null){
TreeNode parentI = treenode.Parent;
if(parentI!=null)
{
//获取其它
}
}

关键就是怎么样根据K获取,node?构造treenode的时候将treenode.name和treenode.text内容存入于dictionary?

记录下来也可以。还有一个种方法就是用递归遍历树节点,找到你要的带有K的节点。递归得到的路径就是你要的路径

20

引用:
Quote: 引用:

根据K获取Node 之后 使用 Parent 属性 向上找值就行了,例如:
if(treenode!=null){
TreeNode parentI = treenode.Parent;
if(parentI!=null)
{
//获取其它
}
}

关键就是怎么样根据K获取,node?构造treenode的时候将treenode.name和treenode.text内容存入于dictionary?

递归获取。 包含2个参数  1个treeNode对象,1个表示K值的string属性。

20

class TreeNode
{
public TreeNode Parent{get;set;}
public List<TreeNode > ChildrenNodes{get;set;}
public string Name{get;set;}
public string Text{get;set;}
public string GetFullPath()
{
string path=Text;
var node=this.Parent;
while(node!=null)
{
path=node.Text+”->”+path;
node=node.Parent;
}
return path;
}
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明treenode路径搜索问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)