Recursively search nested lists - c#

I've read and searched and I'm yet to figure out an answer to this relatively simple issue.
I have a class:
public class AccessibleTreeItem
{
public string name;
public List<AccessibleTreeItem> children;
public AccessibleTreeItem()
{
children = new List<AccessibleTreeItem>();
}
}
which is populate using a series of functions that don't really matter in this context, but what I'm looking for is a way to search through ALL of the children items in the list, searching for a particular 'name' value, and if found, returning that List.
How is this achieved in the easiest manner, with the least performance hit? Thanks - I've been stumped at this point for days now...

public class AccessibleTreeItem
{
public string name;
public List<AccessibleTreeItem> children;
public AccessibleTreeItem()
{
children = new List<AccessibleTreeItem>();
}
public static AccessibleTreeItem Find(AccessibleTreeItem node, string name)
{
if (node == null)
return null;
if (node.name == name)
return node;
foreach (var child in node.children)
{
var found = Find(child, name);
if (found != null)
return found;
}
return null;
}
}

Related

I'm struggling to get a foreach loop working to loop through each element of a LinkedList

Im trying to do a Depth first traversal on graph node but I cant get my for each loop to loop correctly
Here is the code for doing the search
The foreach line is the problem
public void DepthFirstTraverse(T startID, ref List<T> visited)
{
LinkedList<T> adj;
Stack<T> toVisit = new Stack<T>();
GraphNode<T> current = new GraphNode<T>(startID);
toVisit.Push(startID);
while (toVisit.Count != 0)
{
current.ID = toVisit.Peek();
toVisit.Pop();
visited.Add(current.ID);
Console.WriteLine(current.GetAdjList());
foreach(T n in current.GetAdjList())
{
Console.WriteLine("Loop working");
if(toVisit.Contains(n)==false && visited.Contains(n)==true)
{
toVisit.Push(n);
}
}
}
}
Here is the graphnode code, it calls the getadjlist and should loop through for each element in that list, currently it doesnt loop at all
public class GraphNode
{
private T id; // data stored in the node (“id” of the node).
private LinkedList adjList; // adjacent list of the node
// constructor
public GraphNode(T id)
{
this.id = id;
adjList = new LinkedList<T>();
}
// set and get the data stored in the node
public T ID
{
set { id = value; }
get { return id; }
}
//add a directed edge from “this” node to the node "to”
public void AddEdge(GraphNode<T> to)
{
adjList.AddFirst(to.ID);
}
// returns the adjacent list of the node
public LinkedList<T> GetAdjList()
{
return adjList;
}
}

Simple List Merge C# (Without Generics)

I have created a simple list class from scratch. This is for a class assignment that I have been working on for about a week - very new to lists. We can not use generics so trying to research my question below has not been fruitful. Although I did get to watch 7 tutorials on youtube by BetterCoder and I found some stuff in my book but nothing with an example of "merging".
I have three classes - my node, my list, and my program. In my list class, I am working on building a Merge() method which eventually will compare the data in the two lists and merge them into an ordered list.
Right now for some reason my Merge method - which is very basic to help me understand what is happening - is not working correctly. It has both lists passed to it, and is adding the data from listTwo to listOne BUT for some reason when it's printing to the console the second Node's Data shows twice :
EX: 1 -> 2 -> 2
instead of printing the head (1), the next (2) and then the next (3) which it should be.
EX: 1 -> 2 -> 3
In the program class I have proven with a write line that (listOne.firstNode.Next.Next.Data) = 3 . Which it should be.
Can someone help me figure out if the nodes in list one aren't pointing to each other correctly or whatever is going on?
My Merge Method must be passed both list objects (listOne and listTwo) and eventually I need to make those passed as references but I haven't figured that out quite yet and will focus on that later I suppose.
namespace LinkedList
{
//This is my Node Class
class Node
{
public object Data { get; set; }
public Node Next { get; set; }
public Node(object dataValue) : this(dataValue, null) { }
public Node(object dataValue, Node nextNode)
{
Data = dataValue;
Next = nextNode;
}
}
//This is my List Class
class List
{
public Node firstNode;
public int count;
public List()
{
firstNode = null;
}
public bool Empty
{
get { return this.count == 0; }
}
public int Count
{
get { return this.count; }
}
public object Add(int index, object o)
{
if (index < 0)
throw new ArgumentOutOfRangeException("Index: " + index);
if (index > count)
index = count;
Node current = this.firstNode;
if (this.Empty || index == 0)
{
this.firstNode = new Node(o, this.firstNode);
}
else
{
for (int i = 0; i < index - 1; i++)
current = current.Next;
current.Next = new Node(o, current.Next);
}
count++;
return o;
}
public object Add(object o)
{
return this.Add(count, o);
}
public object Merge(List a, List b)
{
a.Add(b.firstNode.Data);
return a;
}
public void Print()
{
while (this.count > 0)
{
Console.Write(firstNode.Data + "->");
if(firstNode.Next != null)
firstNode.Data = firstNode.Next.Data;
count--;
}
}
}
//And here is my Program
class Program
{
static void Main(string[] args)
{
List listOne = new List();
List listTwo = new List();
listOne.Add(1);
listOne.Add(2);
listTwo.Add(3);
listTwo.Print();
Console.WriteLine("");
listOne.Merge(listOne, listTwo);
Console.WriteLine("");
listOne.Print();
//This line below shows that the data "3" from listTwo is being added to listOne in the list Merge Method
//Console.WriteLine(listOne.firstNode.Next.Next.Data);
Console.ReadKey();
}
}
}
Actual problem in your print method
public void Print()
{
Node node = firstNode;
for (int i = 0; i < this.count; i++)
{
Console.Write(node.Data + "->");
if (node.Next != null)
node = node.Next;
}
}
Alex Sikilinda , you are right the merge method is incomplete.
public object Merge(List a, List b)
{
Node bNode = b.firstNode;
while (bNode != null)
{
a.Add(bNode.Data);
bNode = bNode.Next;
}
return a;
}
I would write it this way:
public void Merge(List b)
{
Node lastNode = GetLastNode();
if (lastNode != null)
{
lastNode.Next = b.firstNode;
}
else
{
this.firstNode = b.firstNode;
}
}
// this method is used to find the last node in current list
private Node GetLastNode()
{
if (this.firstNode == null)
{
return null;
}
Node current = this.firstNode;
while (current.Next != null)
{
current = current.Next;
}
return current;
}
First of all, I changed signature of Merge from public object Merge(List a, List b) to public void Merge(List b). Now we can use it like this:
listOne.Merge(listTwo);
This will link listOne's last element with the first element of listTwo and thus they are merged.
Now we need to change Print method since current version modifies the list, which shouldn't happen:
public void Print()
{
Node currentNode = this.firstNode;
while(currentNode != null)
{
Console.Write(currentNode.Data + ' ');
currentNode = currentNode.Next;
}
}
Instead of assigning the data back to first node I assign the
firstNode = firstNode.Next;
Please check the below Print Code
public void Print()
{
while (this.count > 0)
{
Console.Write(firstNode.Data + "->");
if (firstNode.Next != null)
firstNode = firstNode.Next;
count--;
}
}

BInary Search Tree iterator c# without parent property

I recently started making a Binary Search Tree in C# in order to practice. After completing this task I decided to implement ICollection<T> so that my tree can be used with a foreachand just in general in order for me to practice.
I've constructed my classes in such a way that I have a Node<T> class and a BinarySearchTree<T> class that contains a Node<T> a Count integer and a IsReadOnly boolean. This is my Node Class:
internal class Node<T>: INode<T> where T: IComparable<T>
{
public Node<T> RightChildNode { get; set; }
public Node<T> LeftChildNode { get; set; }
public T Key { get; set; }
//some methods go here
}
and this is my BST class:
public class BinarySearchTree<T>: ICollection<T> where T: IComparable<T>
{
internal Node<T> Root { get; set; }
public int Count { get; private set; }
public bool IsReadOnly => false;
//some methods go here
}
Now in order to implement ICollection<T> i obviously need an enumerator which I have (partly) implemented as such:
internal class BinarySearchTreeEnumerator<T> : IEnumerator<T> where T: IComparable<T>
{
private BinarySearchTree<T> _parentTree;
private BinarySearchTree<T> _currentTree;
private Node<T> _currentNode => _currentTree.Root;
private T _currentKey;
public T Current => _currentNode.Key;
/// <summary>
/// generic constructor
/// </summary>
/// <param name="tree"></param>
public BinarySearchTreeEnumerator(BinarySearchTree<T> tree)
{
this._parentTree = tree;
}
object IEnumerator.Current => Current;
void IDisposable.Dispose(){}
//pls
public bool MoveNext()
{
if (_currentTree is null)
{
_currentTree = _parentTree;
}
var leftSubtree = this._currentTree.GetLeftSubtree();
var rightSubtree = this._currentTree.GetRightSubtree();
if (!(leftSubtree is null))
{
this._currentTree = leftSubtree;
}
else if (!(rightSubtree is null))
{
this._currentTree = rightSubtree;
}
else
{
}
}
public void Reset()
{
_currentTree = _parentTree;
}
}
now my issue is quite obviously with the MoveNext() method. It doesn't work now because what it does is it just goes down the tree on the leftmost possible path and then gets stuck when it gets to the end of that path. I know I can fix this problem by adding a Parent property to my Node<T> class and then whenever I reach the end of a path in my tree I can just go one Node up and check if there's a different path... However this would mean completely changing my original class and I would prefer not to do that.
Is this just unavoidable? Is there any way to solve this issue without changing my Node<T> class in such a way?
Edit: I Made a thing but its not working :/
public bool MoveNext()
{
if (_currentNode is null)
{
this._currentNode = _parentTree.Root;
this._nodeStack.Push(_currentNode);
return true;
}
var leftNode = this._currentNode.LeftChildNode;
var rightNode = this._currentNode.RightChildNode;
if (!(leftNode is null))
{
this._currentNode = leftNode;
this._nodeStack.Push(_currentNode);
return true;
}
else if (!(rightNode is null))
{
this._currentNode = rightNode;
this._nodeStack.Push(_currentNode);
return true;
}
else
{
//current node does not have children
var parent = this._nodeStack.Pop();
do
{
if (parent is null)
{
return false;
}
} while (!(parent.RightChildNode is null));
this._currentNode = parent.RightChildNode;
this._nodeStack.Push(_currentNode);
return true;
}
}
It might be easier to use recursion to implement this; for example:
Recursive version (for balanced trees only)
public IEnumerator<T> GetEnumerator()
{
return enumerate(Root).GetEnumerator();
}
IEnumerable<T> enumerate(Node<T> root)
{
if (root == null)
yield break;
yield return root.Key;
foreach (var value in enumerate(root.LeftChildNode))
yield return value;
foreach (var value in enumerate(root.RightChildNode))
yield return value;
}
These are members of BinarySearchTree<T>.
Given the above implementation, then the following code:
BinarySearchTree<double> tree = new BinarySearchTree<double>();
tree.Root = new Node<double> {Key = 1.1};
tree.Root.LeftChildNode = new Node<double> {Key = 2.1};
tree.Root.RightChildNode = new Node<double> {Key = 2.2};
tree.Root.LeftChildNode.LeftChildNode = new Node<double> { Key = 3.1 };
tree.Root.LeftChildNode.RightChildNode = new Node<double> { Key = 3.2 };
tree.Root.RightChildNode.LeftChildNode = new Node<double> { Key = 3.3 };
tree.Root.RightChildNode.RightChildNode = new Node<double> { Key = 3.4 };
foreach (var value in tree)
{
Console.WriteLine(value);
}
produces this output:
1.1
2.1
3.1
3.2
2.2
3.3
3.4
WARNING: Stack space is limited to 1MB for a 32-bit process and 4MB for a 64-bit process, so using recursion is likely to run out of stack space if the tree is degenerate (badly unbalanced).
Non-recursive version
You can implement the non-recursive version fairly simply, like so:
IEnumerable<T> enumerate(Node<T> root)
{
var stack = new Stack<Node<T>>();
stack.Push(root);
while (stack.Count > 0)
{
var node = stack.Pop();
if (node == null)
continue;
yield return node.Key;
stack.Push(node.RightChildNode);
stack.Push(node.LeftChildNode);
}
}
This returns the elements in the same order as the recursive version.
Since this version will not run out of stack space even for a degenerate tree, it is preferable to the recursive version.
If you add to your enumerator a
private List<Node<T>> _currentParentNodes = new List<Node<T>>();
and use it like a stack, each time you go down a level you push the current node to currentParentNodes, each time you have to go up you pop the parent node from currentParentNodes, then all your problems will pop away :-)
Do you need a depth-first-search(DFS) approach? It has a recursive nature which is hard to save as a state (it uses the call stack).
Maybe consider the broad-first-search (BFS) approach, which is iterative. You'd only need a currentNode and a Queue.
The rule would be
current = Queue.poll();
For child in current
Queue.offer(child);
At init you would do
Queue.offer(rootNode);
I apologize for my poor formatting and syntax, mobile user here.
Andres

C# how to avoid multiple foreach and if statements [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have the below function that works and cycles through 3 possible levels.
Is there a way to do the same as the function below but not have to do the multiple foreach statements?
Basically the reponses list can contain multiple GroupResponseTypes
each of these can contain multiple ElementResponseBaseTypes
which can be a variety of types themselves
I'm interested in finding a value in an ElementResponseType
And each ElementResponseBaseType itself can be a GroupResponseType, it to containing multiple types.
So i'm looking at an easy way to scan through the whole structure for a specific Element.Reference
and return the relevant value
Any help is much appreciated
public static string GetValueFromFormField(List<ResponseBaseType> responses, string fieldref)
{
string fieldvalue = String.Empty;
foreach (GroupResponseType groups in responses)
{
foreach (ElementResponseBaseType firstelements in groups.Responses)
{
if (firstelements.GetType() == typeof(ElementResponseType))
{
if (firstelements.Element.Reference == fieldref)
{
ElementResponseType firstelement = new ElementResponseType();
firstelement = (ElementResponseType)firstelements;
fieldvalue = firstelement.Value;
}
}
else if (firstelements.GetType() == typeof(GroupResponseType))
{
GroupResponseType secondgroup = new GroupResponseType();
secondgroup = (GroupResponseType)firstelements;
foreach (ElementResponseBaseType secondelements in secondgroup.Responses)
{
if (secondelements.GetType() == typeof(ElementResponseType))
{
if (secondelements.Element.Reference == fieldref)
{
ElementResponseType secondelement = new ElementResponseType();
secondelement = (ElementResponseType)secondelements;
fieldvalue = secondelement.Value;
}
}
else if (secondelements.GetType() == typeof(GroupResponseType))
{
GroupResponseType thirdgroup = new GroupResponseType();
thirdgroup = (GroupResponseType)secondelements;
foreach (ElementResponseBaseType thirdelements in thirdgroup.Responses)
{
if (thirdelements.GetType() == typeof(ElementResponseType))
{
if (thirdelements.Element.Reference == fieldref)
{
ElementResponseType thirdelement = new ElementResponseType();
thirdelement = (ElementResponseType)thirdelements;
fieldvalue = thirdelement.Value;
}
}
}
}
}
}
}
}
return fieldvalue;
}
You need to identify which parts of your code are used repeatedly and factor them out into new methods. If you do that over and over, eventually you will obtain something like this:
public static string GetValueFromResponses(IEnumerable<ElementResponseBaseType> responses, string fieldref)
{
foreach (ElementResponseBaseType response in responses)
{
ElementResponseType element = response as ElementResponseType;
if (element != null)
{
string foundValue = CheckElement(element, fieldref);
if (foundValue != null)
{
return foundValue;
}
}
else
{
GroupResponseType group = response as GroupResponseType;
if (group != null)
{
string foundValue = GetValueFromResponses(group.Responses, fieldref);
if (foundValue != null)
{
return foundValue;
}
}
}
}
return string.Empty;
}
private static string CheckElement(ElementResponseType element, string fieldref)
{
if (element.Element.Reference == fieldref)
{
return element.Value;
}
return null;
}
Here's a version that uses Linq (this contains all of the functionality in your original method):
public static string GetValueFromResponses(IEnumerable<ElementResponseBaseType> responses, string fieldref)
{
var foundInElements = responses.OfType<ElementResponseType>()
.Select(e => CheckElement(e, fieldref));
var foundInGroups = responses.OfType<GroupResponseType>()
.Select(g => GetValueFromResponses(g.Responses,
fieldref));
return foundInElements.Concat(foundInGroups)
.FirstOrDefault(s => s != null) ?? string.Empty;
}
private static string CheckElement(ElementResponseType element, string fieldref)
{
if (element.Element.Reference == fieldref)
{
return element.Value;
}
return null;
}
You should give your base type, in this case ResponseBaseType, a member that returns all of it's decedent leaf nodes. You can then implement the behavior of that member separately for each type. The group type can return all of the leaves in all of its own children (recursively), and the single item can return itself.
You can then take any instance of the base type and get all of the leaves, or, in this case, the first leaf. Note that since you're only trying to get the first result here, not all of them, you'd benefit from making your implementation of the group's member use deferred execution, so that you don't need to bother computing all of the values just to get the first.
As complex as that might seem at first, it takes very little code to actually implement.
public abstract class ResponseBaseType
{
public abstract IEnumerable<ElementResponseType> Leaves { get; }
}
public class GroupResponseType : ResponseBaseType
{
public IEnumerable<ResponseBaseType> Children { get; private set; }
public override IEnumerable<ElementResponseType> Leaves
{
get
{
return Children.SelectMany(child => child.Leaves);
}
}
}
public class ElementResponseType : ResponseBaseType
{
public override IEnumerable<ElementResponseType> Leaves
{
get
{
yield return this;
}
}
}
This enables you to take your list of responses, map it to a sequences of all of their leaves, and then get the first/last leaf from that.
responses.SelectMany(response => response.Leaves).Last();

Traverse tree initializing variables

If you find a more appreapiate way of naming the title of this question please feel free to update it.
I have the Node
public Class Node
{
public List<Node> Children = new List<Node>();
public Node Parent;
public FileSystemInfo Value;
}
to represent a file in a tree.
Now I will like to change that node class for:
class ScanItem
{
public List<ScanItem> Children;
public ScanItem Parent;
public long Size;
public string Name;
public string FullPath;
public bool IsDirectory;
// etc....
}
So my question is how will I be able to cast a Node class object to a ScanItem. I will like to store the size of files and directories not just files. (Node class only stores sizes of files not directories) Therefore I will have to somehow recursively keep track of the sum of the sizes of children in order to achieve what I want.
So far I have placed this constructor on ScanItem:
public ScanItem(Node node)
{
this.Name = node.Name;
this.FullPath = node.Value.FullName;
foreach(var child in node.Children)
{
this.Children.add(new ScanItem(child));
}
}
That will enable me to perform the cast but I am missing to assign the sizes of directories...
To get the size of a directory you can use this extension method
public static class DirectoryInfoEx
{
public static long GetDirectorySize(this DirectoryInfo di)
{
long size = 0;
var fileInfos = di.GetFiles();
foreach (var fi in fileInfos)
{
size += fi.Length;
}
var subDirInfos = di.GetDirectories();
foreach (var subDir in subDirInfos)
{
size += GetDirectorySize(subDir);
}
return size;
}
}
If I got this correctly, you will just need to add two lines of code in the constructor of the ScanItem class
public ScanItem(Node node)
{
this.Name = node.Name;
this.FullPath = node.Value.FullName;
DirectoryInfo di = new DirectoryInfo(this.FullPath); //<<<<<<<
this.Size = di.GetDirectorySize(); //<<<<<<<
foreach(var child in node.Children)
{
this.Children.add(new ScanItem(child));
}
}
You may need to handle some exceptional situations like UnauthorizedAccessException. You can also improve your style by getting rid of public fields, for example by converting them into public auto properties public long Size{get;set;}.

Categories