Skip to main content
Tweeted twitter.com/StackCodeReview/status/1042518977992445952
added 37 characters in body
Source Link
A.Learn
  • 53
  • 1
  • 6
public interface IDbEntityNode
{
     int Id { get; set; }
     int ParentId { get; set; }
     string Data { get; set; } 
}
public interface IDbEntityNode
{
     int Id { get; set; }
     int ParentId { get; set; }
}
public interface IDbEntityNode
{
     int Id { get; set; }
     int ParentId { get; set; }
     string Data { get; set; } 
}
added 6 characters in body
Source Link
A.Learn
  • 53
  • 1
  • 6

Root node has IdParentId set to 0

private static bool IsRootNode(IDbEntityNode dbEntity)
{
    bool isRootNode = false;
    if (dbEntity.IdParentId == 0 )
        isRootNode = true;
    return isRootNode;              
}

Root node has Id set to 0

private static bool IsRootNode(IDbEntityNode dbEntity)
{
    bool isRootNode = false;
    if (dbEntity.Id == 0 )
        isRootNode = true;
    return isRootNode;              
}

Root node has ParentId set to 0

private static bool IsRootNode(IDbEntityNode dbEntity)
{
    bool isRootNode = false;
    if (dbEntity.ParentId == 0 )
        isRootNode = true;
    return isRootNode;              
}
edited title and tags; fixed copy/paste indentation
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

Possible code improvements for generic Generic tree data structure c#for flat data from a database

public interface IDbEntityNode
    {
         int Id { get; set; }
         int ParentId { get; set; }
    }
 public class ExceptionCategory :IDbEntityNode
    {
        public  int Id { get; set; }
        public  int ParentId { get; set; }
        public string Data { get; set; }      
        public ExceptionCategory(string data, int id, int parentId)
        {
            Id = id;
            ParentId = parentId;
            Data = data;
        }
    }
public class GenericNode<T> 
    {
        public T NodeInformation { get; set; }
        public GenericNode<T> Parent { get; set; }
        public List<GenericNode<T>> Children { get; set; } = new List<GenericNode<T>>();
    }
public static List<GenericNode<T>> CreateGenericTree<T>(List<T> flatDataObject,Func<T,bool> IsRootNode) where T : IDbEntityNode            
        {
            var lookup = new Dictionary<int, GenericNode<T>>();
            var rootNodes = new List<GenericNode<T>>();
            var noOfElements = flatDataObject.Count;
            for (int element = 0; element < noOfElements; element++)
            {
                GenericNode<T> currentNode;
                if (lookup.TryGetValue(flatDataObject[element].Id, out currentNode))
                {
                    currentNode.NodeInformation = flatDataObject[element];
                }
                else
                {
                    currentNode = new GenericNode<T>() { NodeInformation = flatDataObject[element] };
                    lookup.Add(flatDataObject[element].Id, currentNode);
                }

                if (IsRootNode(flatDataObject[element])) 
                {
                    rootNodes.Add(currentNode);
                }
                else
                {
                    GenericNode<T> parentNode;
                    if (!lookup.TryGetValue(flatDataObject[element].ParentId, out parentNode))
                    {   
                        parentNode = new GenericNode<T>();
                        lookup.Add(flatDataObject[element].ParentId, parentNode);
                    }
                    parentNode.Children.Add(currentNode);
                    currentNode.Parent = parentNode;
                }
            }
            
            return rootNodes;
        }
private static void Main(string[] args)
        {
            List<IDbEntityNode> flatDataStructure = new List<IDbEntityNode>
            {
                new ExceptionCategory("System Exception",1,0),
                new ExceptionCategory("Index out of range",2,1),
                new ExceptionCategory("Null Reference",3,1),
                new ExceptionCategory("Invalid Cast",4,1),
                new ExceptionCategory("OOM",5,1),
                new ExceptionCategory("Argument Exception",6,1),
                new ExceptionCategory("Argument Out Of Range",7,6),
                new ExceptionCategory("Argument Null",8,6),
                new ExceptionCategory("External Exception",9,1),
                new ExceptionCategory("Com",10,9),
                new ExceptionCategory("SEH",11,9),
                new ExceptionCategory("Arithmatic Exception",12,1),
                new ExceptionCategory("DivideBy0",13,12),
                new ExceptionCategory("Overflow",14,12),
            };

            var tree = CreateGenericTree(flatDataStructure, IsRootNode);
        }
 private static bool IsRootNode(IDbEntityNode dbEntity)
        {
            bool isRootNode = false;
            if (dbEntity.Id == 0 )
                isRootNode = true;
            return isRootNode;              
        }

Possible code improvements for generic tree data structure c#

public interface IDbEntityNode
    {
         int Id { get; set; }
         int ParentId { get; set; }
    }
 public class ExceptionCategory :IDbEntityNode
    {
        public  int Id { get; set; }
        public  int ParentId { get; set; }
        public string Data { get; set; }      
        public ExceptionCategory(string data, int id, int parentId)
        {
            Id = id;
            ParentId = parentId;
            Data = data;
        }
    }
public class GenericNode<T> 
    {
        public T NodeInformation { get; set; }
        public GenericNode<T> Parent { get; set; }
        public List<GenericNode<T>> Children { get; set; } = new List<GenericNode<T>>();
    }
public static List<GenericNode<T>> CreateGenericTree<T>(List<T> flatDataObject,Func<T,bool> IsRootNode) where T : IDbEntityNode            
        {
            var lookup = new Dictionary<int, GenericNode<T>>();
            var rootNodes = new List<GenericNode<T>>();
            var noOfElements = flatDataObject.Count;
            for (int element = 0; element < noOfElements; element++)
            {
                GenericNode<T> currentNode;
                if (lookup.TryGetValue(flatDataObject[element].Id, out currentNode))
                {
                    currentNode.NodeInformation = flatDataObject[element];
                }
                else
                {
                    currentNode = new GenericNode<T>() { NodeInformation = flatDataObject[element] };
                    lookup.Add(flatDataObject[element].Id, currentNode);
                }

                if (IsRootNode(flatDataObject[element])) 
                {
                    rootNodes.Add(currentNode);
                }
                else
                {
                    GenericNode<T> parentNode;
                    if (!lookup.TryGetValue(flatDataObject[element].ParentId, out parentNode))
                    {   
                        parentNode = new GenericNode<T>();
                        lookup.Add(flatDataObject[element].ParentId, parentNode);
                    }
                    parentNode.Children.Add(currentNode);
                    currentNode.Parent = parentNode;
                }
            }
            
            return rootNodes;
        }
private static void Main(string[] args)
        {
            List<IDbEntityNode> flatDataStructure = new List<IDbEntityNode>
            {
                new ExceptionCategory("System Exception",1,0),
                new ExceptionCategory("Index out of range",2,1),
                new ExceptionCategory("Null Reference",3,1),
                new ExceptionCategory("Invalid Cast",4,1),
                new ExceptionCategory("OOM",5,1),
                new ExceptionCategory("Argument Exception",6,1),
                new ExceptionCategory("Argument Out Of Range",7,6),
                new ExceptionCategory("Argument Null",8,6),
                new ExceptionCategory("External Exception",9,1),
                new ExceptionCategory("Com",10,9),
                new ExceptionCategory("SEH",11,9),
                new ExceptionCategory("Arithmatic Exception",12,1),
                new ExceptionCategory("DivideBy0",13,12),
                new ExceptionCategory("Overflow",14,12),
            };

            var tree = CreateGenericTree(flatDataStructure, IsRootNode);
        }
 private static bool IsRootNode(IDbEntityNode dbEntity)
        {
            bool isRootNode = false;
            if (dbEntity.Id == 0 )
                isRootNode = true;
            return isRootNode;              
        }

Generic tree data structure for flat data from a database

public interface IDbEntityNode
{
     int Id { get; set; }
     int ParentId { get; set; }
}
public class ExceptionCategory :IDbEntityNode
{
    public  int Id { get; set; }
    public  int ParentId { get; set; }
    public string Data { get; set; }      
    public ExceptionCategory(string data, int id, int parentId)
    {
        Id = id;
        ParentId = parentId;
        Data = data;
    }
}
public class GenericNode<T> 
{
    public T NodeInformation { get; set; }
    public GenericNode<T> Parent { get; set; }
    public List<GenericNode<T>> Children { get; set; } = new List<GenericNode<T>>();
}
public static List<GenericNode<T>> CreateGenericTree<T>(List<T> flatDataObject,Func<T,bool> IsRootNode) where T : IDbEntityNode            
{
    var lookup = new Dictionary<int, GenericNode<T>>();
    var rootNodes = new List<GenericNode<T>>();
    var noOfElements = flatDataObject.Count;
    for (int element = 0; element < noOfElements; element++)
    {
        GenericNode<T> currentNode;
        if (lookup.TryGetValue(flatDataObject[element].Id, out currentNode))
        {
            currentNode.NodeInformation = flatDataObject[element];
        }
        else
        {
            currentNode = new GenericNode<T>() { NodeInformation = flatDataObject[element] };
            lookup.Add(flatDataObject[element].Id, currentNode);
        }

        if (IsRootNode(flatDataObject[element])) 
        {
            rootNodes.Add(currentNode);
        }
        else
        {
            GenericNode<T> parentNode;
            if (!lookup.TryGetValue(flatDataObject[element].ParentId, out parentNode))
            {   
                parentNode = new GenericNode<T>();
                lookup.Add(flatDataObject[element].ParentId, parentNode);
            }
            parentNode.Children.Add(currentNode);
            currentNode.Parent = parentNode;
        }
    }
    
    return rootNodes;
}
private static void Main(string[] args)
{
    List<IDbEntityNode> flatDataStructure = new List<IDbEntityNode>
    {
        new ExceptionCategory("System Exception",1,0),
        new ExceptionCategory("Index out of range",2,1),
        new ExceptionCategory("Null Reference",3,1),
        new ExceptionCategory("Invalid Cast",4,1),
        new ExceptionCategory("OOM",5,1),
        new ExceptionCategory("Argument Exception",6,1),
        new ExceptionCategory("Argument Out Of Range",7,6),
        new ExceptionCategory("Argument Null",8,6),
        new ExceptionCategory("External Exception",9,1),
        new ExceptionCategory("Com",10,9),
        new ExceptionCategory("SEH",11,9),
        new ExceptionCategory("Arithmatic Exception",12,1),
        new ExceptionCategory("DivideBy0",13,12),
        new ExceptionCategory("Overflow",14,12),
    };

    var tree = CreateGenericTree(flatDataStructure, IsRootNode);
}
private static bool IsRootNode(IDbEntityNode dbEntity)
{
    bool isRootNode = false;
    if (dbEntity.Id == 0 )
        isRootNode = true;
    return isRootNode;              
}
edited title
Source Link
A.Learn
  • 53
  • 1
  • 6
Loading
Source Link
A.Learn
  • 53
  • 1
  • 6
Loading