Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i tried this. i make a class for tree
C#
class BT
    {

        private BT left;
        internal BT Left
        {
            get { return left; }
            set { left = value; }
        }

        private BT right;
        internal BT Right
        {
            get { return right; }
            set { right = value; }
        }

        private int value;
        public int Value
        {
             get { return this.value; }
                set { this.value = value; }
        }

        static private int count=0;
        public static int Count
        {
            get { return BT.count; }
            set { BT.count = value; }
        }

        public BT()
        {
            left = null;
            right = null;
            value = 0;
        }
        public void Add(BT t,int input)
        {
            if (t.value == 0)
            {
                t.value = input;
                t.Left = null;
                t.Right= null;
                count++;
            }
            else if(t.value>=input)
            {
                t.left.value = input;
                t.left.left = null;
                t.right.right = null;
                count++;
            }
            else if(t.value<input)
            {
                t.right.value = input;
                t.left.left = null;
                t.right.right = null;
                count++;
            }
        }
        public void Print(BT tree)
        {
            Form1 o = new Form1();
            if (tree.right != null)
                Print(tree.right);
            o.richTextBox1.AppendText(tree.value.ToString());
            if (tree.left != null)
                Print(tree.left);
        }
    }
}
and call it by this code
C#
BT pNode = new BT();
            pNode.Add(pNode, 5);          
            pNode.Add(pNode, 2);
            pNode.Add(pNode,1);
            pNode.Add(pNode,3);
            pNode.Print(pNode);
it gives run time error "Object reference not set to an instance of an object." on line t.left.value = input;
Posted
Comments
BillWoodruff 6-Nov-11 6:47am    
It might also be useful for you to take a look at: http://www.codeproject.com/KB/recipes/BinarySearchTree.aspx

And for MS's (2005) ideas about binary tree: http://msdn.microsoft.com/en-US/library/ms379573(v=VS.80).aspx

But, applause for "rolling your own." !
BillWoodruff 6-Nov-11 7:07am    
One small additional question: is it your intent (in using the static variable 'count, and its matching static Property) to essentially give a 'unique identifier' to each Node ?

By the way: trust the compiler:

public static int count { get; set; }

Will auto-create the backing-field for you, unless you are using .NET < 3.0.

You are not creating the new objects for your tree. Start by replacing the following code blocks.
C#
else if(t.value>=input)
{
    t.left.value = input;
    t.left.left = null;
    t.right.right = null;
    count++;
}

with
C#
else if(t.value>=input)
{
    t.left = new BT();

    t.left.value = input;
    t.left.left = null;
    t.left.right = null;
    count++;
}

and
C#
else if(t.value<input)
{
    t.right.value = input;
    t.left.left = null;
    t.right.right = null;
    count++;
}

with
C#
else if(t.value<input)
{
    t.right = new BT();

    t.right.value = input;
    t.right.left = null;
    t.right.right = null;
    count++;
}

The added and changed code is bold.

Next to the new statements being added the incorrect left and right branches were used in the null assignments. By the way these assignments are not needed as the BT constructor sets them to null already.
 
Share this answer
 
Comments
Mehdi Gholam 6-Nov-11 5:28am    
5'ed
André Kraak 6-Nov-11 5:30am    
Thanks.
Sweety Khan 6-Nov-11 5:52am    
wOw thanx i understand. see my print function bc nothing is displaying in textbox
André Kraak 6-Nov-11 6:01am    
If nothing is displayed something else is wrong, but as the code is right now it should display 5 and 3. This is because you never get passed the start of the tree.

In your Add function you decide to go right or left. At that point you need to check if there is already a left or right branch present, if this is the case you need to go down that branch. This can be done by calling the Add function again for the branch.
Andre is right to a certain extent, but his solution does not build you a full tree - he overwrites any existing node when you provide a different value.
If your tree values are meant to create a tree such as
    5
   / \
  2
 / \
1   3
Then you need to check is nodes exist already, before creating new ones.
In addition, I would not pass through the node to your add method: make it work on the current instance instead:
SQL
public class BTree
    {
    public int Value { get; set; }
    public BTree Left { get; set; }
    public BTree Right { get; set; }
    public BTree() : this(0) { }
    public BTree(int val)
        {
        Value = val;
        }
    public void Add(int val)
        {
        if (Value == 0)
            {
            Value = val;
            }
        else if (Value > val)
            {
            if (Left == null)
                {
                Left = new BTree(val);
                }
            else
                {
                Left.Add(val);
                }
            }
        else
            {
            if (Right == null)
                {
                Right = new BTree(val);
                }
            else
                {
                Right.Add(val);
                }
            }
        }
    }
Two other things occur:
1) Your Count is pretty useless: it returns the total number of nodes that have been cretaed, rather than the number which are in use, or which are involved in this tree. If you overwrite a node (as Andre does in his example), it does not decrease the count. You would be better off implementing a Count property which counted the number of nodes in teh tree from this point:
C#
public int Count
    {
    get
        {
        int count = 1;
        if (Left != null) count += Left.Count;
        if (Right != null) count += Right.Count;
        return count;
        }
    }

2) You probably don't want a Print method - that word has a particular meaning in computing. Instead, consider providing a ToString method which returns the whole tree as a string:
C#
public override string ToString()
     {
     StringBuilder sb = new StringBuilder();
     sb.Append(Value.ToString());
     if (Left != null || Right != null)
         {
         sb.Append(":(");
         if (Left != null)
             {
             sb.Append(Left.ToString());
             }
         sb.Append(",");
         if (Right != null)
             {
             sb.Append(Right.ToString());
             }
         sb.Append(")");
         }
     return sb.ToString();
     }
 
Share this answer
 
Comments
BillWoodruff 6-Nov-11 7:10am    
+5 A truly valuable response, thanks !
Sweety Khan 19-Nov-11 13:16pm    
it makes my application hang. i dont understand this public BTree() : this(0) { }
OriginalGriff 20-Nov-11 2:56am    
public BTree() : this(0) { }
Declares a constructor taking no parameters, which consists of a call to the constructor taking a single parameter, in this case an integer, given the value zero.
Sweety Khan 20-Nov-11 12:40pm    
ok but why we add this constructor?
Sweety Khan 20-Nov-11 12:37pm    
one thing i observe in add function that every time we call add function, we r adding values in the root n its leaves, we r not going down. am i right?
when creating a tree, the number of nodes in it changes as you add new elements, right?
so you need to create a new node for every 'add' operation

class BT
{
...
public void Add(BT t,int input)
{
...
else if(t.value>=input)
{
//add:  t.left = new BT();
t.left.value = input;           //<--- here t.left is null in your problem
t.left.left = null;
t.right.right = null;
count++;
}
else if(t.value<input)
{
//similarly here, 
//add: t.right = new BT();
t.right.value = input;
t.left.left = null;
t.right.right = null;
count++;
}
}
public void Print(BT tree)
{
Form1 o = new Form1();
if (tree.right != null)
Print(tree.right);
o.richTextBox1.AppendText(tree.value.ToString());
if (tree.left != null)
Print(tree.left);
}
}
}
 
Share this answer
 
Comments
Sweety Khan 6-Nov-11 5:52am    
Thanx :)
Whenever you try to add a new node, you have to create it - you don't!
What you have to do is: When you add a new Left: you have to check if the left node exists, if it does, then you (presumably) want to add the new node it it, rather than the head. If it doesn't exist, then you can add a new node, and set the value.

It's difficult, because I am not sure what tree you expect to get from your inputs: try drawing yourself a tree of the expected results, and from that you should be able to work out where you need to add new nodes each time you add a value to the root node.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900