Simple Binary Search Tree At the moment I am learning algorithms and here I am trying to implement a simple binary search tree. I would like to know your suggestions on whether I am on the right track or not, and how this can be improved.

BinaryTree.cs

class BinaryTree 
{
    private Node root;
    private int count;

    public BinaryTree()
    {
        root = null;
        count = 0;
    }
    public bool isEmpty()
    {
        return root == null;
    }

    public void insert(int d)
    {
        if (isEmpty())
        {
            root = new Node(d);
        }
        else
        {
            root.insertData(ref root, d);
        }

        count++;
    }

    public bool search(int s)
    {
        return root.search(root, s);
    }

    public bool isLeaf()
    {
        if(!isEmpty())
        return root.isLeaf(ref root);

        return true;
    }

    public void display()
    {
        if(!isEmpty())
        root.display(root);
    }

    public int Count()
    {
        return count;
    }
}


Node.cs

class Node
{
    private int number;
    public Node rightLeaf;
    public Node leftLeaf;

    public Node(int value)
    {
        number = value;
        rightLeaf = null;
        leftLeaf = null;
    }

    public bool isLeaf(ref Node node)
    {
        return (node.rightLeaf == null &amp;&amp; node.leftLeaf == null);

    }

    public void insertData(ref Node node, int data)
    {
        if (node == null)
        {
            node = new Node(data);

        }
        else if (node.number &lt; data)
        {
            insertData(ref node.rightLeaf, data);
        }

        else if (node.number &gt; data)
        {
            insertData(ref node.leftLeaf, data);
        }
    }

    public bool search(Node node, int s)
    {
        if (node == null)
            return false;

        if (node.number == s)
        {
            return true;
        }
        else if (node.number &lt; s)
        {
           return search(node.rightLeaf, s);
        }
        else if (node.number &gt; s)
        {
            return search(node.leftLeaf, s);
        }

        return false;
    }

    public void display(Node n)
    {
        if (n == null)
            return;

        display(n.leftLeaf);
        Console.Write(" " + n.number);
        display(n.rightLeaf);
    }

}


Program.cs

class Program
{
    static void Main(string[] args)
    {
        BinaryTree b = new BinaryTree();

        b.insert(1);
        b.insert(6);
        b.insert(2);
        b.insert(4);
        b.insert(5);
        b.insert(3);

        b.display();

        Console.ReadLine();
    }
}


Output


  1 2 3 4 5 6
I don't quite understand why you'd use a ref here. 


public void insertData(ref Node node, int data)
{
    if (node == null)
    {
        node = new Node(data);

    }
    else if (node.number &lt; data)
    {
        insertData(ref node.rightLeaf, data);
    }

    else if (node.number &gt; data)
    {
        insertData(ref node.leftLeaf, data);
    }
}



Why not just return the node?

public Node insertData(Node node, int data)
{
    if (node == null)
    {
        return new Node(data);

    }

    if (node.number &lt; data)
    {
        return insertData(node.rightLeaf, data);
    }

    if (node.number &gt; data)
    {
        return insertData(node.leftLeaf, data);
    }
}




Your capitalization is consistent, but slightly confusing and non-standard. For example, both your private fields and public methods are camelCase. 


   public void insert(int d)
    {
        if (isEmpty())
        {
            root = new Node(d);
        }



This would be perfectly fine if you distinguished the field by using the this keyword or an underscore to make it look different from your methods. 

    public void insert(int d)
    {
        if (isEmpty())
        {
            _root = new Node(d);
        }


Or

    public void insert(int d)
    {
        if (isEmpty())
        {
            this.root = new Node(d);
        }


But like I said, it's non-standard. The standard is to use PascalCase for methods, camelCase for local vars, and _underscoredCamelCase for private fields. 

    public void Insert(int d)
    {
        if (IsEmpty())
        {
            _root = new Node(d);
        }