Hi all!
I'm working on an AVL Tree. The tree itself seems to be working but I need a iterator to walk through the values of the tree. Therefore I tried to implement the IEnumerator interace. Unfortunately I get a compile time error implementing IEnumerator and IComparable. First the code and below that the error.
class AvlTreePreOrderEnumerator<T> : IEnumerator<T> where T :IComparable<T>
{
private AvlTreeNode<T> current = default(T);
private AvlTreeNode<T> tree = null;
private Queue<AvlTreeNode<T>> traverseQueue = null;
public AvlTreePreOrderEnumerator(AvlTreeNode<T> tree)
{
this.tree = tree;
//Build queue
traverseQueue = new Queue<AvlTreeNode<T>>();
visitNode(this.tree.Root);
}
private void visitNode(AvlTreeNode<T> node)
{
if (node == null)
return;
else
{
traverseQueue.Enqueue(node);
visitNode(node.LeftChild);
visitNode(node.RightChild);
}
}
public T Current
{
get { return current.Value; }
}
object IEnumerator.Current
{
get { return Current; }
}
public void Dispose()
{
current = null;
tree = null;
}
public void Reset()
{
current = null;
}
public bool MoveNext()
{
if (traverseQueue.Count > 0)
current = traverseQueue.Dequeue();
else
current = null;
return (current != null);
}
}
The error given by VS2008: Error 1 The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Opdr2_AvlTreeTest_Final.AvlTreeNode'. There is no boxing conversion or type parameter conversion from 'T' to 'System.IComparable'.
This error is given on the following lines:
//members
private AvlTreeNode<T> current = default(T); //current highlighted
private AvlTreeNode<T> tree = null; //tree highlighted
private Queue<AvlTreeNode<T>> traverseQueue = null; //traverseQueue highlighted
//Constructor
public AvlTreePreOrderEnumerator(AvlTreeNode<T> tree) // AvlTreePreOrderEnumerator highlighted
//Method
private void visitNode(AvlTreeNode<T> node) //visitNode highlighted
For now I've not included the tree and node logic. I anybody thinks it is necessary to resolve this problem, just say so!
Thx!