I am writing a Generic Binary Search Tree. I need to compare two generic types. How to do it, assuming the the user has implemented IComparable in T class.
private void Insert(T newData, ref Node<T> currentRoot)
{
if (currentRoot == null)
{
currentRoot = new Node<T>(newData);
return;
}
if (newData <= currentRoot.data) //doesn't work, need equivalent functionality
Insert(newData, ref currentRoot.lChild);
else
Insert(newData, ref currentRoot.rChild);
}
You have to add a generic constraint
where T: IComparable<T>to your method to make theCompareTo()method available to instances of your typeT.Then you can use: