Wenn ich den folgenden Code kompiliere, erhalte ich 5 Fehler, die "cannot find symbol"
in Bezug auf t.left
, t.right
und size(t)
lauten. Ich habe auch einen Fehler in der LeitungFehler bei der Kompilierung: Symbol nicht gefunden
CountBinarySearch cbs = new CountBinarySearch(in);
, weil es ein Argument ist vorbei, wenn der Compiler sagt, dass es nicht sein sollte. Diese Zeile ist Code, den mein Professor geschrieben hat und ich kann mich nicht ändern. Welche Änderungen muss ich korrigieren, um die "cannot find symbol"
Fehler, und wie kann ich die CountBinarySearch(in)
gelten für meine CountBinarySearch()
Methode und nicht die Klasse? TIA.
import java.util.Scanner;
import java.math.BigInteger;
// code for BST class taken from Sedgewick textbook, pp. 398-99
public class CountBinarySearch {
private class BST<Key extends Comparable<Key>, Value> {
private Node root;
private class Node {
private Key key;
private Value val;
private Node left, right;
private int N; // # nodes in subtree rooted here
public Node(Key key, Value val, int N) {
this.key = key;
this.val = val;
this.N = N;
}
}
public int size() {
return size(root);
}
private int size(Node x) {
if (x==null) return 0;
else return x.N;
}
public Value get(Key key) {
return get(root, key);
}
private Value get(Node x, Key key) {
if (x==null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) return get(x.left, key);
else if (cmp > 0) return get(x.right, key);
else return x.val;
}
public void put(Key key, Value val) {
root = put(root, key, val);
}
private Node put(Node x, Key key, Value val) {
if (x==null) return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp<0) x.left = put(x.left, key, val);
else if (cmp > 0) x.right = put(x.right, key, val);
else x.val = val;
x.N = size(x.left) + size(x.right) + 1;
return x;
}
} //end class BST
private long theCount;
public void CountBinarySearch(Scanner in) {
int n = in.nextInt();
BST tree = new BST();
BigInteger sum = BigInteger.valueOf(0);
sum = sum.add(BigInteger.valueOf(countOrderings(tree)));
BigInteger last8 = new BigInteger("Math.pow(10,8)");
sum = sum.mod(last8);
theCount = sum.longValue();
return;
}
public long countOrderings(BST t) {
if (t==null) return 1;
else {
int a = numElements(t.left);
int b = numElements(t.right);
return aChooseb(a+b,b)*countOrderings(t.left)*countOrderings(t.right);
} //end else
} //end countOrderings
public int numElements(BST t){
return size(t);
} //end numElements
public double aChooseb(int a, int b) {
if (b<0 || b>a) return 0;
if (b>a/2) {
b=a-b; //(n choose k) = (n choose (n-k))
} //end if
double denom = 1.0;
double num = 1.0;
for (int i = 0; i <= b; i++) {
denom *= i;
num *= (a+1-i);
} //end for
return num/denom;
} //end aChooseb
public long getCount() { return theCount; }
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int cases = in.nextInt();
for (int c=0; c<cases; ++c) {
CountBinarySearch cbs = new CountBinarySearch(in);
System.out.println(cbs.getCount());
}
}
} //end class CountBinarySearch
Sie sollten den Unterschied zwischen einem Konstruktor und einer Methode nachschlagen, und auch die Bedeutung von 'privaten' Variablen. – jonhopkins