Enumerator a BinaryTreeGeneric
1
using System;2
using System.Collections;3
using System.Collections.Generic;4

5
class Program6


{7

8
static void Main()9

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

27
foreach (string name in jfkFamilyTree)28

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

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

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


{38
public BinaryTree(T value)39

{40
Value = value;41
}42

43
public T Value44

{45

get
{ return _value; }46

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

50
public Pair<BinaryTree<T>> SubItems51

{52

get
{ return _subItems; }53

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

57

IEnumerable 成员#region IEnumerable 成员58

59
IEnumerator IEnumerable.GetEnumerator()60

{61
return GetEnumerator();62
}63

64
public IEnumerator<T> GetEnumerator()65

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

{71
if (tree != null)72

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

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

84
#endregion85
}86

87
interface IPair<T>88


{89
T First90

{91
get;92
}93
T Second94

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

{99
get;100
}101
}102

103
public enum PairItem104


{105
First,106
Second107
}108

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


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

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

{118

get
{ return _first; }119
private set120

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

{127

get
{ return _second; }128
private set129

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

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

{138
get139

{140
switch (index)141

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

{155
switch (index)156

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

172

IEnumerable173

174
public IEnumerator<T> GetEnumerator()175

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

180
#endregion181

182

IEnumerable 成员#region IEnumerable 成员183

184
IEnumerator IEnumerable.GetEnumerator()185

{186
return GetEnumerator(); 187
}188

189
#endregion190
}

浙公网安备 33010602011771号