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.
Well if you want to call
Compare, yourBinarySearchTreeclass 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:Aclass to implementIComparer<WBPMember>instead of justIComparerBinarySearchTreewhich takes anIComparer<T>and remembers it for later.BinarySearchTreeusing the default comparer forTSomething like this: