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 7010335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:58:40+00:00 2026-05-27T21:58:40+00:00

Please help me to resolve the following problem: First of all, I have one

  • 0

Please help me to resolve the following problem:
First of all, I have one class

namespace ProbA
{

    public class A : IComparer
    {
        Private int a;
        public int IComparer.Compare(object CurrentNode, object DataNode)
        {
            WBPMember Current = (WBPMember)CurrentNode;
            WBPMember Data = (WBPMember)DataNode;
            return Current.a- Data.a;
        }
    }
}



 namespace BST
{

public class BinarySearchTree<T> : ICollection<T>, IEnumerable<T>
    {
public virtual void Add(T data)
        {
            // create a new Node instance
            BinaryTreeNode<T> n = new BinaryTreeNode<T>(data);
            int result;

            BinaryTreeNode<T> current = root, parent = null;
            while (current != null)
            {
      result = /**** I need to call the Class A compare method.     
     }
}
    }
}

I want to execute ClassA Compare method, when following statement execute in ProbA assembly.

A objA = new A();
BinarySearchTree<A> bst = new BinarySearchTree<A>();
Bst.Add(objA);

Problem is both classes are in different libraries. And BinarySearchTree is generic class. So how can i compare A typed object in Add function of BinarySearchTree class.


I update this post for more clarification.
Let me describe my problem more clearly.
I have some type of businessrules. And as per different business rules , I need to construct different type of BST. Offcouse, BST generates at the same logic but on the different kind of logic of comparision.

Lets say, I have BusineesRules assembly. And it contains 4 kind of rules like Business1, Business2, Business3. Business4.
And I have an assembly for generating BST and other traversal methods. This BST is fully generic and in assembly BST
Now when I need to implement compare method in each class.
Like,.

Public Class Business1: IComparer
{
    // Implemetation of Icompare.
}

Same for all the other classes.

When I try to add instance of Business1 in BST, make comparision based on its own implementation and same for other classes.

Code for BST is given below:

public class BinarySearchTree<T> : ICollection<T>, IEnumerable<T>
    {
        #region "Private Member Variables"
        private BinaryTreeNode<T> root = null;
        private int count = 0;
        private IComparer<T> comparer = Comparer<T>.Default;    // used to compare node values when percolating down the tree
        #endregion

        #region Constructors
        public BinarySearchTree() { }
        public BinarySearchTree(IComparer<T> comparer)
        {
            this.comparer = comparer;
        }
        #endregion

public virtual void Add(T data)
        {
            // create a new Node instance
            BinaryTreeNode<T> n = new BinaryTreeNode<T>(data);
            int result;

            // now, insert n into the tree
            // trace down the tree until we hit a NULL
            BinaryTreeNode<T> current = root, parent = null;
            while (current != null)
            {
                result = comparer.Compare(current.Value, data);                
                if (result == 0)
                    // they are equal - attempting to enter a duplicate - do nothing
                    return;
                else if (result > 0)
                {
                    // current.Value > data, must add n to current's left subtree
                    parent = current;
                    current = current.Left;
                }
                else if (result < 0)
                {
                    // current.Value < data, must add n to current's right subtree
                    parent = current;
                    current = current.Right;
                }
            }

            // We're ready to add the node!
            count++;
            if (parent == null)
                // the tree was empty, make n the root
                root = n;
            else
            {
                result = comparer.Compare(parent.Value, data);
                if (result > 0)
                    // parent.Value > data, therefore n must be added to the left subtree
                    parent.Left = n;
                else
                    // parent.Value < data, therefore n must be added to the right subtree
                    parent.Right = n;
            }
        }
        #endregion

** Add Generic comparer method.
This following class is added into businessrule classes assembly.

class BusinessRules: IComparer
    {

        public int Compare(object CurrentHNode, object DataNode)
        {
            TreeMember Current;
            TreeMember Data;

            if (CurrentHNode is Current)
            {
                Current = (TreeMember)CurrentHNode;
            }
            else
                throw new ArgumentException("Object is not type of WBPMember");

            if (DataNode is Data)
            {
                Data = (TreeMember)DataNode;
            }
            else
                throw new ArgumentException("Object is not type of WBPMember");

            return Current.TreeIndex - Data.TreeIndex;
        }
    }

You can see following line in the code.
“result = comparer.Compare(current.Value, data);”
But it is compare of binarysearch class. So I can call compare method business1 or business2.?
Is this comparor implemetation useful to compare objects in BinarySearchTree.Add() method?
One more important thing is that BST and other businessrule classes are different assemblies.

  • 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-27T21:58:40+00:00Added an answer on May 27, 2026 at 9:58 pm

    Well if you want to call Compare, your BinarySearchTree class will need to have a reference to an instance of the comparer. Assuming you want to make the binary search tree consistent, it seems logical to use the same comparer for all operations acting on a single instance of the search tree. I would suggest that:

    • You change your A class to implement IComparer<WBPMember> instead of just IComparer
    • You create a constructor in BinarySearchTree which takes an IComparer<T> and remembers it for later.
    • You optionally create a parameterless constructor in BinarySearchTree using the default comparer for T

    Something like this:

    public class BinarySearchTree<T> : ICollection<T>, IEnumerable<T>
    {
        private readonly IComparer<T> comparer;
    
        public BinarySearchTree(IComparer<T> comparer)
        {
            // TODO: Work out how to handle comparer == null (could throw an
            // exception, could use the default comparer).
            this.comparer = comparer;
        }
    
        public BinarySearchTree() : this(Comparer<T>.Default)
        {
        }
    
        // Now within Add, you can call comparer.Compare(current.Value, data)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
Help please, After numerous hours searching for the solution to my problem i have
Please help me to improve the following script: http://jsfiddle.net/n9BkM/8/ I need the following fucntionality:
please help me to identify which of these following is more optimized code? for(int
Please help me understand the following code snippet :- def any(l): whether any number
I have a simple assembly program with the following makefile: all : calc calc
I am new to Xcode and objective C programming.Please help me to resolve the
Please help with the problem as I can't guess how to solve it. When
[Update:] i know the problem... but need help to resolve this. The issue is
I need your help to sort out one problem with session timeout in my

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.