Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5963415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:14:58+00:00 2026-05-22T19:14:58+00:00

I have a generic BST and a dataItem class to act as treeNode’s value.

  • 0

I have a generic BST and a dataItem class to act as treeNode’s value.

public class BinarySearchTree<T> : ICollection<T>  where T : IComparable
{

}

public class EnglishDictionaryWord
    : IComparer<EnglishDictionaryWord>, IComparable<EnglishDictionaryWord>
{
    public EnglishDictionaryWord(string word, WordCompareType type)
    {
        Word = word;
        Length = Word.Length;
        _compareType = type;
    }

    public int Compare(EnglishDictionaryWord x, EnglishDictionaryWord y)
    {
        if (_compareType == WordCompareType.Length)
            return new WordLengthComparer().Compare(x, y);
        else if (_compareType == WordCompareType.Lexical)
            return new WordLexicalComparer().Compare(x, y);
        else
            throw new InvalidOperationException("Unsupported Comparison type");
    }

    public int CompareTo(EnglishDictionaryWord obj)
    {
        return Compare(this, obj);
    }
} 

public class WordLengthComparer : IComparer<EnglishDictionaryWord>
{
    public WordLengthComparer()
    {

    }
    public int Compare(EnglishDictionaryWord x, EnglishDictionaryWord y)
    {
        return x.Length - y.Length;
    }
}

and similar Lexical comparer class.

Now when i try to use:

BinarySearchTree<EnglishDictionaryWord> _tree = 
    new BinarySearchTree<EnglishDictionaryWord>();

I get compile error:

The type ‘DsLib.EnglishDictionaryWord’ cannot be used as type parameter ‘T’ in the generic type or method ‘DsLib.BinarySearchTree’. There is no implicit reference conversion from ‘DsLib.EnglishDictionaryWord’ to ‘System.IComparable’.

If I try to make

public class BinarySearchTree<T> : ICollection<T>  where T : IComparable<T>

then I get this compile error about boxing conversion unavailable.

The type ‘T’ cannot be used as type parameter ‘T’ in the generic type or method ‘DsLib.BinaryTreeNode’. There is no boxing conversion or type parameter conversion from ‘T’ to ‘System.IComparable’.

I have 2 questions:

(1).
I am confused on the generics implementation. Can some one detail how to correct it ? and general pattern to avoid such errors in future.
When to use IComparable<T> and when IComparable.

(2). Is this comparer pattern correct, having comparer inside the dataitem class? Because user will provide new EnglishWord to be inserted into tree. He can potentially be using different comparer for each word. Then it will break the Tree.

EDIT: Added BSTNode class code

public class BinaryTreeNode<T> where T : IComparable
{
    public BinaryTreeNode(T value)
    {
        Value = value;
    }

    public T Value { get; protected internal set; }
    public BinaryTreeNode<T> Right { get; protected internal set; }
    public BinaryTreeNode<T> Left { get; protected internal set; }
    public BinaryTreeNode<T> Parent { get; protected internal set; }

    public int Height { get; protected internal set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:14:59+00:00Added an answer on May 22, 2026 at 7:14 pm

    I tried your code with the following definitions:

      public class BinarySearchTree<T>
          : ICollection<T>
          where T : IComparable<T>
    
      public class EnglishDictionaryWord
          : IComparer<EnglishDictionaryWord>,
            IComparable<EnglishDictionaryWord>
    
      public class WordLengthComparer
          : IComparer<EnglishDictionaryWord>
    

    and it works just fine – it compiles, executes etc… (.NET 4.0, c#):

     BinarySearchTree<EnglishDictionaryWord> _tree = 
         new BinarySearchTree<EnglishDictionaryWord>();
    

    And to answer your other questions:

    1. You should always favor IComparable<T> instead of IComparable. It’s faster and less prone to errors (no casting/boxing/unboxing) etc… As for your question: Why is it required? it’s simple – the IComparable<T> and IComparable are different types (they have similar names, but don’t let that confuse you – the types are different). So, you simply need to put the same type wherever referenced.

    2. The data that’s inserted in the tree should have comparing logic. When you define the tree, you specify exactly what types of items it will work with – so for as long as that object lives, you can’t add some completely different type into it. E.g., if you defined:

      BinarySearchTree<EnglishDictionaryWord> _tree;

    you cannot add to _tree something else, like SpanglishDictionaryWord, so the tree does keep it’s structure correctly, because only items of EnglishDictionaryWord are added, and they have defined structure and comparing logic that’s consistent.

    EDIT I just saw you have an "Has" comparer logic, instead of pure "Is" comparable. That should be fixed (remove the reference to the comparer from within data items) – if not, you’re right – the tree might get broken…

    EDIT2 If you need the data items to have flexible comparing logic (ie. change the way they are compared, which is strange so think about it), then the BST has to have a reference to the instance of the comparer you intend to use with it: either put the items that wrap the comparer and the actual item, or put the Comparer of items as a property of BST and use that on each data item when deciding which branch to go to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic Tree class which will implement ICollection< T > (and hence
I have a generic class called Repository. This class has a function that calls
I have a generic class that has a method 'Call' on it with a
Is it possible to have a generic server-side query like the following? public IQueryable<TContact>
I have a WCF service which exposes a Generic interface (and the service has
I have a generic function where it's easy to get the compiler to infer
I have a generic factory which caches an instance before return it (simplified code):
I have a generic method with this signature: private void MyGenericMethod<T>(T arg) where T
I have a generic function for XMLHttpRequests. as follows: function XMLRequest(address, data){ this.html =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.