Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

binary search tree c# stackoverflow

class Node
{
    public int data;
    public Node left, right;

    public Node(int data)
    {
        this.data = data;
        left = null;
        right = null;

    }
}

class BinaryTreeImp
{
    Node root;
    static int count = 0;

    public BinaryTreeImp()
    {
        root = null;

    }
    public Node addNode(int data)
    { 
        Node newNode = new Node(data);

        if (root == null)
        {
            root = newNode;

        }
        count++;
        return newNode;


    }

    public void insertNode(Node root,Node newNode )
    {
        Node temp;
        temp = root;

        if (newNode.data < temp.data)
            {
                if (temp.left == null)
                {
                    temp.left = newNode;

                }

                else
                {
                    temp = temp.left;
                    insertNode(temp,newNode);

                }
            }
            else if (newNode.data > temp.data)
            {
                if (temp.right == null)
                {
                    temp.right = newNode;

                }

                else 
                {
                    temp = temp.right;
                    insertNode(temp,newNode);
                }
            }
        }


    public void displayTree(Node root)
    {
        Node temp;
        temp = root;

        if (temp == null)
            return;
            displayTree(temp.left);
            System.Console.Write(temp.data + " ");
            displayTree(temp.right);


    }

    static void Main(string[] args)
    {
       BinaryTreeImp btObj = new BinaryTreeImp();
       Node iniRoot= btObj.addNode(5);


       btObj.insertNode(btObj.root,iniRoot);
       btObj.insertNode(btObj.root,btObj.addNode(6));
       btObj.insertNode(btObj.root,btObj.addNode(10));
       btObj.insertNode(btObj.root,btObj.addNode(2));
       btObj.insertNode(btObj.root,btObj.addNode(3));
       btObj.displayTree(btObj.root);

       System.Console.WriteLine("The sum of nodes are " + count);
       Console.ReadLine();

    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# switch two values 
Csharp :: c# how to group console output into columns 
Csharp :: c# place all keys in dictionary into array 
Csharp :: c# list add and return 
Csharp :: webtest fullscreen extend window maximize 
Csharp :: ignore warning openxml 
Csharp :: c# printwindow chrome 
Csharp :: c# make two checkbox uncheckable both 
Csharp :: create shortcut C# WPF 
Csharp :: source a listbox by property of object c# 
Csharp :: how to add a round image unity 
Csharp :: unity wheelcollider antiroll 
Csharp :: Transparent UserControl 
Csharp :: Write N lines with M numbers each that describe the layout of the second layer in the way shown above 
Csharp :: C# look through object 
Csharp :: tab key navigation C# winforms 
Csharp :: upcasting and downcasting in c# 
Csharp :: embergene 
Csharp :: how to define a static color resource in xaml wpf 
Csharp :: getString 
Csharp :: stringbuilder sb = new stringbuilder(reallylongstring); you need to identify whether a string stored in an object named stringtofind is within the stringbuilder sb object. 
Csharp :: c# convert datatable to csv 
Csharp :: unity c# bool to int conversion 
Csharp :: c# stream 
Csharp :: deserialize list of objects c# 
Csharp :: unity iap 
Csharp :: inverse kinematics not working unity 
Csharp :: como guardar archivo en un botón asp.net 
Html :: how to use unsplash images in html 
Html :: multipart form 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =