我有一个程序应该允许我创建一个RB树,但是当我运行它时,我收到以下错误:
run:
Error: Main method not found in class mainrbt.MainRBT, please define the main method as:
public static void main(String[] args)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
我一直想弄清楚我是否可以将“public static void main(String [] args)”放在某处,以便我可以继续运行它,但到目前为止我一直没有成功。 / p>
这是MainRBT.java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mainrbt;
import java.util.Random;
import java.util.Stack;
public class MainRBT {
/* *************************************************** *
* PRIVATE FIELDS *
* *************************************************** */
private RBTree tree;
private int size;
/* If during an insert() or delete() it is found that
* the key is present in the tree, keyFound will be true
* and prevValue will contain the previous value
* associated with the key before the update.
*/
private boolean keyFound;
private Object prevValue;
/* *************************************************** *
* PUBLIC INTERFACE *
* *************************************************** */
/*
* Constructs a new empty red-black tree.
*/
public MainRBT() {
tree = null;
size = 0;
}
/**
* Maps the key to the specified value in this red-black tree.
* Neither the key, nor the value can be
* <code>null</code>.
* @return the previous value to which the key was mapped,
* or <code>null</code> if the key did not have a previous
* mapping in the red-black tree.
*/
public synchronized Object put(/*Si*/String key, Object value) {
if (key == null || value == null)
throw new NullPointerException();
RBTree node = new RBTree();
node.key = key;
node.value = value;
keyFound = false;
tree = insert(node, tree);
if (keyFound)
return prevValue;
else {
size++;
return null;
}
}
/**
* Gets the object associated with the specified key in the red-black tree.
* @param args
* @return the value to which the key is mapped in the red-black tree, or
* <code>null</code> if the key is not mapped to any value in
* this red-black tree.
*/
public synchronized Object get(/*Si*/String key) {
RBTree t = tree;
int comp;
while (t != null && (comp = key.compareTo(t.key)) != 0)
if (comp < 0)
t = t.left;
else
t = t.right;
return t != null ? t.value : null;
}
/**
* Returns <code>true</code> if this red-black tree contains no mappings.
*/
public boolean isEmpty() {
return tree == null;
}
/**
* Removes the key (and its corresponding value) from this red-black tree.
* This method does nothing if the key is not in the red-black tree.
* @return the value to which the key had been mapped in this red-black tree,
* or <code>null</code> if the key did not have a mapping.
*/
public synchronized Object remove(/*Si*/String key) {
RBTree node = tree;
while (node != null) {
int comp = key.compareTo(node.key);
if (comp < 0)
node = node.left;
else if (comp > 0)
node = node.right;
else {
prevValue = node.value;
tree = delete(node, tree);
size--;
return prevValue;
}
}
return null;
}
/**
* Clear the red-black tree so that it contains no mappings.
*/
public synchronized void clear() {
tree = null;
size = 0;
}
/**
* Returns the number of keys in this red-black tree.
*/
public int size() {
return size;
}
/**
* Returns a string representation of this red-black tree.
* This routine is inefficient and primarily intended for
* debugging. To access the elements in the red-black tree in sorted order
* use the <code>keys()</code> and <code>elements()</code> methods.
* @see RedBlackTree#keys
* @see RedBlackTree#elements
*/
public synchronized String toString() {
StringBuffer strbuf = new StringBuffer();
strbuf.append("{");
if (tree != null)
strbuf.append(tree.toString());
if (strbuf.length() > 1)
strbuf.setLength(strbuf.length() - 2); // remove last ", "
strbuf.append("}");
return strbuf.toString();
}
/**
* Returns a string displaying the tree structure
* and the priority numbers.
*/
public synchronized String printDebug() {
StringBuffer strbuf = new StringBuffer();
String newline = System.getProperty("line.separator");
strbuf.append("size: " + size + newline);
if (tree != null)
tree.printDebug(0, strbuf);
return strbuf.toString();
}
public String printStat() {
StatStruct stat = new StatStruct();
collectStat(tree, 0, stat);
StringBuffer strbuf = new StringBuffer();
String newline = System.getProperty("line.separator");
strbuf.append("Aver depth: " +
(float) stat.totDepth / this.size + newline);
strbuf.append("Max depth: " + stat.maxDepth + newline);
return strbuf.toString();
}
/* *************************************************** *
* PRIVATE METHODS *
* *************************************************** */
/* Inserts a node into tree and returns the updated red-black tree */
private RBTree insert(RBTree node, RBTree tree) {
RBTree father = null, son = tree;
/* Insert the new node into the tree. */
while (son != null) {
father = son;
int comp = node.key.compareTo(son.key);
if (comp < 0)
son = son.left;
else if (comp > 0)
son = son.right;
else {
keyFound = true;
prevValue = son.value;
son.value = node.value;
return tree;
}
}
node.parent = father;
if (father == null)
tree = node;
else if (node.key.compareTo(father.key) < 0)
father.left = node;
else father.right = node;
/* Inforce the color invariants of the red-black tree. */
node.color = RBTree.RED;
while (node != tree && node.parent.color == RBTree.RED) {
if (node.parent == node.parent.parent.left) {
son = node.parent.parent.right;
if (son != null && son.color == RBTree.RED) {
node.parent.color = RBTree.BLACK;
son.color = RBTree.BLACK;
node = node.parent.parent;
node.color = RBTree.RED;
} else {
if (node == node.parent.right) {
node = node.parent;
tree = node.rotateLeft(tree);
}
node.parent.color = RBTree.BLACK;
node.parent.parent.color = RBTree.RED;
tree = node.parent.parent.rotateRight(tree);
}
} else {
son = node.parent.parent.left;
if (son != null && son.color == RBTree.RED) {
node.parent.color = RBTree.BLACK;
son.color = RBTree.BLACK;
node = node.parent.parent;
node.color = RBTree.RED;
} else {
if (node == node.parent.left) {
node = node.parent;
tree = node.rotateRight(tree);
}
node.parent.color = RBTree.BLACK;
node.parent.parent.color = RBTree.RED;
tree = node.parent.parent.rotateLeft(tree);
}
}
}
tree.color = RBTree.BLACK;
return tree;
}
/* Deletes a node from a red-black tree and
* returns the updated red-black tree.
*/
private RBTree delete(RBTree node, RBTree tree) {
RBTree x, y;
if (node.left == null || node.right == null)
y = node;
else
y = node.successorGet();
if (y.left != null)
x = y.left;
else
x = y.right;
if (x != null)
x.parent = y.parent;
if (y.parent == null)
tree = x;
else if (y == y.parent.left)
y.parent.left = x;
else
y.parent.right = x;
if (y != node) {
node.key = y.key;
node.value = y.value;
}
/* If the node to be removed is BLACK,
* restore the red-black tree invariants.
* The color of a null leaf is BLACK.
*/
if (y.color == RBTree.BLACK) {
RBTree father = y.parent;
while (x != tree && (x == null || x.color == RBTree.BLACK)) {
if (x == father.left) {
RBTree w = father.right;
if (w == null)
x = tree;
else {
if (w.color == RBTree.RED) {
w.color = RBTree.BLACK;
father.color = RBTree.RED;
tree = father.rotateLeft(tree);
continue;
}
if ((w.left == null || w.left.color == RBTree.BLACK) &&
(w.right == null || w.right.color == RBTree.BLACK)) {
w.color = RBTree.RED;
x = father;
father = x.parent;
}
else {
if (w.right == null || w.right.color == RBTree.BLACK) {
if (w.left != null) {
w.left.color = RBTree.BLACK;
w.color = RBTree.RED;
tree = w.rotateRight(tree);
w = father.right;
}
}
w.color = father.color;
father.color = RBTree.BLACK;
if (w.right != null)
w.right.color = RBTree.BLACK;
tree = father.rotateLeft(tree);
x = tree;
}
}
} else {
RBTree w = father.left;
if (w == null)
x = tree;
else {
if (w.color == RBTree.RED) {
w.color = RBTree.BLACK;
father.color = RBTree.RED;
tree = father.rotateRight(tree);
continue;
}
if ((w.right == null || w.right.color == RBTree.BLACK) &&
(w.left == null || w.left.color == RBTree.BLACK)) {
w.color = RBTree.RED;
x = father;
father = x.parent;
}
else {
if (w.left == null || w.left.color == RBTree.BLACK) {
if (w.right != null) {
w.right.color = RBTree.BLACK;
w.color = RBTree.RED;
tree = w.rotateLeft(tree);
w = father.left;
}
}
w.color = father.color;
father.color = RBTree.BLACK;
if (w.left != null)
w.left.color = RBTree.BLACK;
tree = father.rotateRight(tree);
x = tree;
}
}
}
}
if (x != null)
x.color = RBTree.BLACK;
}
return tree;
}
private class StatStruct {
int totDepth = 0;
int maxDepth = 0;
}
private void collectStat(RBTree t, int depth, StatStruct stat) {
if (t == null)
return;
else {
if (depth > stat.maxDepth)
stat.maxDepth = depth;
stat.totDepth += depth;
collectStat(t.left, depth + 1, stat);
collectStat(t.right, depth + 1, stat);
}
}
}
另外,这是我的RBTree.java文件,以防它与此问题相关:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mainrbt;
/**
*
* @author Owner
*/
public class RBTree {
public static final short BLACK = 0;
public static final short RED = 1;
short color;
RBTree left, right, parent;
/*Si*/String key;
Object value;
/* Rotate this tree to the left, update parent,
* and return the root.
*/
final RBTree rotateLeft(RBTree root) {
RBTree temp = right;
right = temp.left;
if (temp.left != null)
temp.left.parent = this;
temp.parent = parent;
if (parent == null)
root = temp;
else
if (this == parent.left)
parent.left = temp;
else
parent.right = temp;
temp.left = this;
parent = temp;
return root;
}
/* Rotate this tree to the right, update parent,
* and return the root.
*/
final RBTree rotateRight(RBTree root) {
RBTree temp = left;
left = temp.right;
if (temp.right != null)
temp.right.parent = this;
temp.parent = parent;
if (parent == null)
root = temp;
else
if (this == parent.right)
parent.right = temp;
else
parent.left = temp;
temp.right = this;
parent = temp;
return root;
}
/* Get the successor of this tree.
*/
final RBTree successorGet() {
RBTree temp, p;
if (right != null) {
temp = right;
while (temp.left != null)
temp = temp.left;
}
else {
temp = this;
p = parent;
while (p != null && temp == p.right) {
temp = p;
p = p.parent;
}
}
return temp;
}
public String toString() {
StringBuffer strbuf = new StringBuffer();
if (left != null)
strbuf.append(left.toString());
strbuf.append(key + "=" + value + ", ");
if (right != null)
strbuf.append(right.toString());
return strbuf.toString();
}
/* Print in sorted order, displaying the tree structure
* and the node colors.
*/
void printDebug(int level, StringBuffer strbuf) {
String newline = System.getProperty("line.separator");
if (left != null)
left.printDebug(level + 1, strbuf);
for (int i = 0; i < level; i++)
strbuf.append(" ");
if (color == RBTree.BLACK)
strbuf.append("BLACK, " + value + ": " + key + newline);
else
strbuf.append("RED, " + value + ": " + key + newline);
if (right != null)
right.printDebug(level + 1, strbuf);
}
}
如果有人知道如何解决此错误,请告知我们。
答案 0 :(得分:2)
首先需要运行的类是你放入该方法的那个。
答案 1 :(得分:0)
将main方法放入要首先运行代码的类中。主要方法就像项目的入口一样。