Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using IComparable objects in a BST

Status
Not open for further replies.

mcox05

Programmer
Apr 25, 2006
25
US
I just switched from Java and as most already know Java incorporates a powerfully generic tool called Comparable. It seems as if C# has this same tool but in all the examples I have found... one way or another, I have to hard code a cast type to compare to... is this the only solution?

Main BST code not implemented

Tree - main driver class that controls all dictionary operations of the tree
TreeNode - of type IComparable, has a Node, left, and a Right (pretty standard)
Node - of type IComparable with a private IComparable object as its data


public class Node: IComparable
{
private IComparable obj;
public IComparable Obj
{
get
{ return (obj); }
set
{ obj = value; }
}

public Node()
{
obj = null;
}

public Node(IComparable integer)
{
obj = integer;
}

public int CompareTo(object obj)
{
if (obj is IComparable)
return ( (this.obj).CompareTo( (IComparable)obj ) );
else
throw new Exception("Your object is not of type IComparable!");
}

new public bool Equals(object obj)
{ return( this.obj.Equals(obj) ); }

public override string ToString()
{ return( this.obj.ToString() ); }
}



I always come to a debugging error in VS stating that my IComparable must be of type (whatever I am using at that moment for data) in this particular case an Int32 type.

Can this code be generalized? More specifically, can this code be set up that it never needs to know the cast type?
 
What I did was put the implementation of IComparable on the object held by the Node (a Widget), and not on the Node itself. You're wanting to compare Widgets in order to make your left-child/right-child branching decision, not compare Node objects. So you get code like this: (this is for an AVL tree)
Code:
private IComparer       _comparer;  // Gets set in AVLTree constructor

:
:

public AVLTreeNode Find(object value)
{
  AVLTreeNode child;
  int compareResult;

  child = _root;
  while (true)
  {
    // Sanity check
    if (child == null)
      return null;

    compareResult = _comparer.Compare(child.Value, value);
    if (compareResult == 0)
      return child;

    else if (compareResult > 0)
      if (child.Right == null)
        return null;
      else
        child = child.Right;

    else if (compareResult < 0)
      if (child.Left == null)
        return null;
      else
        child = child.Left;
  }
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
The other thing is my code was written for v1.1 of the framework. Using v2.0 of the framework, with it's support for generics, it would be very different. Sorry, I haven't had the time to port the code over yet. :-(

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top