我是学习用java编写代码的初学者,正在实现一个红黑树数据结构。我为 Main 类中的节点创建了一个类,并使用了 T extends Comparable T。但是,以下行RedBlackNode<T> nil =new RedBlackNode<T>(mainkey);给出错误,因为它没有识别“T”数据类型的使用。我正在努力学习 Comparable 的用法,但无法解决此问题。任何帮助,将不胜感激public class Main { public void main(String[] args) { System.out.println("Hello World! qNew"); int mainkey=10; System.out.println(mainkey); RedBlackNode<T> nil =new RedBlackNode<T>(mainkey); //RedBlackNode<T> root=nil; //System.out.println(nil.key); } public class RedBlackNode<T extends Comparable <T>> { public static final int BLACK = 0; //Enumerating Colors with numbers for public static final int RED = 1; // Color of node public T key; RedBlackNode<T> parent; //Parent Node RedBlackNode<T> left; //Left Child Node RedBlackNode<T> right; //Right Child Node public int numLeft=0; //No of elements to left of a node public int numRight=0; //No of elements to right of a node public int color; //Color of each node //Default constructor to initialize RedBlackNode() { color=BLACK; numLeft=0; numRight=0; parent=null; left=null; right=null; } //Constructor to initialize key value of the node RedBlackNode(T key) { this(); this.key=key; } }}
添加回答
举报
0/150
提交
取消