如何将architectureid列表转换为二叉树 [英] How to transform list of hierarchyid into a binary tree

查看:90
本文介绍了如何将architectureid列表转换为二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如下的多层次营销(二进制):

I am working on a multi-level marketing (binary) which looks like this:

(但是不要求二叉树是完美的.一个节点可以有0-2个子节点)

(but the binary tree is not required to be perfect. A node can have 0-2 child)

我的问题是我从数据库中获取的数据是平面列表.

My problem is the data that I fetch from the database is flat list.

请注意,我正在使用层次结构(SQL Server 2014)

Notice that I am using hierarchyid (sql server 2014)

基本上TextNode列就像一个面包屑.

Basically the TextNode column is like a breadcrumb.

每个斜线/代表level.

如果我以/1/的TextNode作为根.那么每个以/1/开头的节点都属于该根,分别是/1//1/1//1/1/1/(包括根节点,该节点将为0级)

If I have TextNode of /1/ as root. then every node that starts with /1/ belongs to that root which are /1/, /1/1/ and /1/1/1/ (the root node is included which will be the level 0)

我已经尝试过接受了该问题的答案,但无效.

I've tried the accepted answer in this question but its not working.

如何将平面列表转换为二叉树,以便可以轻松遍历并将其显示在屏幕上?

How can I transform the flatlist to a Binary Tree so that I can easily traverse and display it on a screen?

如果有必要,我正在使用C#,ASP MVC 5,SQL Server 2014.

Im using C#, ASP MVC 5, SQL Server 2014 if it matters.

推荐答案

我根据Alex的实现完全实现了此代码,但是正如在某些情况下提到的那样,它无法正常工作..请看一下我的Image和我的代码(摘自Alex帖子)[数据库中的数据正确,但在树状视图中似乎存在一些问题]

I implement exactly this code According to Alex implementation but as is mentioned in some case it didn't work correctly .. have a look to my Image and my code (which copied from Alex post) [data in the database are correct but in tree view seems some problems ]

public class Row : IRow<string>
{
    public string TextNode { get; }
    public string Value { get; }
    public long Id { get; }
    public string FIN { get; }
    public Row(string textNode, string userName, long id, string fin)
    {
        FIN = fin;
        Id = id;
        TextNode = textNode;
        Value = userName;
    }
}

public interface IRow<out T>
{
    string TextNode { get; }
    long Id { get; }
    string FIN { get; }
    T Value { get; }
}

public class TreeNode<T>
{
    private struct NodeDescriptor
    {
        public int Level { get; }
        public int ParentIndex { get; }

        public NodeDescriptor(IRow<T> row)
        {
            var split = row.TextNode.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            Level = split.Length;
            ParentIndex = split.Length > 1 ? int.Parse(split[split.Length - 2]) - 1 : 0;
        }
    }
    public T title { get; }
    public long Id { get; }
    public string FIN { get; }
    public List<TreeNode<T>> children { get; }

    private TreeNode(T value, long id, string fin)
    {
        Id = id;
        FIN = fin;
        title = value;
        children = new List<TreeNode<T>>();
    }

    public static TreeNode<T> Parse(IReadOnlyList<IRow<T>> rows)
    {
        if (rows.Count == 0)
            return null;
        var result = new TreeNode<T>(rows[0].Value, rows[0].Id, rows[0].FIN);
        FillParents(new[] { result }, rows, 1, 1);
        return result;
    }

    private static void FillParents(IList<TreeNode<T>> parents, IReadOnlyList<IRow<T>> rows, int index, int currentLevel)
    {
        var result = new List<TreeNode<T>>();
        for (int i = index; i < rows.Count; i++)
        {
            var descriptor = new NodeDescriptor(rows[i]);
            if (descriptor.Level != currentLevel)
            {
                FillParents(result, rows, i, descriptor.Level);
                return;
            }
            var treeNode = new TreeNode<T>(rows[i].Value, rows[i].Id, rows[i].FIN);
            parents[descriptor.ParentIndex].children.Add(treeNode);
            result.Add(treeNode);
        }
    }
}

g

这也是我的JSON输出,以获取更多信息:

this is also my JSON output for more information :

{"title":"Earth","Id":32,"FIN":"FIN","children":[{"title":"Europe","Id":33,"FIN":"FIN001","children":[{"title":"France","Id":35,"FIN":"FIN001001","children":[{"title":"Paris","Id":36,"FIN":"FIN001001001","children":[]},{"title":"Brasilia","Id":41,"FIN":"FIN002001001","children":[]},{"title":"Bahia","Id":42,"FIN":"FIN002001002","children":[]}]},{"title":"Spain","Id":38,"FIN":"FIN001002","children":[{"title":"Madrid","Id":37,"FIN":"FIN001002001","children":[{"title":"Salvador","Id":43,"FIN":"FIN002001002001","children":[]}]}]},{"title":"Italy","Id":45,"FIN":"FIN001003","children":[]},{"title":"Germany","Id":48,"FIN":"FIN001004","children":[]},{"title":"test","Id":10049,"FIN":"FIN001005","children":[]}]},{"title":"South America","Id":34,"FIN":"FIN002","children":[{"title":"Brazil","Id":40,"FIN":"FIN002001","children":[{"title":"Morano","Id":47,"FIN":"FIN001003001","children":[]}]}]},{"title":"Antarctica","Id":39,"FIN":"FIN003","children":[{"title":"McMurdo Station","Id":44,"FIN":"FIN003001","children":[]}]}]}

这篇关于如何将architectureid列表转换为二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆