0

// the program not print OutPut

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Node
    {
        public int Data;
        public Node Left;
        public Node Right;

        public void DisplayNode()
        {
            Console.Write(Data + " ");
        }
    }
    public class BinarySearchTree
    {
        public Node root;
        public BinarySearchTree()
        {
            root = null;
        }
        public void Insert(int i)
        {
            Node newNode = new Node();
            newNode.Data = i;
            if (root == null)
                root = newNode;
            else
            {
                Node current = root;
                Node parent;
                while (true)
                {
                    parent = current;
                    if (i < current.Data)
                    {
                        current = current.Left;
                        if (current == null)
                        {
                            parent.Left = newNode;
                            break;
                        }
                        else
                        {
                            current = current.Right;
                            if (current == null)
                            {
                                parent.Right = newNode;
                                break;
                            }
                        }
                    }
                }
            }
        }
        

            public void InOrder(Node theRoot)
            {
                if (!(theRoot == null))
                {
                    InOrder(theRoot.Left);
                    theRoot.DisplayNode();
                    InOrder(theRoot.Right);
                }
            }


            static void Main()
            {
                BinarySearchTree nums = new BinarySearchTree();
                nums.Insert(23);
                nums.Insert(45);
                nums.Insert(16);
                nums.Insert(37);
                nums.Insert(3);
                nums.Insert(99);
                nums.Insert(22);
                Console.WriteLine("Inorder traversal: "); 
                nums.InOrder(nums.root);

            }

        }
    }
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Momerath
0

You have a problem with your InsertNode method. It can't insert right side nodes since there is no 'else' for the line 39. You need to go over your closing } characters as you are missing one at one point and have an extra one at another.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.