我正在制作一个程序,该程序能够按升序或降序对整数或分数列表进行排序。我使用的是GUI,当我按下按钮时,原始列表应添加到二叉树中。应该执行有序遍历,并且该列表应显示在GUI中。我已经完成了所有工作,但是随后我发现我应该使搜索树类具有通用性。我已将类更改为通用类,但在从GUI调用该类以实际执行遍历时遇到问题。您能提供的任何帮助将不胜感激。
这是带有Node类的原始SearchTree
public class SearchTree {
// creates the root of the tree
Node root;
// String to store the answer while tree is being traversed
private String answer = "";
SearchTree (Object x) {
root = new Node(x);
}
/* recursive method adding integers to the tree,
* placing it depending on if it is smaller or larger
* than the parent node
*/
Node insertInteger(Node node, Integer x) {
if (node == null) {
return node = new Node(x);
}
if (x < (Integer) node.parent) {
node.left = insertInteger(node.left, x);
} else {
node.right = insertInteger(node.right, x);
}
return node;
}
// recursive method for adding fractions to the tree
Node insertFraction(Node node, Fraction x) {
if (node == null) {
return node = new Node(x);
}
if (x.compareTo((Fraction) node.parent) < 1) {
node.left = insertFraction(node.left, x);
}else {
node.right = insertFraction(node.right, x);
}
return node;
}
// in order traversal for the ascending order
String inOrder(Node node) {
if (node != null) {
inOrder(node.left);
answer = answer + node.parent + " ";
inOrder(node.right);
}
return answer;
}
// reversed traversal for the descending order
String descendingOrder(Node node) {
if (node != null) {
descendingOrder(node.right);
answer = answer + node.parent + " ";
descendingOrder(node.left);
}
return answer;
}
}
class Node {
Object parent;
Node left;
Node right;
// Constructors
Node(Object theParent) {
this(theParent, null, null);
}
private Node(Object theParent, Node lChild, Node rChild) {
parent = theParent;
this.left = lChild;
this.right = rChild;
}
}
通用SearchTree
public class SearchTree<T extends Comparable<T>> {
Node root; //root node of the tree
//defines each node of tree
class Node{
T value;
Node right,left;
Node(T value){
this.value = value;
}
}
// inserts new node in tree
public void insert(T value){
if(isEmpty())
root = new Node(value);
else
insert(root, value);
}
// insert nodes in there proper places
private void insert(Node node, T value){
if(value.compareTo(node.value) < 0){ //if new value less than parent node
if(node.left == null) //if left null then add
node.left = new Node(value);
else
insert(node.left,value); //if left not null then recursive call
}
else if(value.compareTo(node.value) >= 0){ //if new value same or greater than parent node
if(node.right == null) //if right null then add
node.right = new Node(value);
else
insert(node.right,value); //if right not null then recursive call
}
}
// returns root value
public T peek(){
return root.value;
}
// makes sure tree is not empty
public boolean isEmpty(){
return (root == null)? true : false;
}
}
这是问题
private <T extends Comparable<T>> void intOperation() throws NumberFormatException {
try {
String[] stringArray = txtOrig.getText().split(" ");
int [] intArray = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
intArray[i] = Integer.parseInt(stringArray[i]);
}
SearchTree<T> st = new SearchTree<T>();
for (int i = 1; i < intArray.length; i++) {
T val = intArray[i];
st.insert(st.root, val);
}
if (rdbtnAscend.isSelected()) {
txtSort.setText(st.inOrder(st.root));
}else {
txtSort.setText(st.descendingOrder(st.root));
}
/***** This is the original along with the first bracketed try block
SearchTree st = new SearchTree(intArray[0]);
for (int i = 1; i < intArray.length; i++) {
st.insertInteger(st.root, intArray[i]);
}
if (rdbtnAscend.isSelected()) {
txtSort.setText(st.inOrder(st.root));
}else {
txtSort.setText(st.descendingOrder(st.root));
}
*/
这是我被卡住的原因。如何从该try块调用通用SearchTree类?
答案 0 :(得分:0)
您正在使用占位符类型T
正确声明您的类。实例化类时,您使用的是占位符,而不是您希望通用类为的类型。
SearchTree<Integer> st = new SearchTree<Integer>();
通用类将需要使用具体类型实例化,以使所有类型检查正常工作。