Representing a chain of nodes in a human readable way - c#

I'm trying to represent a subway map type thing that has to be drawn progressively (like its growing).
My code all works perfectly but its unreadable. Basically it's a tree structure with recursive nodes and subnodes and my test code looks like this:
Children.Add(new TrackLine(800));
Children[0].Children.Add(new TrackSpot());
Children[0].Children[0].Children.Add(new TrackSplitter());
Children[0].Children[0].Children[0].Children.Add(new TrackRotate(-45));
Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackColorChange(Color.Red));
Children[0].Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackLine(100));
Children[0].Children[0].Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackRotate(45));
Children[0].Children[0].Children[0].Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackLine(200));
Does anyone have any suggestions of how to fix that mess?

You're looking for a way to add it to the deepest child that has no children of it's own?
class Node
{
List<Node> children ;
public void addNode( Node newNode )
{
if( children.Count > 0 )
children[0].addNode( newNode ) ; // recursive call
// to ask first child to add newNode to it
else
children.Add( newNode ) ; // just add it to the children list of THIS node
}
}

To make that code more readable I'd use variables with appropriate names (since I don't know your subject domain, mine are probably not appropriate):
Children.Add(new TrackLine(800));
var lineA = Children[0];
lineA.Children.Add(new TrackSpot());
var spotA = lineA.Children[0];
spotA.Children.Add(new TrackSplitter());
var splitterA = spotA.Children[0];
splitterA.Children.Add(new TrackRotate(-45));
var rotateNeg45 = splitterA.Children[0];
rotateNeg45.Children.Add(new TrackColorChange(Color.Red));
var colorRed = rotateNeg45.Children[0];
colorRed.Add(new TrackLine(100));
var lineB = colorRed.Children[0];
lineB.Children.Add(new TrackRotate(45));
var rotatePos45 = lineB.Children[0];
rotatePos45.Children.Add(new TrackLine(200));
var lineC = rotatePos45.Children[0];
Another approach is to use strings as indexes for Children like this:
Children.Add(new TrackLine(800));
Children["lineA"].Children.Add(new TrackSpot());
Children["lineA"].Children["spotA"].Children.Add(new TrackSplitter());
However I probably would not do it because managing the string constants would be a mess on its own and it would probably require changing Children implementation.

Related

Loop to iterate up parent nodes until it finds specific tag

What I'm doing is finding a specific value within an XML document and then I want to iterate upwards through each parent until it finds the parent with a specific tag name.
List<XElement> fieldReferences = new List<XElement>();
fieldReferences.AddRange(element.XPathSelectElements(string.Format("descendant::nameValue[#idref='{0}']", fieldName)));
fieldReferences.AddRange(element.XPathSelectElements(string.Format("descendant::nameValue[#value='{0}']", fieldName)));
string parentIterator = ".Parent";
string attributeValue = ".Attribute('id').Value";
string parentElementName = ".Name";
foreach (var value in fieldReferences)
{
var parentField = string.Format("{0}{1}", parentIterator, parentElementName);
while (value + parentField != "private" || value + parentField != "public")
{
// keep appending .Parent until it finds what it needs
}
//var parentField = value.Parent.Parent.Parent.Parent.Attribute("id").Value;
outputFields.Add(parentField, name.FirstOrDefault());
}
The issue that I'm having is that parentField will always be evaluated as a string so it'll never actually check the .Parent.Name property of value.
I don't work often with C# so I'm sure there's a much easier way to do this so my question is: How can I get my parentField string to evaluate the way I want OR how can I do this in a different way to achieve the same end result?
EDIT: Here's what my xml looks like. The XPAthSelectElement gets the nameValue element and I want to iterate through each parent element until I find the private tag
<private id="I need to iterate upwards through each parent element until I find this one">
<value>
<request>
<nameValues>
<nameValue idref="I found this" />
<nameValue value=""/>
</nameValues>
</request>
</value>
</private>
So you don't actually need to do this many string operations to then go crazy with XPath. Once you found your child target element, you can just use the Parent property on the XElement iteratively until you find the XElement with a private/public tag. So that gives us this code:
foreach (XElement element in fieldReferences)
{
XElement currentElement = element;
while (currentElement.Parent != null)
{
currentElement = currentElement.Parent;
if (currentElement.Name == "private" || currentElement.Name == "public") {
outputFields.Add(currentElement, /* not sure what you want here */);
break;
}
}
}
So currentElement would start out as the element with the nameValue tag from your example. In the while loop, each iteration currentElement changes to its parent node until there is no more parent or currentElement has become a private or a public tag. If the latter is the case, it gets appended to your result.
You can use the XElement.Ancestors function to get a list of all the elements that contain the nodes you found, then just select the ones you want using LINQ. No need for any loops.
var results = fieldReferences.Select(element => element.Ancestors()
.Where(ancestor => ancestor.Name == "public" ||
ancestor.Name == "private")
.FirstOrDefault());
Note that this will go all the way up the tree, and may have issues if there are multiple matching ancestors (or no matching ancestor). Let me know if that is a problem for you, and what result you want in that case, and I can make adjustments.

Remove all the JTokens except for a specific path

Problem
Let's say I have:
var json = JObject.Parse("imagine some large JSON here");
Now let's do this:
var subtree = json.SelectNode("root.child.grandson");
I care only about:
The grandson
All its descendants
Ancestors in a straight line to the root (parents, their parents, etc...)
Some solution
Great. So I came up with this method:
private static void KillMySiblings(JToken token)
{
if (token.Root == token)
return;
var siblingsAndSelf = token.Parent.Children().ToArray();
foreach (var sibling in siblingsAndSelf)
{
if (ReferenceEquals(sibling, token))
continue;
// this removes the token from its parent
sibling.Remove();
}
}
which with this code:
var ancestors = subtree.AncestorsAndSelf();
foreach (var ancestor in ancestors)
KillMySiblings(ancestor);
does the trick.
So why u postin'?
Because I don't like my solution and am seeking something simpler in concept and more efficient. I don't like the idea of going through potentially O(n) nodes and removing them. I feel I should extract those parents and build a new tree from them - but I don't know how to do this properly.
Any help would be greatly appreciated! :D

Remove Child Nodes from Parent Node

Hello I currently have a TreeView with the following structure:
Root
Child
Root
Child
Root
Child
Child
RootN
ChildN
The TreeView structure can basically have NRootNodes - NChildren and the NRootNodes can have NRoots and NChildren so basically just like Windows Explorer Window.
My current issue that I have is that I have to get all the Parents or Root, in this case Roots / RootN and then I have to Remove all of their Child Nodes, in this case Child / ChildN. In the end I have to have only the Parent Nodes and then Clone them so I can move them to a different location within the TreeView.
RootNodes have a unique Tag - Folder and ChildNodes have another unique Tag - Calculations, as I have said earlier, I have to get rid of all Calculations in the Selected Node so only the Structure of that Selected Node will Remain.
Basically in the end I have to have something like this:
Root
Root
Root
Root
Root
I have a recursive method that "scans" the SelectedNode and gets all the Parents:
public List<TreeNode> CollectParentNodes(TreeNodeCollection parentCollection, List<TreeNode> collectedNodes)
{
foreach (TreeNode node in parentCollection)
{
if (!collectedNodes.Contains(node.Parent))
{
collectedNodes.Add(node.Parent);
parentNodeAdded = true;
}
if (node.Level != 0 && node.Tag.ToString() != Enumerations.NodeType.Calculation.ToString())
collectedNodes.Add(node);
if (node.Nodes.Count > 0)
CollectParentNodes(node.Nodes, collectedNodes);
}
parentNodeAdded = false;
return collectedNodes;
}
In the end I have a List that will hold all the Parents but the problem I'm facing is that that Parents also contain their descendents, in this case the Calculations
I have searched Google and StackOverFlow but I could not find anything of help, I appologize in advance if this has already been answered.
Thank you.
You can create an extension method GetAllNodes for TreeView that return List
Remember using using System.Linq; at top of your code
public static class Extensions
{
public static List<TreeNode> GetAllNodes(this TreeView tree)
{
var firstLevelNodes = tree.Nodes.Cast<TreeNode>();
return firstLevelNodes.SelectMany(x => GetNodes(x)).Concat(firstLevelNodes).ToList();
}
private static IEnumerable<TreeNode> GetNodes(TreeNode node)
{
var nodes = node.Nodes.Cast<TreeNode>();
return nodes.SelectMany(x => GetNodes(x)).Concat(nodes);
}
}
And the usage will be:
var result = this.treeView1.GetAllNodes().Where(x => x.Tag == "FOLDER").ToList();
Remember to add namespace of your extensions class at top of your code wherever you want to use it.
As an example you can set All nodes with tag of Folder to be in Red forecolor:
var result = this.treeView1.GetAllNodes().Where(x => (x.Tag as string) == "FOLDER").ToList();
result.ForEach(x => x.ForeColor = Color.Red);
And here is an Screenshot
This will create a new tree with the selected node as root and which child nodes consists only of nodes that are tagged "Folder".
You need to create a copy constructor (or extension method) to deep copy the node to prevent the manipulation on the node objects to impact your original tree source:
public TreeNode CollectFolderChildNodes(TreeNode selectedNode)
{
if (selectedNode.Tag == "Calculation")
return null;
// Get all the children that are tagged as folder
var childRootNodes = selectedNode.Children.Where((childNode) => childNode.Tag == "Folder";
// Clone root node using a copy constructor
var newRoot = new TreeNode(selectedNode);
newRoot.Children.Clear();
foreach (var childNode in childRootNodes)
{
// Iterate over all children and add them to the new tree
if (childNode.Children.Any())
{
// Repeat steps for the children of the current child.
// Recursion stops when the leaf is reached
newRoot.Children.Add(CollectFolderChildNodes(childNode));
}
else
{
// The current child item is leaf (no children)
newRoot.Children.Add(new TreeNode(childNode));
}
}
return newRoot;
}
I think this should do it, but I didn't tested it. But maybe at least the idea behind it is clear.
But as I mentioned before, maybe it's better to traverse the tree (using same ItemsSource) and set a property (e.g. IsHidingCalculations) to true so that only the folders will show up. You would need to implement an ItemsStyle and use a trigger that sets the items Visibility to Collapsed when your IsHidingCalculations evaluates to true.
To clone a node without its children you can create an extension method like this:
public static TreeNode CloneWithoutChildren(this TreeNode node)
{
return new TreeNode(node.Text, node.ImageIndex, node.SelectedImageIndex)
{
Name = node.Name,
ToolTipText = node.ToolTipText,
Tag = node.Tag,
Checked = node.Checked
}
}
and then:
collectedNodes.Add(node.CloneWithoutChildren());

Replacing recursive with loop

I am working on an ASP.Net page, and there is tree view in it. In the tree view some nodes have nested nodes like branches. I have data in a list of custom objects in the following format:
Id, Description, parentId
Right now, I am using a function to recursively add nodes to the tree view. The following is code snippet:
private bool findParentAddNode(string id, string description, string parentid, ref List<CustomTreeNode> treeList)
{
bool isFound = false;
foreach (CustomTreeNode node in treeList)
{
if (node.id == parentid)//if current node is parent node, add in it as its child
{
node.addChild(id, description, parentid);
isFound = true;
break;
}
else if (node.listOfChildNodes != null)//have child nodes
{
isFound = findParentAddNode(id, description, parentid, ref node.listOfChildNodes);
if (isFound)
break;
}
}
return isFound;
}
The above technique works well but, for more then 30K nodes, its performance is slow. Please suggest an algorithm to replace this recursive call with loops.
As it recurses down the tree, the code is doing a linear search over the lists of child nodes.
This means that for randomly distributed parent ids, after adding N nodes to the tree it will on average search N/2 nodes for the parent before adding the N+1th node. So the cost will be O(N²) on the number of nodes.
Instead of a linear scan, create an index of id to node and use that to find the parent quickly. When you create a node and add it to the tree, also add it to a Dictionary<int,CustomTreeNode>. When you want to add a node to parent, find the parent in the index and add it. If addChild returns the child it creates, then the code becomes:
Dictionary<int,CustomTreeNode> index = new Dictionary<int,CustomTreeNode>();
private bool findParentAddNode(string id, string description, string parentid)
{
if ( !nodeIndex.TryGetValue ( parentid, out parentNode ) )
return false;
index[id] = parentNode.addChild(id, description, parentid);
return true;
}
You will need to add the root of the tree to the index before using findParentAddNode.
An iterative version of a breadth-first search should be something like the following:
var rootNodes = new List<CustomTreeNode> { new CustomTreeNode() };
while (rootNodes.Count > 0) {
var nextRoots = new List<CustomTreeNode>();
foreach (var node in rootNodes) {
// process node here
nextRoots.AddRange(node.CustomTreeNode);
}
rootNodes = nextRoots;
}
That said, this isn't tested, and since it's a BFS, isn't optimal w/r/t memory. (Memory use is O(n), not O(log n) compared to DFS or iterative-deepening DFS.)
You can return data in xml format from sql server database using for xml
then bind it to treeview control.

TreeNode key issue

I am using TreeView control in WinForm.
I am trying to use the following code, but getting "NullReferenceException".
I am following the syntax provided i.e. tree.Nodes[key].Nodes.Add(key,text)
I don't know whats wrong with the code.
Please have a look at the code i used -
tvTree.Nodes.Add("Subjects", "Subjects");
tvTree.Nodes["Subjects"].Nodes.Add("Physics", "Physics");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP1", "Paper1");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP2", "Paper2");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP3", "Paper3");
Thanks for sharing your time.
Your problem is that the "Physics" nodes are not direct children of tvTree but instead are children of the "Subjects" node. What should make this easier is that TreeNodeCollection.Add returns a TreeNode that you can reference later on.
var subjects = tvTree.Nodes.Add("Subjects", "Subjects");
var physics = subjects.Nodes.Add("Physics", "Physics");
physics.Nodes.Add("PhysicsP1", "Paper1");
physics.Nodes.Add("PhysicsP2", "Paper2");
physics.Nodes.Add("PhysicsP3", "Paper3");
If you only have the name, you can use Find:
var parentName = "from wherever";
var parentNodes = tvTree.Nodes.Find(parentName, true);
/* handle multiple results */
/* add children */
Also you may achieve this with
tvTree.Nodes.Add("Subjects", "Subjects");
tvTree.Nodes["Subjects"].Nodes.Add("Physics", "Physics");
var phyNode = tvTree.Nodes.Find("Physics", true).First();
phyNode.Nodes.Add("PhysicsP1", "Paper1");
phyNode.Nodes.Add("PhysicsP2", "Paper2");
phyNode.Nodes.Add("PhysicsP3", "Paper3");
You can use this
tvTree.Nodes["Subjects"].Nodes["Physics"].Add("PhysicsP1", "Paper1");

Categories