T
Tony Johansson
Hello!
I have made a generic class called Tree<T> to implement a BinaryTree below.
First I used type int to add a node to the binary Tree<T> in this way
Tree<int> tree1 = new Tree<int>(10);
and used Insert to add another node to the BinaryTree in this way
tree1.Insert(5);
This worked successfully to use int.
Now to my question. I now try to use Circle instead of an int and this
doesn't work. I have implemented the
IComparable<T> CompareTo in class Circle.
When I try to compile I get this compile error.
"Error 1 The type 'BinaryTree_Test.Circle' must be convertible to
'System.IComparable<BinaryTree_Test.Circle>' in order to use it as parameter
'T' in the generic type or method 'BinaryTree.Tree<T>'
F:\C#\BinaryTree\BinaryTree Test\Program.cs 13 13 BinaryTree Test"
I can't understand what the problem is?
I have all the classes listed below.
class Program
{
static void Main(string[] args)
{
Circle myCircle = new Circle(3);
Tree<Circle> tree1 = new Tree<Circle>(myCircle);
//Tree<int> tree1 = new Tree<int>(10);
// tree1.Insert(5);
}
}
public class Tree<T> where T : IComparable<T>
{
private T data;
private Tree<T> left;
private Tree<T> right;
public Tree(T NodeValue)
{
data = NodeValue;
left = null;
right = null;
}
public T NodeData
{
get { return data; }
set { data = value; }
}
public Tree<T> LeftTree
{
get { return left; }
set { left = value; }
}
public Tree<T> RightTree
{
get { return right; }
set { right = value; }
}
public void Insert(T newItem)
{
T currentNodeValue = NodeData;
if (currentNodeValue.CompareTo(newItem) > 0)
{
if (LeftTree == null)
LeftTree = new Tree<T>(newItem);
else
LeftTree.Insert(newItem);
}
else
{
if (RightTree == null)
RightTree = new Tree<T>(newItem);
else
RightTree.Insert(newItem);
}
}
}
public class Circle
{
private int radius;
public Circle(int radius)
{ this.radius = radius; }
public double Area()
{ return radius * radius * 3.14156; }
public int CompareTo(Circle other)
{
if (Area() == other.Area()) return 0;
if (Area() > other.Area()) return 1;
return -1;
}
}
//Tony
I have made a generic class called Tree<T> to implement a BinaryTree below.
First I used type int to add a node to the binary Tree<T> in this way
Tree<int> tree1 = new Tree<int>(10);
and used Insert to add another node to the BinaryTree in this way
tree1.Insert(5);
This worked successfully to use int.
Now to my question. I now try to use Circle instead of an int and this
doesn't work. I have implemented the
IComparable<T> CompareTo in class Circle.
When I try to compile I get this compile error.
"Error 1 The type 'BinaryTree_Test.Circle' must be convertible to
'System.IComparable<BinaryTree_Test.Circle>' in order to use it as parameter
'T' in the generic type or method 'BinaryTree.Tree<T>'
F:\C#\BinaryTree\BinaryTree Test\Program.cs 13 13 BinaryTree Test"
I can't understand what the problem is?
I have all the classes listed below.
class Program
{
static void Main(string[] args)
{
Circle myCircle = new Circle(3);
Tree<Circle> tree1 = new Tree<Circle>(myCircle);
//Tree<int> tree1 = new Tree<int>(10);
// tree1.Insert(5);
}
}
public class Tree<T> where T : IComparable<T>
{
private T data;
private Tree<T> left;
private Tree<T> right;
public Tree(T NodeValue)
{
data = NodeValue;
left = null;
right = null;
}
public T NodeData
{
get { return data; }
set { data = value; }
}
public Tree<T> LeftTree
{
get { return left; }
set { left = value; }
}
public Tree<T> RightTree
{
get { return right; }
set { right = value; }
}
public void Insert(T newItem)
{
T currentNodeValue = NodeData;
if (currentNodeValue.CompareTo(newItem) > 0)
{
if (LeftTree == null)
LeftTree = new Tree<T>(newItem);
else
LeftTree.Insert(newItem);
}
else
{
if (RightTree == null)
RightTree = new Tree<T>(newItem);
else
RightTree.Insert(newItem);
}
}
}
public class Circle
{
private int radius;
public Circle(int radius)
{ this.radius = radius; }
public double Area()
{ return radius * radius * 3.14156; }
public int CompareTo(Circle other)
{
if (Area() == other.Area()) return 0;
if (Area() > other.Area()) return 1;
return -1;
}
}
//Tony