Search code examples
c#binary-search-treeinorder

InOrder traversal is only going to the first left node then an error?


I am trying to do a simple inorder traversal of a BST and the BST builds properly but when I try to print the BST inorder it goes to the first left node and then doesn't go to any other nodes after that and creates an overflow.

Ive tried to do different inorder traversals with getLeft and getRight but it is still doing the same thing

class Program
    {
        static void Main(string[] args)
        {
            Tree tree = new Tree();

            List<int> rands = new List<int>();

            Random random = new Random();

            int between = random.Next(20, 30);

            for (int i = 0; i < between; i++)
            {
                rands.Add(random.Next(100));
            }

            for (int x = 0; x < rands.Count; x++)
            {
                tree.constructTree(rands[x]);
            }

            tree.Inorder(tree.returnRoot());

        }
    class Node
    {
        private int number;
        public Node left;
        public Node right;

        public Node()
        {
        }

        public Node getLeft()
        {
            return this.left;
        }

        public Node getRight()
        {
            return this.right;
        }

        public int GetSetNumber
        {
            get
            {
                return this.number;
            }
            set
            {
                this.number = value;
            }
        }

        public Node GetSetLeft
        {
            get
            {
                return this.left;
            }
            set
            {
                this.left = value;
            }
        }

        public Node GetSetRight
        {
            get
            {
                return this.right;
            }
            set
            {
                this.right = value;
            }
        }
    }



    class Tree
    {
        public Node root;

        public Node returnRoot()
        {
            return this.root;
        }

        public void constructTree(int num)
        {
            Node t = new Node();
            t.GetSetNumber = num;

            if (root == null)
            {
                root = t;
            }
            else
            {
                Node cur = root;
                Node top;

                while (true)
                {
                    top = cur;
                    if (num < top.GetSetNumber)
                    {
                        cur = cur.GetSetLeft;
                        if (cur == null)
                        {
                            top.GetSetLeft = t;
                            return;
                        }
                    }
                    else
                    {
                        cur = cur.GetSetRight;
                        if (cur == null)
                        {
                            top.GetSetRight = t;
                            return;
                        }
                    }
                }

            }
        }

        public void Inorder(Node Root)
        {
            if (root == null)
            {
                return;
            }
            Inorder(root.left);
            Console.WriteLine(root.GetSetNumber + " ");
            Inorder(root.right);
        }

    }

Solution

  • You are referencing root in Inorder, not Root. root (lower-case r) is the class variable containing the root of the entire tree, not the Node parameter you have passed in. Your code loops infinitely because you are calling Inorder on the same node forever.

    If you capitalize your references to root in Inorder (or use a different variable name in the method to avoid confusion, like current) you should be able to make some progress.

        public void Inorder(Node Root)
        {
            if (Root == null)
            {
                return;
            }
            Inorder(Root.left);
            Console.WriteLine(Root.GetSetNumber + " ");
            Inorder(Root.right);
        }