C#树类型及其遍历
最近有个项目不仅需要取部门的层级关系,还要处理不规则的关系(移除某个部门),只有树结构才能实现相关遍历和操作。
涉及到的知识点:泛型、递归、数据结构
既然研究树类型就先来看下树的定义:
一棵树(tree)是由n(n>0)个元素组成的有限集合,其中:
(1)每个元素称为结点(node);
(2)有一个特定的结点,称为根结点或根(root);
(3)除根结点外,其余结点被分成m(m>=0)个互不相交的有限集合,而每个子集又都是一棵树(称为原树的子树);——百度
本文将简化树,只研究树的结点-结点树。结点树包含:父结点(根结点的父结点为null)、子结点(List集合)、数据对象。
类的设计:
public class BoTree<T>
{
public BoTree()
{
nodes = new List<BoTree<T>>();
} public BoTree(T data)
{
this.Data = data;
nodes = new List<BoTree<T>>();
} private BoTree<T> parent;
/// <summary>
/// 父结点
/// </summary>
public BoTree<T> Parent
{
get { return parent; }
}
/// <summary>
/// 结点数据
/// </summary>
public T Data { get; set; } private List<BoTree<T>> nodes;
/// <summary>
/// 子结点
/// </summary>
public List<BoTree<T>> Nodes
{
get { return nodes; }
}
/// <summary>
/// 添加结点
/// </summary>
/// <param name="node">结点</param>
public void AddNode(BoTree<T> node)
{
if (!nodes.Contains(node))
{
node.parent = this;
nodes.Add(node);
}
}
/// <summary>
/// 添加结点
/// </summary>
/// <param name="nodes">结点集合</param>
public void AddNode(List<BoTree<T>> nodes)
{
foreach (var node in nodes)
{
if (!nodes.Contains(node))
{
node.parent = this;
nodes.Add(node);
}
}
}
/// <summary>
/// 移除结点
/// </summary>
/// <param name="node"></param>
public void Remove(BoTree<T> node)
{
if (nodes.Contains(node))
nodes.Remove(node);
}
/// <summary>
/// 清空结点集合
/// </summary>
public void RemoveAll()
{
nodes.Clear();
}
}
测试:
首先创建一个学生类(任意)
public class Student
{
public Student(string name, string sex, int age)
{
this.Name = name;
this.Sex = sex;
this.Age = age;
}
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
初始化树:
BoTree<Student> tree1 = new BoTree<Student>();
tree1.Data = new Student("小波1", "男", ); BoTree<Student> tree2 = new BoTree<Student>();
tree2.Data = new Student("小波2", "男", ); BoTree<Student> tree3 = new BoTree<Student>();
tree3.Data = new Student("小波3", "男", ); BoTree<Student> tree4 = new BoTree<Student>();
tree4.Data = new Student("小波4", "男", ); tree1.AddNode(tree2);
tree1.AddNode(tree3);
tree3.AddNode(tree4);
调试:

可以从监视中看出tree1有2个子结点

可以看出tree4的父结点为tree3
下面我们来遍历这棵树:
public static void Recursive(BoTree<Student> tree)
{
Console.WriteLine("姓名:{0},姓名:{1},年龄:{2}", tree.Data.Name, tree.Data.Sex, tree.Data.Age);
if (tree.Nodes.Count > )
{
foreach (var item in tree.Nodes)
{
Recursive(item);
}
}
}
调用结果:

需要说明的是:不要尝试用Nodes.Add(T item)来添加结点,而是用AddNode方法来添加结点。AddNode方法将对Parent进行赋值,保证了父结点可查询
C#树类型及其遍历的更多相关文章
- 树和二叉树->遍历
文字描述 二叉树的先根遍历 若二叉树为空,则空操纵,否则 (1) 访问根结点 (2) 先根遍历左子树 (3) 先根遍历右子树 二叉树的中根遍历 若二叉树为空,则空操纵,否则 (1) 中根遍历左子树 ( ...
- 深入学习C#匿名函数、委托、Lambda表达式、表达式树类型——Expression tree types
匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
- List Leaves 树的层序遍历
3-树2 List Leaves (25 分) Given a tree, you are supposed to list all the leaves in the order of top do ...
- 树的深度优先遍历和广度优先遍历的原理和java实现代码
import java.util.ArrayDeque; public class BinaryTree { static class TreeNode{ int value; TreeNode le ...
- Tree(树的还原以及树的dfs遍历)
紫书:P155 uva 548 You are to determine the value of the leaf node in a given binary tree that is th ...
- 多级树的深度遍历与广度遍历(Java实现)
目录 多级树的深度遍历与广度遍历 节点模型 深度优先遍历 广度优先遍历 多级树的深度遍历与广度遍历 深度优先遍历与广度优先遍历其实是属于图算法的一种,多级树可以看做是一种特殊的图,所以多级数的深/广遍 ...
- Uva 122 树的层次遍历 Trees on the level lrj白书 p149
是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- hdu 4605 线段树与二叉树遍历
思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...
随机推荐
- laravel 5.4 导出excel表格
1.在laravel根目录下面找到一个composer.json文件 打开并在require下面加上一句话,如图所示: 注:以后这个excel的版本号可能还会修改 2.执行 composer inst ...
- phjp(ajax)作用知识点
1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...
- JVM内存布局
1. 概述 对于从事c和c++程序开发的开发人员来说,在内存管理领域,他们既拥有最高权力的”皇帝“又是从事最基础工作的”劳动人民“---既拥有每个对象的”所有权“,又担负着每个对象开始到终结的维护责任 ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- 51单片机和STM32单片机区别在那里
大部分朋友可能都知道51单片机和stm32单片机也知道一般入门会先学习51单片机在学习stm32单片机会简单一些,但是对于51单片机和stm32单片机的具体区别却不知道了,有些人觉得没必要,但是我个 ...
- 项目实战:负载均衡集群企业级应用实战—LVS详解
目录 一.负载均衡集群介绍 二.lvs 的介绍 三.LVS负载均衡四种工作模式 1.NAT工作模式 2.DR工作模式 3.TUN工作模式 4.full-nat 工作模式 5.四者的区别 四.LVS i ...
- MAVEN day04 SSH之分模块开发
一.创建父工程 1.选择>>"Maven Project"创建Maven工程.并且选择Packaging为 POM. 创建父工程主要是让子工程区继承父工程,减少冗余,多 ...
- python框架之Flask(4)-上下文管理
知识储备 偏函数 作用 偏函数,帮助开发者自动传递参数. 使用 import functools def index(a1, a2): return a1 + a2 # 原来的调用方式 # ret = ...
- https://github.com/tensorflow/models/blob/master/research/slim/datasets/preprocess_imagenet_validation_data.py 改编版
#!/usr/bin/env python # Copyright 2016 Google Inc. All Rights Reserved. # # Licensed under the Apach ...
- require 和 import 区别
我很懵逼啊 都是在引用模块,那到底用哪个? 参考:https://www.cnblogs.com/hwldyz/p/9145959.html 看来会明白一点的 首先这两个都是为了JS模块化编程使用. ...