snippetcsharpMinor
Create a tree recursivly
Viewed 0 times
recursivlytreecreate
Problem
I want to create a Treeview in C# which will group file by prefix (here the prefix is a marked by the separator
Files list:
Corresponding tree:
And how I do it:
` private GraphUINode(List subNodes, GraphUINode parent, string name, int lvl = 0)
: base(parent.m_viewDataSubControl)
{
parent.Nodes.Add(this);
this.Name = name;
this.Text = name;
string currentPrefix = "";
int pertinentSubNodes = 0;
while (pertinentSubNodes children = new List();
foreach (string child in subNodes)
{
// The child is in the same group than the previous one
if (child.StartsWith(currentPrefix))
{
children.Add(child);
}
else
{
// Create a node only if needed
if (children.Count > 1)
{
// Create the new node
new GraphUINode(children, this, currentPrefix, lvl + 1);
children.Clear();
children.Add(child);
}
else
{
if (children.Count == 1)
{
new GraphTemplateNode(this, m_viewDataSubControl, children[0]);
children.Clear();
}
new GraphTemplateNode(this, m_viewDataSubControl, child);
}
currentPrefix = "";
for (int i = 0; i
but this looks very dirty. How can I improve it?
_). The following files should give this tree:Files list:
p_a
p_a_test
p_LIG
p_p
p_p_c
p_p_c2
p_p_ccc
p_p_test
p_tres
TestLineGraph1
TestLineGrpah
Corresponding tree:
|--p_
|--p_a
|--p_a_test
|--p_LIG
|--p_p
|--p_p_
|--p_p_c
|--p_p_c2
|--p_p_ccc
|--p_p_test
|--p_tres
TestLineGraph1
TestLineGraph
And how I do it:
` private GraphUINode(List subNodes, GraphUINode parent, string name, int lvl = 0)
: base(parent.m_viewDataSubControl)
{
parent.Nodes.Add(this);
this.Name = name;
this.Text = name;
string currentPrefix = "";
int pertinentSubNodes = 0;
while (pertinentSubNodes children = new List();
foreach (string child in subNodes)
{
// The child is in the same group than the previous one
if (child.StartsWith(currentPrefix))
{
children.Add(child);
}
else
{
// Create a node only if needed
if (children.Count > 1)
{
// Create the new node
new GraphUINode(children, this, currentPrefix, lvl + 1);
children.Clear();
children.Add(child);
}
else
{
if (children.Count == 1)
{
new GraphTemplateNode(this, m_viewDataSubControl, children[0]);
children.Clear();
}
new GraphTemplateNode(this, m_viewDataSubControl, child);
}
currentPrefix = "";
for (int i = 0; i
but this looks very dirty. How can I improve it?
Solution
Is this a bug ?
The correspondending tree you showed is not consistent. It should look like
Style
-
You should always be consistent with the coding style you use. So if you omit (
-
You shouldn't shorten variable / parameter names because that removes readability. ->
Comments
Comments should describe why something is done. What is done should be described by the code itself by using meaningful names for classes, methods, parameters.
A comment which describes what is done like
should be removed.
Refactoring
Can't be done until the possibility of the bug as mentioned is cleared.
As the root problem to solve is to create a tree structure based on a
The correspondending tree you showed is not consistent. It should look like
|--p_
|--p_a
|--p_a_
|--p_a_test <- this
|--p_LIG
|--p_p
|--p_p_
|--p_p_c
|--p_p_c2
|--p_p_ccc
|--p_p_test
|--p_tres
TestLineGraph1
TestLineGraphStyle
-
You should always be consistent with the coding style you use. So if you omit (
-
You shouldn't shorten variable / parameter names because that removes readability. ->
lvl Comments
Comments should describe why something is done. What is done should be described by the code itself by using meaningful names for classes, methods, parameters.
A comment which describes what is done like
// Create the new node
new GraphUINode(children, this, currentPrefix, lvl + 1);should be removed.
Refactoring
Can't be done until the possibility of the bug as mentioned is cleared.
As the root problem to solve is to create a tree structure based on a
List I would extract this tree creation outside of the constructor to a separate method which only takes a List as input parameter and is returning something like class TreeNode
{
public String Name { get; private set; }
public IList ChildNodes { get; private set; }
public TreeNode()
{
ChildNodes = new List();
}
public void FillChildNodes(IList childNodes)
{
}
}Code Snippets
|--p_
|--p_a
|--p_a_
|--p_a_test <- this
|--p_LIG
|--p_p
|--p_p_
|--p_p_c
|--p_p_c2
|--p_p_ccc
|--p_p_test
|--p_tres
TestLineGraph1
TestLineGraph// Create the new node
new GraphUINode(children, this, currentPrefix, lvl + 1);class TreeNode
{
public String Name { get; private set; }
public IList<TreeNode> ChildNodes { get; private set; }
public TreeNode()
{
ChildNodes = new List<TreeNode>();
}
public void FillChildNodes(IList<String> childNodes)
{
}
}Context
StackExchange Code Review Q#73837, answer score: 4
Revisions (0)
No revisions yet.