Mam sobie 3 klasy: BinaryTreeNode, BST i BSTdfs, wszystkie generyczne. BST jest kolekcją elementów, BSTdfs ma w zamierzeniu dodawać możliwość iterowania po drzewku BST w porządku preorder.
class BinaryTreeNode<T>
{
public T key;
public BinaryTreeNode<T> left, right, up;
}
class BST<T> where T : IComparable<T>
{
//tu implemntacja bst która jest OK
}
class BSTdfs<T> : BST<T>, IEnumerable<T>,IEnumerator<T> where T : IComparable<T>
{
protected Stack<BinaryTreeNode<T>> S;
public BSTdfs()
{
S = new Stack<BinaryTreeNode<T>>();
}
public IEnumerator<T> GetEnumerator()
{
return (IEnumerator<T>)this;
}
public bool MoveNext()
{
var curr = S.Pop();
if (curr.right != null)
S.Push(curr.right);
if (curr.left != null)
S.Push(curr.left);
return S.Count() != 0;
}
public void Reset()
{
S.Push(root);
}
public object Current
{
get
{
return S.Peek().key;
}
}
}
Kod się nie kompiluje, tzn. wyrzuca mi komunikaty:
- doesn't implement interface member IDispose.Disopse() (wtf?)
- Current doesn't have matching return type of T (tutaj zmieniając typ current z object na T dotaje analogiczny kominikat tylko have matching return type of object)
- GetEnumerator doesn't have matching return type of IEnumerator (kolejne wtf?)
Generalnie mało w sieci jest na temat implementacji IEnumerable, ja korzystałem z tych porad:
<url> http://www.c-sharpcorner.com/UploadFile/prasadh/Enumerators11132005232321PM/Enumerators.aspx </url>