© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
5 replies
xyrile

❔ Design pattern Iterator

So I was working with
Node<T>
Node<T>
in PreOrder Traversal when i try to test it gives me an error message saying
Something is wrong in our systems, your code took too long to execute.
Something is wrong in our systems, your code took too long to execute.


Here's my code
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<T> PreOrder
        {
        get
            {
                return new PreOrderIterator<T>(this);
            }
        }
    }
    public class PreOrderIterator<T> : IEnumerable<T>
    {
        private Stack<Node<T>> stack = new Stack<Node<T>>();
        public PreOrderIterator(Node<T> root)
        {
            stack.Push(root);
        }
        public IEnumerator<T> GetEnumerator()
        {
            return GetEnumerator();
        }

        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<T> PreOrder
        {
        get
            {
                return new PreOrderIterator<T>(this);
            }
        }
    }
    public class PreOrderIterator<T> : IEnumerable<T>
    {
        private Stack<Node<T>> stack = new Stack<Node<T>>();
        public PreOrderIterator(Node<T> root)
        {
            stack.Push(root);
        }
        public IEnumerator<T> GetEnumerator()
        {
            return GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Design Pattern
C#CC# / help
3y ago
Design pattern
C#CC# / help
4y ago
✅ Design Pattern Advice
C#CC# / help
3y ago
❔ HeadFirst Design Pattern
C#CC# / help
3y ago