Binary Search Tree Delete Method

This is a discussion on Binary Search Tree Delete Method within the C# Programming forums, part of the General Programming Boards category; So I'm making a Binary Search tree in C#, and I'm having some troubles with my delete method. I'm pretty ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Binary Search Tree Delete Method

    So I'm making a Binary Search tree in C#, and I'm having some troubles with my delete method. I'm pretty sure the algorithm works (The concept) but whenever I compile I get 2 errors, and I'm not sure why.

    Note: I'm not use to C#, I've only been using it for about 1 day now.

    Here is the delete method:

    Code:
    private void Delete( t data, Node<t> reference ) {
                int c = data.CompareTo( reference.data );
                if ( c < 0 || c > 0 ) {
                    if ( reference.left.data == data || reference.right.data == data ) {
                        if ( reference.left.left != null )
                            reference.left = reference.left.left;
                        else if ( reference.left.left == null && reference.left.right != null )
                            reference.left = reference.left.right;
                        else
                            reference.left = null;
                    }
                    else
                        Delete( data, reference.left );
                }
            }
    The 2 errors I keep getting are:

    Code:
    Error	1	Operator '==' cannot be applied to operands of type 't' and 't' Line: 83
    Error	2	Operator '==' cannot be applied to operands of type 't' and 't' Line: 83
    The line that it's referring to is:

    Code:
    if ( reference.left.data == data || reference.right.data == data ) {
    If you want to see the rest of the code you can go here: http://rafb.net/p/UZm4F669.html

    Thanks.
    Last edited by pobri19; 09-26-2008 at 03:33 AM. Reason: More information.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Could you not use CompareTo again here, instead of ==?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    "data" is of a generic type which does not support "==" operator in general.

    In you case, you can constrain T to of interface type "IComparable<T>", thus you do any comparison by using data1.CompareTo(data2) and avoid using "==".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Binary Search Tree
    By dookie in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2008, 05:35 PM
  2. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 09:11 AM
  3. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  4. Binary Search Tree, Inserting node
    By cheryl_li in forum C Programming
    Replies: 1
    Last Post: 09-13-2003, 04:53 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21