Enumerator a BinaryTreeGeneric

 code
code1
 using System;
using System;2
 using System.Collections;
using System.Collections;3
 using System.Collections.Generic;
using System.Collections.Generic;4

5
 class Program
class Program6


 {
{7

8
 static void Main()
    static void Main()9

 
     {
{10
 // JFK
        // JFK 11
 BinaryTree<string> jfkFamilyTree = new BinaryTree<string>(
        BinaryTree<string> jfkFamilyTree = new BinaryTree<string>(12
 "John Fitzgerald Kennedy");
            "John Fitzgerald Kennedy");13
 jfkFamilyTree.SubItems = new Pair<BinaryTree<string>>(
        jfkFamilyTree.SubItems = new Pair<BinaryTree<string>>(14
 new BinaryTree<string>("Joseph Patrick Kennedy"),
            new BinaryTree<string>("Joseph Patrick Kennedy"),15
 new BinaryTree<string>("Rose Elizabeth Fitzgerald"));
            new BinaryTree<string>("Rose Elizabeth Fitzgerald"));16
 // Grandparents (Father's side)
        // Grandparents (Father's side) 17
 jfkFamilyTree.SubItems.First.SubItems =
        jfkFamilyTree.SubItems.First.SubItems =18
 new Pair<BinaryTree<string>>(
            new Pair<BinaryTree<string>>(19
 new BinaryTree<string>("Patrick Joseph Kennedy"),
            new BinaryTree<string>("Patrick Joseph Kennedy"),20
 new BinaryTree<string>("Mary Augusta Hickey"));
            new BinaryTree<string>("Mary Augusta Hickey"));21
 // Grandparents (Mother's side)
        // Grandparents (Mother's side) 22
 jfkFamilyTree.SubItems.Second.SubItems =
        jfkFamilyTree.SubItems.Second.SubItems =23
 new Pair<BinaryTree<string>>(
            new Pair<BinaryTree<string>>(24
 new BinaryTree<string>("John Francis Fitzgerald"),
            new BinaryTree<string>("John Francis Fitzgerald"),25
 new BinaryTree<string>("Mary Josephine Hannon"));
            new BinaryTree<string>("Mary Josephine Hannon"));26

27
 foreach (string name in jfkFamilyTree)
        foreach (string name in jfkFamilyTree)28

 
         {
{29
 Console.WriteLine(name);
            Console.WriteLine(name);30
 }
        }31

32
 Console.ReadKey();
        Console.ReadKey();            33
 }
    }34
 }
}35

36
 public class BinaryTree<T> : IEnumerable<T>
public class BinaryTree<T> : IEnumerable<T>37


 {
{38
 public BinaryTree(T value)
    public BinaryTree(T value)39

 
     {
{40
 Value = value;
        Value = value;41
 }
    }42

43
 public T Value
    public T Value44

 
     {
{45

 get
        get  { return _value; }
{ return _value; }46

 set
        set  { _value = value; }
{ _value = value; }47
 }
    }48
 private T _value;
    private T _value;49

50
 public Pair<BinaryTree<T>> SubItems
    public Pair<BinaryTree<T>> SubItems51

 
     {
{52

 get
        get  { return _subItems; }
{ return _subItems; }53

 set
        set  { _subItems = value; }
{ _subItems = value; }54
 }
    }55
 private Pair<BinaryTree<T>> _subItems;
    private Pair<BinaryTree<T>> _subItems;56

57

 IEnumerable 成员#region IEnumerable 成员
    IEnumerable 成员#region IEnumerable 成员58

59
 IEnumerator IEnumerable.GetEnumerator()
    IEnumerator IEnumerable.GetEnumerator()60

 
     {
{61
 return GetEnumerator();
        return GetEnumerator();62
 }
    }63

64
 public IEnumerator<T> GetEnumerator()
    public IEnumerator<T> GetEnumerator()65

 
     {
{66
 // Return the item at this node.
        // Return the item at this node. 67
 yield return Value;
        yield return Value;68
 // Iterate through each of the elements in the pair.
        // Iterate through each of the elements in the pair. 69
 foreach (BinaryTree<T> tree in SubItems)
        foreach (BinaryTree<T> tree in SubItems)70

 
         {
{71
 if (tree != null)
            if (tree != null)72

 
             {
{73
 // Since each element in the pair is a tree,
                // Since each element in the pair is a tree,      74
 // traverse the tree and yield each
                // traverse the tree and yield each               75
 // element.
                // element.                                       76
 foreach (T item in tree)
                foreach (T item in tree)77

 
                 {
{78
 yield return item;
                    yield return item;79
 }
                }80
 }
            }81
 }
        }82
 }
    }83

84
 #endregion
    #endregion85
 }
}86

87
 interface IPair<T>
interface IPair<T>88


 {
{89
 T First
    T First90

 
     {
{91
 get;
        get;92
 }
    }93
 T Second
    T Second94

 
     {
{95
 get;
        get;96
 }
    }97
 T this[PairItem index]
    T this[PairItem index]98

 
     {
{99
 get;
        get;100
 }
    }101
 }
}102

103
 public enum PairItem
public enum PairItem104


 {
{105
 First,
    First,106
 Second
    Second107
 }
}108

109
 public struct Pair<T> : IPair<T>, IEnumerable<T>
public struct Pair<T> : IPair<T>, IEnumerable<T>110


 {
{111
 public Pair(T first, T second)
    public Pair(T first, T second)112

 
     {
{113
 _first = first;
        _first = first;114
 _second = second;
        _second = second;115
 }
    }116
 public T First
    public T First117

 
     {
{118

 get
        get  { return _first; }
{ return _first; }119
 private set
        private set120

 
         {
{121
 _first = value;
            _first = value;122
 }
        }123
 }
    }124
 private T _first;
    private T _first;125
 public T Second
    public T Second126

 
     {
{127

 get
        get  { return _second; }
{ return _second; }128
 private set
        private set129

 
         {
{130
 _second = value;
            _second = value;131
 }
        }132
 }
    }133
 private T _second;
    private T _second;134

135
 [System.Runtime.CompilerServices.IndexerName("Entry")]
    [System.Runtime.CompilerServices.IndexerName("Entry")]136
 public T this[PairItem index]
    public T this[PairItem index]137

 
     {
{138
 get
        get139

 
         {
{140
 switch (index)
            switch (index)141

 
             {
{142
 case PairItem.First:
                case PairItem.First:143
 return First;
                    return First;144
 case PairItem.Second:
                case PairItem.Second:145
 return Second;
                    return Second;146
 default:
                default:147
 throw new NotImplementedException(
                    throw new NotImplementedException(148
 string.Format(
                    string.Format(149
 "The enum {0} has not been implemented",
                    "The enum {0} has not been implemented",150
 index.ToString()));
                    index.ToString()));151
 }
            }152
 }
        }153
 set
        set154

 
         {
{155
 switch (index)
            switch (index)156

 
             {
{157
 case PairItem.First:
                case PairItem.First:158
 First = value;
                    First = value;159
 break;
                    break;160
 case PairItem.Second:
                case PairItem.Second:161
 Second = value;
                    Second = value;162
 break;
                    break;163
 default:
                default:164
 throw new NotImplementedException(
                    throw new NotImplementedException(165
 string.Format(
                    string.Format(166
 "The enum {0} has not been implemented",
                    "The enum {0} has not been implemented",167
 index.ToString()));
                    index.ToString()));168
 }
            }169
 }
        }170
 }
    }171

172

 IEnumerable
    IEnumerable173

174
 public IEnumerator<T> GetEnumerator()
    public IEnumerator<T> GetEnumerator()175

 
     {
{176
 yield return First;
        yield return First;177
 yield return Second;
        yield return Second;178
 }
    }179

180
 #endregion
    #endregion181

182

 IEnumerable 成员#region IEnumerable 成员
    IEnumerable 成员#region IEnumerable 成员183

184
 IEnumerator IEnumerable.GetEnumerator()
    IEnumerator IEnumerable.GetEnumerator()185

 
     {
{186
 return GetEnumerator();
        return GetEnumerator();                                      187
 }
    }188

189
 #endregion
    #endregion190
 }
} .jpg) 
  
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号