Domanda

Sto lavorando su un albero AVL. L'albero in sé sembra funzionare, ma ho bisogno di un iteratore di camminare attraverso i valori del albero. Perciò ho cercato di attuare l'interace IEnumerator. Purtroppo ho un errore di tempo di compilazione attuazione IEnumerator e IComparable. In primo luogo il codice e al di sotto che l'errore.

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);
    }
}

L'errore data dal VS2008: Errore 1 Il tipo 'T' non può essere utilizzato come parametro di tipo 'T' nel tipo generico o metodo 'Opdr2_AvlTreeTest_Final.AvlTreeNode'. Non v'è alcuna conversione di conversione di pugilato o parametro di tipo da 'T' a 'System.IComparable'.

Questo errore è dato sulle seguenti linee:

    //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  

Per il momento non ho compreso la logica dell'albero e del nodo. Ho qualcuno pensa sia necessario per risolvere questo problema, basta dirlo!

Thx!

È stato utile?

Soluzione

Potresti provare a cambiare a questo

class AvlTreePreOrderEnumerator<T> : IEnumerator<T> where T :IComparable

Altri suggerimenti

Ho il sospetto che avete dichiarato la vostra classe nodo come questo:

public class AvlTreeNode<T> where T : IComparable<AvlTreeNode<T>> {
    public AvlTreeNode<T> Root;
    public AvlTreeNode<T> LeftChild {get;set;}
    public AvlTreeNode<T> RightChild {get;set;}
    public T Value { get; set;}
}

Prova questo (cambio tipo di parametro IComparable):

public class AvlTreeNode<T> where T : IComparable<T> {
    public AvlTreeNode<T> Root;
    public AvlTreeNode<T> LeftChild {get;set;}
    public AvlTreeNode<T> RightChild {get;set;}
    public T Value { get; set;}
}

È inoltre necessario modificare il campo current a questo:

private AvlTreeNode<T> current = new AvlTreeNode<T>();

Anche se non avete pubblicato il codice vero e proprio causa l'errore (Opdr2_AvlTreeTest_Final.AvlTreeNode) eppure, ho il forte sospetto che il problema è che si sta utilizzando questa classe in un generico metodo / tipo con un parametro di tipo che non è costretto a implementare IComparable<T>.

È in grado di riprodurre un errore simile con un semplice esempio:

// no problem here at definition site:
void IsLarger<T>(T a, T b) where T : IComparable<T> {
   return a.CompareTo(b) > 0;
}

void Test<T>(T arg) { // note: T is not necessarily IComparable<T>
   Console.WriteLine(IsLarger(arg, arg)); // The compiler shouldn't allow this.
}

Come sottolineato già, non è stato incluso tutto il codice, ma solo costretto a T IComparable , mentre l'errore che hai elencato è che è vuole T implementare IComparable (vale a dire la forma non generico di IComparable)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top