Facebook
From Paweł Pohl, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 120
  1. '''
  2. class OysterIterator : IIterator<BSTNode>
  3.         {
  4.                 enum NodeType
  5.                 {
  6.                         Left, Right, Self, None
  7.                 }
  8.                 private BSTNode Root { get; set; }
  9.                 private OysterIterator Iterator { get; set; }
  10.                 private NodeType NodeToGet { get; set; }
  11.                 private bool HasParent { get; set; }
  12.  
  13.  
  14.                 public OysterIterator(BSTNode root)
  15.                 {
  16.                         Root = root;
  17.                         Reset();
  18.                 }
  19.  
  20.                 private OysterIterator(BSTNode root, bool hasParent): this(root)
  21.                 {
  22.                         HasParent = hasParent;
  23.                 }
  24.  
  25.                 // Left branch -> Root -> Right branch -> Null/Reset
  26.                 public BSTNode Next()
  27.                 {
  28.                         if (NodeToGet == NodeType.None)
  29.                                 if (!HasParent) // Only the top root should reset the iterator
  30.                                 {
  31.                                         Reset();
  32.                                         if (NodeToGet == NodeType.None) // After reset it can still be empty (so the collection is empty)
  33.                                                 return null;
  34.                                 }
  35.                                 else
  36.                                         return null;
  37.                         if (NodeToGet == NodeType.Self)
  38.                         {
  39.                                 if (Root.Right == null)
  40.                                         NodeToGet = NodeType.None;
  41.                                 else
  42.                                 {
  43.                                         NodeToGet = NodeType.Right;
  44.                                         Iterator = new OysterIterator(Root.Right, true);
  45.                                 }
  46.                                 return Root;
  47.                         }
  48.  
  49.                         var node = Iterator.Next();
  50.                         if (node == null)
  51.                         {
  52.                                 if (NodeToGet == NodeType.Left)
  53.                                         NodeToGet = NodeType.Self;
  54.                                 else // right node returned null
  55.                                         NodeToGet = NodeType.None;
  56.                                 node = Next();
  57.                         }
  58.                         return node;
  59.                 }
  60.  
  61.                 public void Reset()
  62.                 {
  63.                         Iterator = null;
  64.                         NodeToGet = NodeType.None;
  65.                         if (Root == null) return;
  66.                         if (Root.Left != null)
  67.                         {
  68.                                 Iterator = new OysterIterator(Root.Left, true);
  69.                                 NodeToGet = NodeType.Left;
  70.                         }
  71.                         else
  72.                                 NodeToGet = NodeType.Self;
  73.                 }
  74.         }
  75. '''