I am trying to write a Function Previous(), to get the previous nodes of a Tree. The statement is: "We have a tree that is built using the class Node where an instance of the class represents a node in the tree. For simplicity, the node has a single data field of type int.
Your task is to write the extension method NodeExtensions.Previous() to find the previous element in the tree. You can write as many helper methods as you want, but don't change the signature of the extension method NodeExtensions.Previous()."
The node class goes like this:
public class Node
{
private List<Node> _children;
public Node(int data, params Node[] nodes)
{
Data = data;
AddRange(nodes);
}
public Node Parent { get; set; }
public IEnumerable<Node> Children
{
get
{
return _children != null
? _children
: Enumerable.Empty<Node>();
}
}
public int Data { get; private set; }
public void Add(Node node)
{
Debug.Assert(node.Parent == null);
if (_children == null)
{
_children = new List<Node>();
}
_children.Add(node);
node.Parent = this;
}
public void AddRange(IEnumerable<Node> nodes)
{
foreach (var node in nodes)
{
Add(node);
}
}
public override string ToString()
{
return Data.ToString();
}
}
I need to write an extension method like this:
using System;
using System.Linq;
public static class NodeExtensions
{
public static Node Previous(this Node node)
{
// TODO Implement extension method here
}
}
I have a test case:
using System;
using System.Text;
using NUnit.Framework;
public class NodeExtensionsTests
{
[Test]
public void Test()
{
// Test tree:
//
// 1
// +-2
// +-3
// +-4
// +-5
// +-6
// +-7
//
var lastNode = new Node(7);
var tree = new Node(
1,
new Node(
2,
new Node(3),
new Node(4)),
new Node(
5,
new Node(6),
lastNode));
// Expected output:
//
// 7
// 6
// 5
// 4
// 3
// 2
// 1
//
var n = lastNode;
while (n != null)
{
Console.WriteLine(n.Data);
n = n.Previous();
}
// Test
//
n = lastNode;
Assert.AreEqual(7, n.Data);
n = n.Previous();
Assert.AreEqual(6, n.Data);
n = n.Previous();
Assert.AreEqual(5, n.Data);
n = n.Previous();
Assert.AreEqual(4, n.Data);
n = n.Previous();
Assert.AreEqual(3, n.Data);
n = n.Previous();
Assert.AreEqual(2, n.Data);
n = n.Previous();
Assert.AreEqual(1, n.Data);
n = n.Previous();
Assert.IsNull(n);
}
}
Whatever I try, it either goes into an infinite loop, or just returns the root nodes instead of "all silblings". Can someone help me here?
I think this will work for you
public static class NodeExtensions
{
public static Node Previous(this Node node)
{
if (node.Parent == null) { return null; }
var brothers = (List<Node>) node.Parent.Children;
var index = brothers.IndexOf(node);
if(index == 0)
{
return node.Parent;
}
else
{
var next = brothers[index - 1];
while (next.Children.Any())
{
next = next.Children.Last();
}
return next;
}
}
}
Related
here i have the following code and input that prints a tree structure. My question is how can i make it so that the nodes and leafs that have the value "Unavailable" are skipped from being printed.
namespace Tree{public class TreeNode<T>
{
private T value;
private bool hasParent;
private List<TreeNode<T>> children;
public TreeNode(T value)
{
if (value == null)
{
throw new ArgumentNullException("Cannot insert null value");
}
this.value = value;
this.children = new List<TreeNode<T>>();
}
public T Value
{
get
{
return this.value;
}
set
{
this.value = value;
}
}
public int ChildrenCount
{
get
{
return this.children.Count;
}
}
public void AddChild(TreeNode<T> child)
{
if (child == null)
{
throw new ArgumentNullException("Cannot insert null value");
}
if (child.hasParent)
{
throw new ArgumentException("The node already has a parent");
}
child.hasParent = true;
this.children.Add(child);
}
public TreeNode<T> GetChild(int index)
{
return this.children[index];
}
}
public class Tree<T>
{
private TreeNode<T> root;
public Tree(T value)
{
if (value == null)
{
throw new ArgumentNullException("Cannot insert null value");
}
this.root = new TreeNode<T>(value);
}
public Tree(T value, params Tree<T>[] children) : this(value)
{
foreach (Tree<T> child in children)
{
this.root.AddChild(child.root);
}
}
public TreeNode<T> Root
{
get
{
return this.root;
}
}
private void PrintDFS(TreeNode<T> root, string spaces)
{
if (this.root == null)
{
return;
}
Console.WriteLine(spaces + root.Value);
TreeNode<T> child = null;
for (int i = 0; i < root.ChildrenCount; i++)
{
child = root.GetChild(i);
PrintDFS(child, spaces + " ");
}
}
public void TraverseDFS()
{
this.PrintDFS(this.root, string.Empty);
}
}
public static class TreeExample
{
static void Main()
{
Tree<string> tree =
new Tree<string>("John",
new Tree<string>("Jasmine",
new Tree<string>("Jay"),
new Tree<string>("Unavailable")),
new Tree<string>("Unavailable",
new Tree<string>("Jack"),
new Tree<string>("Jeremy")),
new Tree<string>("Johanna")
);
tree.TraverseDFS();
}
}}
right now it prints :(John, (Jasmine, (Jay), (Unavailable)), (Unavailable, (Jack, (Jeremy))), (Johanna))
I need it to print :(John, (Jasmine, (Jay)), (Johanna))
So basically skip every leaf with the value "Unavailable" and every node with the value "Unavailable" and all children from that node
Thanks !
This should work:
private void PrintDFS(TreeNode<T> root, string spaces)
{
if (this.root == null
|| "Unavailable" == root.Value.ToString())
{
return;
}
...
The accepted answer is a literally correct answer to the question, but it bakes in logic about what to do with the tree into the tree itself. A tree is a kind of collection or data structure, and you don't often see a List or Dictionary that is able to print itself. Instead the collection provides the right methods to get or change its contents so that you can do what you want.
In your case, you could do something like the following:
public enum TreeVisitorResult {
SkipNode,
Continue
}
// the following two methods inside Tree<T>:
public void VisitNodes(Func<TreeNode<T>, int, TreeVisitorResult> visitor) {
VisitNodes(0, this.root, visitor);
}
private void VisitNodes(int depth, TreeNode<T> node,
Func<TreeNode<T>, int, TreeVisitorResult> visitor) {
if (node == null) {
return;
}
var shouldSkip = visitor(node, depth);
if (shouldSkip == TreeVisitorResult.SkipNode) {
return;
}
TreeNode<T> child = null;
for (int i = 0; i < node.ChildrenCount; i++) {
child = node.GetChild(i);
VisitNodes(depth + 1, child, visitor);
}
}
If you had this method, you could write the Print method outside of the Tree classes, as:
tree.VisitNodes((treeNode, depth) => { // <- this lambda will be called for every node
if (treeNode.Value == "Unavailable") { // <- no need to ToString or cast here, since
// we know that T is string here
return TreeVisitorResult.SkipNode;
} else {
var spaces = new string(' ', depth * 3);
Console.WriteLine(spaces + treeNode.Value);
}
});
I've got a Type that we'll call Foo that can hold a collection of children Foo objects. Foo is Disposable, so when ever a child is disposed of, it will then add itself to the parent's Children collection.
An example usage of this looks like:
using (var a = AddChild(Root, "a"))
{
using (var a1 = AddChild(a, "a1"))
{
using (var a1a = AddChild(a1, "a1a"))
{
}
}
In this example a1a is only added to a1 when it is disposed, and not before. What I am having difficulty in figuring out is a clean way of writing a GetAllFoos method that returns all of the objects in a flattened list, in a FILO order.
In this case, would I just recursively iterate over each child, or is there some fancy LINQ I can use to try and consolidate these collections? I'm using this to take performance measurement snapshots through-out the app, and it's possible that we would call GetAllMeasurements in some cases during a profile so the performance of the method call is important.
This is a complete example app that shows what the expected results would look like. I have to support both FIFO and FILO. I've got a FIFO implementation working but I'm not sure on the best way to handle this inversely for FILO.
using System;
using System.Collections.Generic;
using System.Linq;
namespace FILO_Example
{
public class Foo : IDisposable
{
internal Foo parent;
public Foo(Foo parent = null)
{
this.parent = parent;
}
public string Name { get; set; }
public List<Foo> Children { get; } = new List<Foo>();
public void Dispose() => this.parent.Children.Add(this);
}
class Program
{
public static Foo Root { get; } = new Foo { Name = "Root" };
static void Main(string[] args)
{
// level 1
using (var a = AddChild(Root, "a"))
{
using (var a1 = AddChild(a, "a1"))
{
using (var a1a = AddChild(a1, "a1a"))
{
}
}
using (var a2 = AddChild(a, "a2"))
{
}
}
using (var b = AddChild(Root, "b"))
{
using (var b1 = AddChild(b, "b1"))
{
}
}
List<Foo> allFoos = GetAllFoosFILO().ToList();
Console.WriteLine(allFoos[0]); // Should be b1
Console.WriteLine(allFoos[1]); // Should be b
Console.WriteLine(allFoos[2]); // Should be a2
Console.WriteLine(allFoos[3]); // Should be a1a
Console.WriteLine(allFoos[4]); // Should be a1
Console.WriteLine(allFoos[5]); // Should be a
}
static IEnumerable<Foo> GetAllFoosFILO()
{
return new List<Foo>();
}
static IEnumerable<Foo> GetAllFoosFIFO()
{
var fooStack = new Stack<Foo>();
fooStack.Push(Root);
while (fooStack.Count > 0)
{
Foo currentFoo = fooStack.Pop();
yield return currentFoo;
// If we have children, add them in reverse order so that it's a First-In-First-Out stack
// then the while loop will yield each child element.
if (currentFoo.Children.Count > 0)
{
List<Foo> fooChildren = currentFoo.Children;
for (int currentIndex = fooChildren.Count - 1; currentIndex >= 0; currentIndex--)
{
fooStack.Push(fooChildren[currentIndex]);
}
}
}
}
static Foo AddChild(Foo parent, string name)
{
var child = new Foo(parent) { Name = name };
return child;
}
}
}
As I mentioned in the comments, you have a tree structure. There is no fancy efficient standard LINQ solution, but you can utilize the quite efficient generic Traverse method form my answer to Enumerating Directories iteratively in "postorder":
public static class TreeHelper
{
public static IEnumerable<T> Traverse<T>(T node, Func<T, IEnumerable<T>> childrenSelector, bool preOrder = true)
{
var stack = new Stack<IEnumerator<T>>();
var e = Enumerable.Repeat(node, 1).GetEnumerator();
try
{
while (true)
{
while (e.MoveNext())
{
var item = e.Current;
var children = childrenSelector(item);
if (children == null)
yield return item;
else
{
if (preOrder) yield return item;
stack.Push(e);
e = children.GetEnumerator();
}
}
if (stack.Count == 0) break;
e.Dispose();
e = stack.Pop();
if (!preOrder) yield return e.Current;
}
}
finally
{
e.Dispose();
while (stack.Count != 0) stack.Pop().Dispose();
}
}
}
With that helper, the GetAllFoosFIFO() is simple as that:
static IEnumerable<Foo> GetAllFoosFIFO()
{
return TreeHelper.Traverse(Root, foo => foo.Children.Count > 0 ? foo.Children : null);
}
while for GetAllFoosFILO() you need to pass preorder = false and iterate Children in reverse order:
static IEnumerable<Foo> GetAllFoosFILO()
{
return TreeHelper.Traverse(Root, foo => foo.Children.Count > 0 ?
Enumerable.Range(0, foo.Children.Count)
.Select(i => foo.Children[foo.Children.Count - 1 - i]) : null, false);
}
This should work:
private static IEnumerable<Foo> GetAllFoosFILO(Foo foo)
{
foreach (var c in ((IEnumerable<Foo>)foo.Children).Reverse())
{
var cc = GetAllFoosFILO(c);
foreach (var ccc in cc)
{
yield return ccc;
}
yield return c;
}
}
The weird cast in first foreach loop is for preventing use of List<T>.Reverse instead of Enumerable.Reverse<TSource> extension method which helps us to traverse tree in so called FILO way.
Bonus
With some small touches you can write FIFO like:
private static IEnumerable<Foo> GetAllFoosFIFO(Foo foo)
{
foreach (var c in foo.Children)
{
yield return c;
var cc = GetAllFoosFIFO(c);
foreach (var ccc in cc)
{
yield return ccc;
}
}
}
I'm trying to solve a Leetcode problem Binary Tree Preorder Traversal. Below is what I wrote so far :
using System.Collections.Generic;
using System.Linq;
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
public class Solution
{
public IList<int> PreorderTraversal(TreeNode root)
{
if (root == null)
{
return new List<int> { };
}
else
{
IList<int> ret = new List<int> { root.val };
ret = ret.Concat(PreorderTraversal(root.left)) as IList<int>;
ret = ret.Concat(PreorderTraversal(root.right)) as IList<int>;// <-- this line
return ret;
}
}
}
class Program
{
static void Main(string[] args)
{
var root = new TreeNode(42);
root.right = new TreeNode(99);
var result = new Solution().PreorderTraversal(root);
}
}
I tried the same algorithm with ToList(), it worked. But when running this code, System.ArgumentNullException was thrown at the line I marked. So my question is where did it come from? How to understand it? How to fix it?
Although your initial value for ret is an IList<int>, the return type when using .Concat is an IEnumerable<int>. However you use a safe (as) cast back to an IList<Int>; that's not a valid cast and so your first .Concat operation returns null... The next line then tries to operate on a null value.
.Concat is implemented as an extension method that takes the source (ret in this case) as an argument so it throws an ArgumentNullException when null is passed.
You could fix this by using IEnumerable<int> as the return type and for the declaration of ret and removing your casts.
using System.Collections.Generic;
using System.Linq;
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
public class Solution
{
public IEnumerable<int> PreorderTraversal(TreeNode root)
{
if (root == null)
{
return Enumerable.Empty<int>();
}
else
{
IEnumerable<int> ret = new List<int> { root.val };
ret = ret.Concat(PreorderTraversal(root.left));
ret = ret.Concat(PreorderTraversal(root.right));
return ret;
}
}
}
class Program
{
static void Main(string[] args)
{
var root = new TreeNode(42);
root.right = new TreeNode(99);
var result = new Solution().PreorderTraversal(root);
}
}
For a school project I am making an RPN calculator. This calculator has an abstract class Stack where three different implementations of stacks derive from. One of them is a linked list stack. For these stacks I have to show what values are on the stack. To do this I use an array. For my arraystack and list stack it was easy, but I can't figure out how to convert a linked list to an array. What is the best way to do that?
This is the code that I use for my linked list.
public class Node
{
public int data;
public Node next;
public Node(int i)
{
data = i;
next = null;
}
public void Add(int i)
{
if (next == null)
{
next = new Node(i);
}
else
{
next.Add(i);
}
}
}
public class MyLinkedList
{
private Node headNode;
private int count;
public MyLinkedList()
{
headNode = null;
count = 0;
}
public int Count()
{
return count;
}
public void Add(int i)
{
if (headNode == null)
{
headNode = new Node(i);
}
else
{
headNode.Add(i);
}
count++;
}
Implement IEnumerable on your LinkedList
using System.Linq;
Call ToArray on the LinkedList.
Implementing IEnumerable is trivial. Simply start at the root node in your list, yield return Node.data;, and move to the next node. Rinse and repeat until the next node is null.
Add a new method to your class.
public class MyLinkedList
{
... keep existing methods here ...
public int[] ToArray() {
var result = new int[count]();
var index = 0;
var node = headnode;
while (node != null) {
result[index] = node.data;
node = node.next;
}
return result;
}
Eh... Something like that?
public class MyLinkedList {
...
public Node[] ToArray() {
// You´ve got pre-computed count - let´s use it
Node[] result = new Node[count];
Node node = headNode;
for (int i = 0; i < result.Length; ++i) {
result[i] = node;
node = node.next;
}
return result;
}
}
P.S. public fields like next in the Node class is a bad practice. Convert them into properties
As #Will suggested, I would implement IEnumerable<int> by your class. That will bring you to powerful world of LINQ where you will be able to convert your linked list to array, list, or just filter nodes:
public class MyLinkedList : IEnumerable<int>
{
// your code here
public IEnumerator<int> GetEnumerator()
{
Node current = headNode;
while(current != null)
{
yield return current.data;
current = current.next;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Now you can use all LINQ extensions with your class:
MyLinkedList myList = new MyLinkedList();
myList.Add(10);
myList.Add(15);
myList.Add(20);
int[] array = myList.ToArray();
List<int> list = myList.ToList();
// items above 13
var items = myList.Where(i => i > 13);
I need to create a product catalog, in tree type.
every tree node presents by a ID(string), the functions on the tree data only 2:
getChild(string ID), give a ID, get children (no need include childrens'
children), if ID is null, get all root nodes
getParent(string ID), return parent ID if have, or null if is root
Since once the tree decided, will not change, so I think put all code in static will be best.
So I start to try use Dictionary
"id": {parent:ID, child:[id2, id3, id4....]}
Since theres about 1000+ catalog, I found I quickly mess myself up, lots of mistake in the static data, and make final result on usable. Also, now I only wrote dozens and the code is looking like mess.
Please advice a way create this simple catalog tree with high performance. Thanks
Just make a class out of it.
UPDATED:
class TreeNode : IEnumerable<TreeNode>
{
private readonly Dictionary<string, TreeNode> _children =
new Dictionary<string, TreeNode>();
public readonly string ID;
public TreeNode Parent { get; private set; }
public TreeNode(string id)
{
this.ID = id;
}
public TreeNode GetChild(string id)
{
return this._children[id];
}
public void Add(TreeNode item)
{
if (item.Parent != null)
{
item.Parent._children.Remove(item.ID);
}
item.Parent = this;
this._children.Add(item.ID, item);
}
public IEnumerator<TreeNode> GetEnumerator()
{
return this._children.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public int Count
{
get { return this._children.Count; }
}
}
Usage will be fairly simple to statically define:
var tree = new TreeNode("Root")
{
new TreeNode("Category 1")
{
new TreeNode("Item 1"),
new TreeNode("Item 2"),
new TreeNode("Item 3"),
},
new TreeNode("Category 2")
{
new TreeNode("Item 1"),
new TreeNode("Item 2"),
new TreeNode("Item 3"),
new TreeNode("Item 4"),
}
};
Edit
Some more functionality for even easier creation...
public static TreeNode BuildTree(string tree)
{
var lines = tree.Split(new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries);
var result = new TreeNode("TreeRoot");
var list = new List<TreeNode> { result };
foreach (var line in lines)
{
var trimmedLine = line.Trim();
var indent = line.Length - trimmedLine.Length;
var child = new TreeNode(trimmedLine);
list[indent].Add(child);
if (indent + 1 < list.Count)
{
list[indent + 1] = child;
}
else
{
list.Add(child);
}
}
return result;
}
public static string BuildString(TreeNode tree)
{
var sb = new StringBuilder();
BuildString(sb, tree, 0);
return sb.ToString();
}
private static void BuildString(StringBuilder sb, TreeNode node, int depth)
{
sb.AppendLine(node.ID.PadLeft(node.ID.Length + depth));
foreach (var child in node)
{
BuildString(sb, child, depth + 1);
}
}
Usage:
var tree = TreeNode.BuildTree(#"
Cat1
Sub1
Item1
Item2
Item3
Sub2
Item1
Item2
Cat2
Sub1
Sub2
Item1
Item2
Sub3
Item1
Cat3
Cat4");
I created a Node class that could be helpfull. It is fast and has some extra properties, like:
Ancestors
Descendants
Siblings
Level of the node
Parent
Root
Etc.
You can write a simple binary tree , I wrote some Pseudo code beloew:
class TreeNode {
TreeNode Right;
TreeNode Left;
int id;
//...
}
class BinTree {
void Insert(TreeNode node)
{
while(true) {
if(node.id > target.id) {
if(target.Right != null) {
target = target.Right;
continue;
}
else {
target.Right = node;
break;
}
}
else if(node.id < target.id) {
if(target.Left != null) {
target = target.Left;
continue;
}
else {
target.Left = node;
break;
}
}
else {
throw new ArgumentException("Duplicated id");
}
}
}
TreeNode Search(int id)
{
TreeNode target = root;
while(target != null) {
if(id > target.id) {
target = target.Right;
}
else if(id < target.id) {
target = target.Left;
}
else {
return target;
}
}
return null;
}
}
But if your data count is very large, maybe AVL tree is more efficient