X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FMono.C5%2FUserGuideExamples%2FTreeTraversal.cs;h=82ee3ded984e40959918b6f8fb2dccc8295bb460;hb=ccdf8c3274d1793ffeddedfd784d49707feea62a;hp=0fbc2478aec154f0bebeeb9c922c330f71fec063;hpb=d49951ccf584ba637afb1dab7fff714478e3174d;p=mono.git diff --git a/mcs/class/Mono.C5/UserGuideExamples/TreeTraversal.cs b/mcs/class/Mono.C5/UserGuideExamples/TreeTraversal.cs index 0fbc2478aec..82ee3ded984 100644 --- a/mcs/class/Mono.C5/UserGuideExamples/TreeTraversal.cs +++ b/mcs/class/Mono.C5/UserGuideExamples/TreeTraversal.cs @@ -1,107 +1,107 @@ -// C5 example -// 2004-11-09 - -using System; -using C5; -using SCG = System.Collections.Generic; - -namespace TreeTraversal -{ - class MyTest - { - public static void Main(String[] args) - { - Tree t = MakeTree(1, 15); - Act act = delegate(int val) { Console.Write("{0} ", val); }; - Console.WriteLine("Depth-first:"); - Tree.DepthFirst(t, act); - Console.WriteLine("\nBreadth-first:"); - Tree.BreadthFirst(t, act); - Console.WriteLine("\nDepth-first:"); - Tree.Traverse(t, act, new ArrayList>()); - Console.WriteLine("\nBreadth-first:"); - Tree.Traverse(t, act, new LinkedList>()); - Console.WriteLine(); - } - - // Build n-node tree with root numbered b and other nodes numbered b+1..b+n - public static Tree MakeTree(int b, int n) - { - if (n == 0) - return null; - else - { - int k = n / 2; - Tree t1 = MakeTree(b + 1, k), t2 = MakeTree(b + k + 1, n - 1 - k); - return new Tree(b, t1, t2); - } - } - } - - class Tree - { - private T val; - private Tree t1, t2; - public Tree(T val) : this(val, null, null) { } - public Tree(T val, Tree t1, Tree t2) - { - this.val = val; this.t1 = t1; this.t2 = t2; - } - - public static void DepthFirst(Tree t, Act act) - { - IStack> work = new ArrayList>(); - work.Push(t); - while (!work.IsEmpty) - { - Tree cur = work.Pop(); - if (cur != null) - { - work.Push(cur.t2); - work.Push(cur.t1); - act(cur.val); - } - } - } - - public static void BreadthFirst(Tree t, Act act) - { - IQueue> work = new CircularQueue>(); - work.Enqueue(t); - while (!work.IsEmpty) - { - Tree cur = work.Dequeue(); - if (cur != null) - { - work.Enqueue(cur.t1); - work.Enqueue(cur.t2); - act(cur.val); - } - } - } - - public static void Traverse(Tree t, Act act, IList> work) - { - work.Clear(); - work.Add(t); - while (!work.IsEmpty) - { - Tree cur = work.Remove(); - if (cur != null) - { - if (work.FIFO) - { - work.Add(cur.t1); - work.Add(cur.t2); - } - else - { - work.Add(cur.t2); - work.Add(cur.t1); - } - act(cur.val); - } - } - } - } +// C5 example +// 2004-11-09 + +using System; +using C5; +using SCG = System.Collections.Generic; + +namespace TreeTraversal +{ + class MyTest + { + public static void Main(String[] args) + { + Tree t = MakeTree(1, 15); + Act act = delegate(int val) { Console.Write("{0} ", val); }; + Console.WriteLine("Depth-first:"); + Tree.DepthFirst(t, act); + Console.WriteLine("\nBreadth-first:"); + Tree.BreadthFirst(t, act); + Console.WriteLine("\nDepth-first:"); + Tree.Traverse(t, act, new ArrayList>()); + Console.WriteLine("\nBreadth-first:"); + Tree.Traverse(t, act, new LinkedList>()); + Console.WriteLine(); + } + + // Build n-node tree with root numbered b and other nodes numbered b+1..b+n + public static Tree MakeTree(int b, int n) + { + if (n == 0) + return null; + else + { + int k = n / 2; + Tree t1 = MakeTree(b + 1, k), t2 = MakeTree(b + k + 1, n - 1 - k); + return new Tree(b, t1, t2); + } + } + } + + class Tree + { + private T val; + private Tree t1, t2; + public Tree(T val) : this(val, null, null) { } + public Tree(T val, Tree t1, Tree t2) + { + this.val = val; this.t1 = t1; this.t2 = t2; + } + + public static void DepthFirst(Tree t, Act act) + { + IStack> work = new ArrayList>(); + work.Push(t); + while (!work.IsEmpty) + { + Tree cur = work.Pop(); + if (cur != null) + { + work.Push(cur.t2); + work.Push(cur.t1); + act(cur.val); + } + } + } + + public static void BreadthFirst(Tree t, Act act) + { + IQueue> work = new CircularQueue>(); + work.Enqueue(t); + while (!work.IsEmpty) + { + Tree cur = work.Dequeue(); + if (cur != null) + { + work.Enqueue(cur.t1); + work.Enqueue(cur.t2); + act(cur.val); + } + } + } + + public static void Traverse(Tree t, Act act, IList> work) + { + work.Clear(); + work.Add(t); + while (!work.IsEmpty) + { + Tree cur = work.Remove(); + if (cur != null) + { + if (work.FIFO) + { + work.Add(cur.t1); + work.Add(cur.t2); + } + else + { + work.Add(cur.t2); + work.Add(cur.t1); + } + act(cur.val); + } + } + } + } } \ No newline at end of file