Java “我应该把它放在哪里?”;公共静态void main(字符串[]参数)";在这个节目里?

Java “我应该把它放在哪里?”;公共静态void main(字符串[]参数)";在这个节目里?,java,main,red-black-tree,Java,Main,Red Black Tree,我有一个程序,应该允许我创建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) 我一直在想我是否可以把“publicstat

我有一个程序,应该允许我创建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)
我一直在想我是否可以把“publicstaticvoidmain(String[]args)”放在里面的某个地方,这样我就可以继续运行它,但到目前为止我一直没有成功

以下是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);
   }




}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装维护;
/**
*
*@作者所有者
*/
公共类RBTree{
公共静态最终短黑色=0;
公共静态最终短红色=1;
短颜色;
RBTree左、右、父;
/*Si*/字符串键;
目标价值;
/*向左旋转此树,更新父级,
*并返回根。
*/
最终RBTree rotateLeft(RBTree根){
RBTree temp=右;
右=左温度;
如果(左侧温度!=null)
temp.left.parent=这个;
temp.parent=父级;
如果(父项==null)
根=温度;
其他的
if(this==parent.left)
parent.left=temp;
其他的
parent.right=temp;
左温度=此;
父母=临时;
返回根;
}
/*向右旋转此树,更新父级,
*并返回根。
*/
最终RBTree旋转右(RBTree根){
RBTree temp=左侧;
左=右温度;
如果(临时正确!=null)
temp.right.parent=这个;
temp.parent=父级;
如果(父项==null)
根=温度;
其他的
if(this==parent.right)
parent.right=temp;
其他的
parent.left=temp;
右侧温度=此;
父母=临时;
返回根;
}
/*找到这棵树的继任者。
*/
最终RBTree successorGet(){
红细胞温度,p;
if(右!=null){
温度=右;
while(temp.left!=null)
温度=左侧温度;
}
否则{
温度=这个;
p=父母;
while(p!=null&&temp==p.right){
温度=p;
p=p.parent;
}
}
返回温度;
}
公共字符串toString(){
StringBuffer strbuf=新的StringBuffer();
if(左!=null)
strbuf.append(left.toString());
strbuf.append(key+“=”+value+“,”);
if(右!=null)
strbuf.append(right.toString());
返回strbuf.toString();
}
/*按排序顺序打印,显示树结构
*和节点颜色。
*/
void printDebug(int级别,StringBuffer strbuf){
字符串newline=System.getProperty(“line.separator”);
if(左!=null)
左。打印调试(级别+1,strbuf);
对于(int i=0;i

如果有人知道如何修复此错误,请告诉我。

哪个类需要首先运行该方法。

将main方法放入要首先运行代码的类中。主方法就像项目的入口。

将它放在您希望的主入口点所在的任何位置。关于Java教程的第2页解释了应用程序的主要入口点总是
main