C
C#2y ago
xyrile

Iterator (Argument 1 cannot convert from 'Coding.Exercise.Nodechar[]' to 'char*' Compilation failed)

public class Node<T>
{
public T value;
public Node<T> left, right, parent;

public Node(T value)
{
this.value = value;
}

public Node(T value, Node<T> left, Node<T> right)
{
this.value = value;
this.left = left;
this.right = right;

left.parent = right.parent = this;
}

public IEnumerable<Node<T>> PreOrder
{
get
{
return new PreOrderIterator<T>(this);
}
}
}
public class PreOrderIterator<T> : IEnumerable<Node<T>>
{
private Stack<Node<T>> stack = new Stack<Node<T>>();
public PreOrderIterator(Node<T> root)
{
stack.Push(root);
}
public IEnumerator<Node<T>> GetEnumerator()
{
foreach (Node<T> node in stack)
{
yield return node;
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
public class Node<T>
{
public T value;
public Node<T> left, right, parent;

public Node(T value)
{
this.value = value;
}

public Node(T value, Node<T> left, Node<T> right)
{
this.value = value;
this.left = left;
this.right = right;

left.parent = right.parent = this;
}

public IEnumerable<Node<T>> PreOrder
{
get
{
return new PreOrderIterator<T>(this);
}
}
}
public class PreOrderIterator<T> : IEnumerable<Node<T>>
{
private Stack<Node<T>> stack = new Stack<Node<T>>();
public PreOrderIterator(Node<T> root)
{
stack.Push(root);
}
public IEnumerator<Node<T>> GetEnumerator()
{
foreach (Node<T> node in stack)
{
yield return node;
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
Can someone help me with this ? this is preorder Traverse
9 Replies
ero
ero2y ago
is there a reason you need a separate iterator?
xyrile
xyrileOP2y ago
its part of the coding exercise so i think yes implement preorder traversal that returns a sequence of Ts
ero
ero2y ago
also that code compiles just fine, where is your error?
xyrile
xyrileOP2y ago
Nodechar[]' to 'char*' this i cant jsut figure it out maybe in the yield?
ero
ero2y ago
i mean i would have just done
public IEnumerable<Node<T>> PreOrder
{
get
{
Stack<Node<T>> stack = new();
stack.Push(this);

foreach (Node<T> node in stack)
{
yield return node;
}
}
}
public IEnumerable<Node<T>> PreOrder
{
get
{
Stack<Node<T>> stack = new();
stack.Push(this);

foreach (Node<T> node in stack)
{
yield return node;
}
}
}
xyrile
xyrileOP2y ago
ok let me try
ero
ero2y ago
again, where are you doing this the code you sent doesn't contain an error
xyrile
xyrileOP2y ago
anyways , i messed up already so i will jsut continue this in few hours
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server