一元树 [英] N-ary Trees
问题描述
我有一组似乎适合N元树结构的分层数据,我似乎找不到以解析/存储/操作此数据的最佳方式的任何C#资源.
源数据(SQL表)由节点ID,节点名称和节点父节点组成,该节点ID为空(根节点)或引用了另一个节点ID.
有什么想法吗?
I’ve got a hierarchical set of data that seems to fit an N-ary tree structure, I can’t seem to find any good C# resources on the best way to parse/store/manipulate this data.
The source data (SQL table) consists of a node id, node name and node parent which is null (root node) or references another node id.
Any ideas?
推荐答案
树的常规数据结构应如下所示:
General data structure for tree should look like this:
TreeNode类
{
TreeNode父级;
List< TreeNode>子级;
数据数据; //节点中的任何数据
.........
//可能包含子集合的操纵函数
//暴露给客户端的
}
class TreeNode
{
TreeNode parent;
List<TreeNode> children;
Data data; // any data in thee node
...
// manupulation functions which can include childs collection
// exposed to client
}
Tree类
{
TreeNode根;
...
//树操作功能,例如添加,删除,查找枚举等.
}
class Tree
{
TreeNode root;
...
// tree manipulation functions like add, remove, find enumerate etc.
}
由于N元树没有二叉树的许多复杂功能,因此该类将比二叉树更简单-使用循环枚举每个节点的递归调用的子列表.
出于一般目的,使此类通用.通用参数是Data.
Since N-ary tree doesn't have many complicated features of binary tree, this class will be less complicated than binary tree - use loop to enumerate childs list with recursive call for every node.
For general purpose, make this class generic. Generic parameter is Data.
这篇关于一元树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!