我是學習用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
提交
取消