1 2 public class BinaryTree<T> : IEnumerable<T> 3 { 4 5 public BinaryTree(T value) 6 { } 7 public IEnumerator<T> GetEnumerator() 8 { 9 10 } 11 public T Value 12 { 13 get { return _value; } 14 set { _value = value; } 15 } 16 private T _value; 17 18 } 19 public struct Pair<T> 20 { 21 private T _first; 22 private T _second; 23 public T First 24 { 25 get { return _first; } 26 set { _first = value; } 27 } 28 public T Second 29 { 30 get { return _second; } 31 set { _second = value; } 32 } 33 public Pair(T first,T second) 34 { 35 _first = first; 36 _second = second; 37 38 } 39 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 CSharpPrimitiveTypes primitives = new CSharpPrimitiveTypes(); 6 foreach (string primitive in primitives) 7 { 8 Console.WriteLine(primitive); 9 } 10 11 Console.ReadLine(); 12 13 14 } 15 16 17 } 18 19 public class CSharpPrimitiveTypes : IEnumerable<string> 20 { 21 public IEnumerator<string> GetEnumerator() 22 { 23 yield return "object"; 24 yield return "byte"; 25 yield return "uint"; 26 yield return "ulong"; 27 yield return "float"; 28 yield return "char"; 29 yield return "bool"; 30 yield return "string"; 31 } 32 33 //因为System.Collections.Generic.IEnumerable<T>继承 34 //System.Collections.IEnumerable这个接口,所以也需要实现 35 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 36 { 37 return GetEnumerator(); 38 } 39 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Pair<string> fullname = new Pair<string>("xxm", "yyp"); 6 foreach (string name in fullname) 7 { 8 Console.WriteLine(name); 9 } 10 11 Console.ReadLine(); 12 13 14 } 15 16 17 } 18 19 20 public struct Pair<T> : IEnumerable<T> 21 { 22 private T _first; 23 private T _second; 24 public T First 25 { 26 get { return _first; } 27 set { _first = value; } 28 } 29 public T Second 30 { 31 get { return _second; } 32 set { _second = value; } 33 } 34 public Pair(T first, T second) 35 { 36 _first = first; 37 _second = second; 38 } 39 #region IEnumerable<T> 40 public IEnumerator<T> GetEnumerator() 41 { 42 yield return First; 43 yield return Second; 44 } 45 #endregion 46 #region IEnumerable Members 47 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 48 { 49 return GetEnumerator(); 50 } 51 #endregion 52 } 53
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 BinaryTree<string> jfkFamilyTree = new BinaryTree<string>("root");//根节点 6 7 jfkFamilyTree.Subitems = new Pair<BinaryTree<string>>(//左右树的根节点 8 new BinaryTree<string>("root-left"), 9 new BinaryTree<string>("root-right") 10 ); 11 jfkFamilyTree.Subitems.First.Subitems = new Pair<BinaryTree<string>>(//左子树的左右子树的根节点 12 new BinaryTree<string>("root-left-left"), 13 new BinaryTree<string>("root-left-right") 14 ); 15 jfkFamilyTree.Subitems.Second.Subitems = new Pair<BinaryTree<string>>(//右子树的左右子树的根节点 16 new BinaryTree<string>("root-righ-left"), 17 new BinaryTree<string>("root-righ-right") 18 ); 19 20 //遍历二叉树 21 foreach (string node in jfkFamilyTree) 22 { 23 Console.WriteLine(node); 24 } 25 26 27 28 Console.ReadLine(); 29 30 31 } 32 33 34 } 35 36 //二叉树(根节点,左右子树) 37 public class BinaryTree<T> : IEnumerable<T> 38 { 39 40 public BinaryTree(T value) 41 { 42 _value = value; 43 } 44 45 private T _value;//根节点 46 public T Value 47 { 48 get { return _value; } 49 set { _value = value; } 50 } 51 private Pair<BinaryTree<T>> _subitems;//子树,总共两个,左子树和右子数 52 public Pair<BinaryTree<T>> Subitems//子树元素,每一项都是一个树 53 { 54 get { return _subitems; } 55 set 56 { 57 IComparable first; 58 first = (IComparable)value.First.Value; 59 if (first.CompareTo(value.Second.Value) < 0) 60 { 61 //first is less than second. 62 //在这可以做处理,比如交换,或者报错提示 63 //不过值类型赋值不会成功,需要使用其它方式,如换成类而不是struct 64 } 65 else 66 { 67 //first is not less than second. 68 } 69 _subitems = value; 70 } 71 } 72 public IEnumerator<T> GetEnumerator() 73 { 74 yield return Value;//返回根节点 75 76 foreach (BinaryTree<T> tree in Subitems)//遍历所有子树,从左向右遍历 77 { 78 if (tree != null) 79 { 80 foreach (T item in tree)//遍历子树当中的所有节点,会引起BinaryTree<T>中的GetEnumerator()递归调用 81 { 82 yield return item; 83 } 84 85 } 86 } 87 88 89 } 90 #region IEnumerable Members 91 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 92 { 93 return GetEnumerator(); 94 } 95 #endregion 96 97 98 } 99 100 //子树类(左右子树) 101 public struct Pair<T> : IEnumerable<T> 102 { 103 private T _first;//左子树 104 private T _second;//右子树 105 public T First 106 { 107 get { return _first; } 108 set { _first = value; } 109 } 110 public T Second 111 { 112 get { return _second; } 113 set { _second = value; } 114 } 115 116 public Pair(T first, T second) 117 { 118 _first = first; 119 _second = second; 120 } 121 #region IEnumerable<T> 122 public IEnumerator<T> GetEnumerator() 123 { 124 yield return First; 125 yield return Second; 126 } 127 #endregion 128 129 #region IEnumerable Members 130 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 131 { 132 return GetEnumerator(); 133 } 134 #endregion 135 }
1 Pair<string> game = new Pair<string>("first","second"); 2 3 4 foreach (string name in game.GetReverseEnumerator()) 5 { 6 Console.WriteLine(name); 7 } 8 9 public struct Pair<T> : IEnumerable<T> 10 { 11 ... 12 public IEnumerable<T> GetReverseEnumerator() 13 { 14 yield return Second; 15 yield return First; 16 } 17 }