TreeBuilder科学的树创建器
Posted Timeless
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TreeBuilder科学的树创建器相关的知识,希望对你有一定的参考价值。
public static class TreeBuilder { public static List<dynamic> Build(IEnumerable<dynamic> collection, Func<dynamic, int, dynamic> parser, Func<dynamic, bool> rootSelector, Func<dynamic, dynamic, int, bool> childrenSelector) { if (collection != null && parser != null && rootSelector != null && childrenSelector != null) { var roots = collection.Where(rootSelector).Select(item => parser(item, 0)).ToList(); roots.ForEach(node => { BuildChildren(collection, node, parser, 1, childrenSelector); }); return roots; } return null; } private static void BuildChildren(IEnumerable<dynamic> collection, dynamic parent, Func<dynamic, int, dynamic> parser, int level, Func<dynamic, dynamic, int, bool> childrenSelector) { var children = collection.Where(t => childrenSelector(t, parent, level)).Select(item => parser(item, level)).ToList(); if (children != null && children.Count > 0) { parent.state="closed"; parent.children=children; children.ForEach(node => { BuildChildren(collection, node, parser, level + 1, childrenSelector); }); } } }
示例:
string sql = @"SELECT [Id] ,[PId] ,[Name] ,[OfficeTel] ,[Fax] ,[SafeTel] ,[MSTel] ,[ElectricTel] ,[Order] ,[Type] FROM [NEMS].[dbo].[ContactInfo] where [type][email protected] order by [Order] "; BatchDataHelper helper = new BatchDataHelper("NEMSConnString"); var list = helper.Query(sql, new List<KeyValuePair<string, object>>() { new KeyValuePair<string, object>("type", type) }); var tree = TreeBuilder.Build(list, (d, i) => { d.Level = i; return d; }, d => d.PId == 0, (d, n, i) => n.Id == d.PId);
参数一为构件树的数据集合,参数二为树节点转化委托,参数三为根节点选择器,参数四是父子关系选择器.
以上是关于TreeBuilder科学的树创建器的主要内容,如果未能解决你的问题,请参考以下文章
python 用于数据探索的Python代码片段(例如,在数据科学项目中)
在 Perl HTML::TreeBuilder 中获取/设置 innerHTML?
AttributeError:类型对象“sklearn.tree._tree.TreeBuilder”没有属性“__reduce_cython__”