I am trying to implement a Tree Traversal PreOrder using a return return that returns IEnumerable
private IEnumerable<T> Preorder(Node<T> node) { while(node != null) { yield return node.Data; yield return node.LeftChild.Data; yield return node.RightChild.Data; } }
In this case, it goes into an infinite loop, and yes, I know that I need to continue moving. How can I do that?
If LeftChild or RightChild is NULL, throws a null exception. I think at this moment I need a break output;
I assume that both the order and the order after recording will be similar, any ideas?
I have a recursive version that works well.
public void PreOrderTraversal(Node<T> node) { if(node!=null) { Console.Write(node.Data); } if (node.LeftChild != null) { PreOrderTraversal(node.LeftChild); } if (node.RightChild != null) { PreOrderTraversal(node.RightChild); } }
Thanks.
Darthvader
source share