Chapter 12


Listing B.3. Binary Tree and Pair

public enum PairItem {     First,     Second } ______________________________________________________________________________ ______________________________________________________________________________ interface IPair<T> {     T First     {         get;         set;     }     T Second     {         get;         set;     }     T this[PairItem index]     {        get;        set;     } } using System.Collections; using System.Collections.Generic; public struct Pair<T> : IPair<T>, IEnumerable<T> {    public Pair(T first)    {        _First = first;        _Second = default(T);    }    public Pair(T first, T second)    {        _First = first;        _Second = second;    }    public T First    {        get        {            return _First;        }        set        {            _First = value;        }  }  private T _First;  public T Second  {      get      {          return _Second;      }      set      {          _Second = value;      }  }  private T _Second;  [System.Runtime.CompilerServices.IndexerName("Entry")]  public T this[PairItem index]  {      get      {          switch (index)          {              case PairItem.First:              return First;          case PairItem.Second:              return Second;          default:              throw new NotImplementedException(                  string.Format(                  "The enum {0} has not been implemented",                  index.ToString()));          }      }      set      {          switch (index)          {              case PairItem.First:                  First = value;                  break;              case PairItem.Second:                  Second = value;                  break;              default:                  throw new NotImplementedException(                      string.Format(                      "The enum {0} has not been implemented",                      index.ToString()));             }        }  } #region IEnumerable<T> Members public IEnumerator<T> GetEnumerator() {     yield return First;     yield return Second; } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() {     return GetEnumerator(); } #endregion public IEnumerable<T> GetReverseEnumerator() {     yield return Second;     yield return First; }     // Listing 12.24     public IEnumerable<T> GetNotNullEnumerator()     {         if ((First == null) || (Second == null))         {             yield break;         }         yield return Second;         yield return First;     } } ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ using System.Collections; using System.Collections.Generic; public interface IBinaryTree<T> {     T Item     {         get;         set;     }     Pair<IBinaryTree<T>> SubItems     {         get;         set;     } } public class BinaryTree<T> : IEnumerable<T> {     public BinaryTree(T value)     {        Value = value;     }     public T Value     {         get { return _Value; }         set { _Value = value; }      }      private T _Value;      public Pair<BinaryTree<T>> SubItems      {          get { return _SubItems; }          set          {              IComparable first;              first = (IComparable)value.First.Value;              if (first.CompareTo(value.Second.Value) < 0)              {                  // first is less than second.              }              else              {                  // first and second are the same or                  // second is less than first.              }              _SubItems = value;          }     }     private Pair<BinaryTree<T>> _SubItems;     public T this[params PairItem[] branches]     {         get         {             BinaryTree<T> currentNode = this;             int totalLevels =                 (branches == null) ? 0 : branches.Length;             int currentLevel = 0;             while (currentLevel < totalLevels)             {                 currentNode =                     currentNode.SubItems[branches[currentLevel]];                 if (currentNode == null)                 {                     // The binary tree at this location is null.                     throw new IndexOutOfRangeException();                 }                 currentLevel++;             }             return currentNode.Value;        } } #region IEnumerable<T> // Listing 12.22 public IEnumerator<T> GetEnumerator() {     // Return the item at this node.     yield return Value;     // Iterate through each of the elements in the pair.     foreach (BinaryTree<T> tree in SubItems)     {         if (tree != null)         {         // Since each element in the pair is a tree,         // traverse the tree and yield each         // element.         foreach (T item in tree)         {             yield return item;         }       }     }   }   #endregion IEnumerable<T>   #region IEnumerable Members   IEnumerator IEnumerable.GetEnumerator()   {       return GetEnumerator();   }   #endregion }




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net