TreeView 树的查找/遍历
来源:互联网 发布:微信埋雷控制尾数软件 编辑:程序博客网 时间:2024/05/30 04:18
最近几个项目用到 TreeView 控件,遍历/查找 TreeView 树节点使用较多。每次都写,多有不便,干脆写个相关的类,以后用就方便了。
TreeView 遍历可分为“递归”和“非递归”两种。递归的简单,但是效率低,占内存也多。非递归的麻烦些,但效率高些。
递归:
/// <summary>
/// 递归查找某节点下指定显示文本的树节点。
/// </summary>
/// <param name="parent">父节点</param>
/// <param name="text">查找的显示文本</param>
/// <returns></returns>
public static TreeNode FindNodeRecurtion(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Text == strValue) return tnParent;
TreeNode tnReturn = null;
foreach (TreeNode node in tnParent.Nodes)
{
tnReturn = FindNodeRecurtion(node, strValue);
if (tnReturn!= null) break;
}
return tnReturn;
}
非递归:
非递归的可有的研究了。TreeView 的遍历实际上即为多叉树的遍历,这是个难点,以后有时间慢慢研究。大家有好的算法多多指教。
方法一:
这个方法网上可以找到的,基本上随便一搜都是它。不过其中有个错误,这里也已经作了修改。代码都是检验过的,复制到程序中即可使用。
/// <summary>
/// 遍历所有树节点到集合
/// </summary>
/// <param name="tree"></param>
/// <returns></returns>
public static List<TreeNode> TraverseWithoutRecurtion(TreeView tree)
{
List<TreeNode> list = new List<TreeNode>();
TreeNode tnCurrent, tnParent;
foreach (TreeNode node in tree.Nodes)
{
list.Add(node);
tnParent = node;
tnCurrent = tnParent.FirstNode;
while (tnCurrent != null && tnCurrent != tnParent)
{
while (tnCurrent != null)
{
list.Add(tnCurrent);
if (tnCurrent.Nodes.Count > 0)
{
//子路径中最深的第一个子节点
tnParent = tnCurrent;
tnCurrent = tnCurrent.FirstNode;
}
else if (tnCurrent != tnParent.LastNode)
{
//下一个同级节点
tnCurrent = tnCurrent.NextNode;
}
else
break;
}
//获取父节点的下一个同级节点
while (tnCurrent != tnParent && tnParent!= null && tnCurrent == tnParent.LastNode)
{
tnCurrent = tnParent;
tnParent = tnParent.Parent;
}
//下一个同级节点
if (tnCurrent != tnParent)
tnCurrent = tnCurrent.NextNode;
}
}
return list;
}
这是查找指定文本节点的非递归方法。
/// <summary>
/// 非递归查询指定节点
/// </summary>
/// <param name="tnParent"></param>
/// <param name="strValue"></param>
/// <returns></returns>
public static TreeNode FindNodeWithoutRecurtion(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Text == strValue) return tnParent;
else if (tnParent.Nodes.Count == 0) return null;
TreeNode tnCurrent, tnCurrentPar;
tnCurrentPar = tnParent;
tnCurrent = tnCurrentPar.FirstNode;
while (tnCurrent != null && tnCurrent != tnParent)
{
while (tnCurrent != null)
{
if (tnCurrent.Text == strValue) return tnCurrent;
else if (tnCurrent.Nodes.Count > 0)
{
tnCurrentPar = tnCurrent;
tnCurrent = tnCurrent.FirstNode;
}
else if (tnCurrent != tnCurrentPar.LastNode)
{
tnCurrent = tnCurrent.NextNode;
}
else
break;
}
while (tnCurrent != tnParent && tnCurrentPar != null && tnCurrent == tnCurrentPar.LastNode)
{
tnCurrent = tnCurrentPar;
tnCurrentPar = tnCurrentPar.Parent;
}
if (tnCurrent != tnParent)
tnCurrent = tnCurrent.NextNode;
}
return null;
}
/// <summary>
/// 遍历指定树节点所有子节点到集合(包括该结点)
/// </summary>
/// <param name="tnRoot">指定的树节点</param>
/// <returns></returns>
public static List<TreeNode> TraverseWithoutRecurtion(TreeNode tnRoot)
{
List<TreeNode> list = new List<TreeNode>();
TreeNode tnCurrent, tnParent;
list.Add(tnRoot);
tnParent = tnRoot;
tnCurrent = tnParent.FirstNode;
while (tnCurrent != null && tnCurrent != tnParent)
{
while (tnCurrent != null)
{
list.Add(tnCurrent);
if (tnCurrent.Nodes.Count > 0)
{
//子路径中最深的第一个子节点
tnParent = tnCurrent;
tnCurrent = tnCurrent.FirstNode;
}
else if (tnCurrent != tnParent.LastNode)
{
//下一个同级节点
tnCurrent = tnCurrent.NextNode;
}
else
break;
}
//获取父节点的下一个同级节点
while (tnCurrent != tnParent && tnParent != null && tnCurrent == tnParent.LastNode)
{
tnCurrent = tnParent;
tnParent = tnParent.Parent;
}
//下一个同级节点
if (tnCurrent != tnParent)
tnCurrent = tnCurrent.NextNode;
}
return list;
}
- TreeView 树的查找/遍历
- TreeView的子节点和父节点级联选中,遍历TreeView并查找和选定节点
- TreeView (树视图)遍历数据库的方法
- TreeView (树视图)遍历数据库的方法
- treeview的遍历
- 可双向并指定开始结点的treeview节点遍历查找方法类
- 如何在Windows Form控件TreeView中遍历和查找指定的CheckBox状态
- 二叉查找树的建立,遍历,查找
- 实现对TreeView的遍历
- TreeView遍历上面的CheckBoxs
- WPF TreeView的TreeViewItem查找
- 二叉树的遍历、查找
- 遍历树,获取TreeView中CheckBox的值
- C#中遍历TreeView并查找和选定节点
- C#中遍历TreeView并查找和选定节点
- 遍历TreeView
- 遍历 Treeview
- 遍历treeview
- (摘转)身体器官名称词汇
- wordpress禁用自动保存(autosave)和版本修订(revision)
- 日语中穿衣服的相关说法
- 四舍五入加千分位
- 《红楼梦》告诉我们
- TreeView 树的查找/遍历
- 设置容器的最小高度
- 技术性资源记录
- 初来乍到
- 常用可视化图形介绍
- Java中new String("abc")讲解
- svn服务器安装配置
- DOS 局域网常用命令集
- svn服务器安装配置