Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:20:50+00:00 2026-05-22T23:20:50+00:00

I m trying to implement Tree Traversal PreOrder using yield return which returns an

  • 0

I m trying to implement Tree Traversal PreOrder using yield return which returns an 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 infinite loop and yes I know that I need to keep traversing. How can this be done?

If the LeftChild or RightChild is null, throws a null exception. I think at that point i need yield break;

I assume, inorder and postorder would be similar too, any ideas?

I have the Resursive 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:20:51+00:00Added an answer on May 22, 2026 at 11:20 pm

    Option #1 Recursive

    public class Node<T> : IEnumerable<T>
    {
        public Node<T> LeftChild { get; set; }
    
        public Node<T> RightChild { get; set; }
    
        public T Data { get; set; }
    
        public IEnumerator<T> GetEnumerator()
        {
            yield return Data;
    
            if (LeftChild != null)
            {
                foreach (var child in LeftChild)
                    yield return child;
            }
            if (RightChild != null)
            {
                foreach (var child in RightChild)
                    yield return child;
            }
        }
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    Usage:

    var child1 = new Node<int> { Data = 1 };
    var child2 = new Node<int> { Data = 2 };
    var child3 = new Node<int> { Data = 3, LeftChild = child1 };
    var root = new Node<int> { Data = 4, LeftChild = child3, RightChild = child2 };
    
    foreach (var value in root)
        Console.WriteLine(value);
    

    Option #2 Non-recursive static method

    public static IEnumerable<T> Preorder<T>(Node<T> root)
    {
        var stack = new Stack<Node<T>>();
        stack.Push(root);
    
        while (stack.Count > 0)
        {
            var node = stack.Pop();
            yield return node.Data;
            if (node.RightChild != null)
                stack.Push(node.RightChild);
            if (node.LeftChild != null)
                stack.Push(node.LeftChild);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a method for a binary tree which returns a stream.
I'm trying to figure out how to implement a function in a tree node
I am trying to implement a tree view in my application. I am using
I am trying to implement a leftist tree using heaps as a base class.
I am trying to implement an array based tree where each node has a
I'm trying to implement a tree like structure using a Materialized Path model described
I'm trying to implement a tree-like structure with two classes: Tree and Node .
I'm quite new to C and I'm trying to implement a binary tree in
I am trying to implement a red-black tree in C#. I already created an
I'm trying to implement http://github.com/bengottlieb/Twitter-OAuth-iPhone/tree/master into my code. I'm following the Demo provided and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.