Created
March 2, 2012 18:07
-
-
Save aloker/1960108 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace DFS | |
| { | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| public class Control | |
| { | |
| public Control(string id) | |
| { | |
| Id = id; | |
| } | |
| public string Id { get; private set; } | |
| public Control[] Controls { get; set; } | |
| } | |
| public class MyControl : Control | |
| { | |
| public MyControl(string id) | |
| : base(id) | |
| { | |
| } | |
| } | |
| public static class ControlExtensions | |
| { | |
| public static T FindFirstControlOfTypeBreadthFirst<T>(this Control rootControl) where T : Control | |
| { | |
| var fringe = new Queue<Control>(); | |
| fringe.Enqueue(rootControl); | |
| while (fringe.Count > 0) | |
| { | |
| var current = fringe.Dequeue(); | |
| Console.WriteLine("Visiting {0}", current.Id); | |
| var check = current as T; | |
| if (check != null) | |
| { | |
| return check; | |
| } | |
| if (current.Controls != null) | |
| { | |
| foreach (var child in current.Controls) | |
| { | |
| fringe.Enqueue(child); | |
| } | |
| } | |
| } | |
| return null; | |
| } | |
| private class Program | |
| { | |
| private static void Main(string[] args) | |
| { | |
| var graph = new Control("root") | |
| { | |
| Controls = new[] | |
| { | |
| new Control("1") | |
| { | |
| Controls = new[] | |
| { | |
| new Control(("11")) { Controls = new[] { new Control("111"), new MyControl("112") } }, | |
| new Control(("12")) { Controls = new[] { new Control("121"), new Control("122") } } | |
| } | |
| }, | |
| new Control("2") | |
| { | |
| Controls = new[] | |
| { | |
| new Control(("21")) { Controls = new[] { new Control("211"), new Control("212") } }, | |
| new Control(("22")) { Controls = new[] { new Control("221"), new Control("222") } } | |
| } | |
| }, | |
| new Control("3") | |
| { | |
| Controls = new[] | |
| { | |
| new Control(("31")) { Controls = new[] { new Control("311"), new Control("312") } }, | |
| new Control(("32")) { Controls = new[] { new Control("321"), new Control("322") } } | |
| } | |
| }, | |
| new Control("4") | |
| { | |
| Controls = new[] | |
| { | |
| new Control(("41")) { Controls = new[] { new Control("4111"), new Control("412") } }, | |
| new Control(("42")) { Controls = new[] { new Control("421"), new Control("422") } } | |
| } | |
| }, | |
| } | |
| }; | |
| Console.WriteLine("Searching breadth-first"); | |
| var match = graph.FindFirstControlOfTypeBreadthFirst<MyControl>(); | |
| Console.WriteLine("Found control {0}", match.Id); | |
| } | |
| } | |
| } | |
| // Output: | |
| // Searching breadth-first | |
| // Visiting root | |
| // Visiting 1 | |
| // Visiting 2 | |
| // Visiting 3 | |
| // Visiting 4 | |
| // Visiting 11 | |
| // Visiting 12 | |
| // Visiting 21 | |
| // Visiting 22 | |
| // Visiting 31 | |
| // Visiting 32 | |
| // Visiting 41 | |
| // Visiting 42 | |
| // Visiting 111 | |
| // Visiting 112 | |
| // Found control 112 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment