Enumerator a BinaryTreeGeneric

it2022-05-09  22

code   1using System;  2using System.Collections;  3using System.Collections.Generic;  4  5class Program  6{  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 36public class BinaryTree<T> : IEnumerable<T> 37{ 38    public BinaryTree(T value) 39    { 40        Value = value; 41    } 42 43    public T Value 44    { 45        get return _value; } 46        set { _value = value; } 47    } 48    private T _value; 49 50    public Pair<BinaryTree<T>> SubItems 51    { 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    #endregion 85} 86 87interface IPair<T> 88{ 89    T First 90    { 91        get; 92    } 93    T Second 94    { 95        get; 96    } 97    T this[PairItem index] 98    { 99        get;100    }101}102103public enum PairItem104{105    First,106    Second107}108109public 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;134135    [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    }171172    IEnumerable 成员#region IEnumerable<T> 成员173174    public IEnumerator<T> GetEnumerator()175    {176        yield return First;177        yield return Second;178    }179180    #endregion181182    IEnumerable 成员#region IEnumerable 成员183184    IEnumerator IEnumerable.GetEnumerator()185    {186        return GetEnumerator();                                      187    }188189    #endregion190} 

转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/05/11/742700.html

相关资源:MC Virtual Usb Bus Enumerator驱动程序

最新回复(0)