Skip to content

Instantly share code, notes, and snippets.

@AdhirRamjiawan
Created August 16, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdhirRamjiawan/63c60008dbe7432518a2804348a28ccc to your computer and use it in GitHub Desktop.
Save AdhirRamjiawan/63c60008dbe7432518a2804348a28ccc to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnTreeDS
{
public class Node
{
public string Name { get; set; }
public bool Visited { get; set; }
public List<Node> Children { get; set; } = new List<Node>();
}
class Program
{
/*
_ R_
/ | \
1 2 3
/ | \
4 5 6
*/
public static void TraverseFn(Node currentNode)
{
currentNode.Visited = true;
Console.WriteLine("Visited: " + currentNode.Name);
foreach (var child in currentNode.Children)
TraverseFn(child);
}
static void Main(string[] args)
{
var Root = new Node() { Name = "Root" };
var child1 = new Node() { Name = "1" };
child1.Children.Add(new Node() { Name = "4" });
child1.Children.Add(new Node() { Name = "5" });
child1.Children.Add(new Node() { Name = "6" });
Root.Children.Add(child1);
Root.Children.Add(new Node() { Name = "2" });
Root.Children.Add(new Node() { Name = "3" });
TraverseFn(Root);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment