SE450: A Tree: External Iterator in CSharp [11/25] Previous pageContentsNext page

External iterators are much easier to write in C#, which maintains the stack for you. Rather than return, use yield.

file:csharp/tree.cs [source]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// from http://en.csharp-online.net/ECMA-334:_26.4_Implementation_example
// See also http://msdn.microsoft.com/msdnmag/issues/06/00/C20/default.aspx

using System;
using System.Collections;
using System.Collections.Generic;

class Tree<T> : IEnumerable<T>
{
  private T value;
  private Tree<T> left;
  private Tree<T> right;
  public Tree(T value, Tree<T> left, Tree<T> right) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
  // Returns the elements postorder
  IEnumerator<T> IEnumerable<T>.GetEnumerator() {
    if (left != null) foreach (T x in left) yield return x;
    if (right != null) foreach (T x in right) yield return x;
    yield return value;
  }
  IEnumerator IEnumerable.GetEnumerator() {
    return  ((IEnumerable<T>)this).GetEnumerator();
  }
}

class Program
{
  private static Tree<T> MakeTree<T>(params T[] items) {
    return MakeTree(items, 0, items.Length - 1);
  }
  private static Tree<T> MakeTree<T>(T[] items, int left, int right) {
    if (left > right) return null;
    int i = (left + right) / 2;
    return new Tree<T>(items[i],
                       MakeTree(items, left, i - 1),
                       MakeTree(items, i + 1, right));
  }


  public static void Main() {
    {
      Tree<int> t = MakeTree(1, 2, 3, 4, 5, 6, 7, 8, 9);
      foreach (int i in t) Console.Write("{0} ", i);
      Console.WriteLine();
    }
    {
      Tree<string> t = MakeTree("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
      foreach (string s in t) Console.Write("{0} ", s);
      Console.WriteLine();
    }
  }
}

Previous pageContentsNext page