Java 决策树执行

Java 决策树执行,java,binary-tree,Java,Binary Tree,我正在使用二叉决策树进行调查。给用户一个他们回答“是”或“否”的问题,答案决定了下一步要问什么问题 我的问题是代码只在左边工作,这意味着用户只有在代码正常工作时才能回答“否”。如果用户给出的答案是肯定的,则会抛出NullPointerException。是我做错了什么吗 import java.util.Scanner; public class MusicGenreExpert { private static LinkedBinaryTree<String&g

我正在使用二叉决策树进行调查。给用户一个他们回答“是”或“否”的问题,答案决定了下一步要问什么问题

我的问题是代码只在左边工作,这意味着用户只有在代码正常工作时才能回答“否”。如果用户给出的答案是肯定的,则会抛出NullPointerException。是我做错了什么吗

    import java.util.Scanner;

    public class MusicGenreExpert {
    private static LinkedBinaryTree<String> tree;
    static Scanner scan = new Scanner(System.in);

    public MusicGenreExpert() throws ElementNotFoundException
    {
        String e1 = "Are you eco-friendly?";
        String e2 = "Do you consider yourself to be outgoing?";
        String e3 = "Do you consider yourself to be an introvert?";
        String e4 = "On an average day, do you think that you approach and solve" +
                     "problems differently than others?";
        String e5 = "Are you often uneasy (nervous)?";
        String e6 = "If the person in front of you dropped money without knowing," +
        "would you pick it up and keep it?";
        String e7 = "Is gentle a good word that describes you?";
        String e8 = "Do you often worry about what other people may think?";
        String e9 = "Do you think highly of yourself, with little to no care of" +
                      "what others may think?";
        String e10 = "Are you happy with the accomplishments you've had in life" +
                      "thus far?";
        String e11 = "Would you consider yourself to be old fashion?";
        String e12 = "Do you put your all in everything that you do?";
        String e13 = "Is your idea of an epic night going to a rave with thousands" +
                     "of people?";
        String e14 = "Do you believe that Bob Marley is one of the greatest artist" + 
                     "of all time?";
        String e15 = "Would you call yourself an extravert?";
        String e16 = "Do you know remember when you fell in love with" +
                     "Hip Hop?";
        String e17 = "You are the music genre classical. You are a creative introvert"+
        "with a good sense of self esteem. You are at ease with yourself as well as"+
        "the world around you.";
        String e18 = "The indie and punk music genre best fits you. You are an intellectual" +
        "and creative introvert. You prefer not to work as hard as others and are less gentle." +
        "Your self esteem is a constant battle.";
        String e19 = "Rock/Heavy Metal best describes you. You are a creative and gentle" +
        "introvert with a constant battle with self esteem. However you are no stranger to" +
        "hard work.";
        String e20 = "You are a high self esteem, honest and convetional" +
        "introvert. The Pop culture is who you are inside. You may lack in creativity," +
        "but you make up for in hard work.";
        String e21 = " You remember the exact day you fell in love with Hip Hop." +
        "\n With your high self esteem and outgoing personality, you are sure to be" +
        "\n the life of the party. However, you could be more eco friendly and less" +
        " aggressive.";
        String e22 = "You are jazz, blues, and soul. You are a lot more extraverted" +
        "than others. Your at ease personality, high self esteem, and creativity" +
        "makes you the person that eveyone wants to know.";
        String e23 = "The electronic/dance genre describes exactly who you are." +
        "Assertive, outgoing, creative, yet aggressive gives everyone that comes" +
        "in contact with you something to love. You aren't as kind as others, but" +
        "hey, they love you anyway.";
        String e24 = "Music is suppose to tell a story, and country music does" +
        "just that. You are hardworking, conventional and outgoing. Oh and contrary" +
        "to what others may believe your emotional stability is just fine.";
        String e25 = "There is no other genre better to describe you than Reggae." +
        "You are a creative outgoing individual, with high self esteem. Where you" +
        "lack in hardwork you make up for in your kind and generous ways.";

        LinkedBinaryTree<String> n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14,
        n15, n16, n17, n18, n19, n20, n21, n22, n23, n24, n25, n26, n27, n28, n29, n30,
        n31, n32, n33, n34, n35, n36, n37, n38, n39, n40, n41, n42, n43, n44, n45, n46,
        n47, n48, n49, n50, n51, n52, n53;

        n50 = new LinkedBinaryTree<String> (e16);
        n51 = new LinkedBinaryTree<String> (e17);
        n33 = new LinkedBinaryTree<String> (e8, n50, n51);

        n52 = new LinkedBinaryTree<String> (e16);
        n53 = new LinkedBinaryTree<String> (e18);
        n49 = new LinkedBinaryTree<String> (e8, n52, n53);

        n32 = new LinkedBinaryTree<String> (e21);
        n16 = new LinkedBinaryTree<String> (e15, n32, n33);

        n34 = new LinkedBinaryTree<String> (e17);
        n35 = new LinkedBinaryTree<String> (e18);
        n18 = new LinkedBinaryTree<String> (e7, n34, n35);

        n36 = new LinkedBinaryTree<String> (e18);
        n37 = new LinkedBinaryTree<String> (e19);
        n19 = new LinkedBinaryTree<String> (e12, n36, n37);

        n38 = new LinkedBinaryTree<String> (e16);
        n39 = new LinkedBinaryTree<String> (e20);
        n23 = new LinkedBinaryTree<String> (e16, n38, n39);

        n40 = new LinkedBinaryTree<String> (e21);
        n41 = new LinkedBinaryTree<String> (e23);
        n25 = new LinkedBinaryTree<String> (e8, n40, n41);

        n42 = new LinkedBinaryTree<String> (e22);
        n43 = new LinkedBinaryTree<String> (e20);
        n26 = new LinkedBinaryTree<String> (e16, n42, n43);

        n44 = new LinkedBinaryTree<String> (e23);
        n45 = new LinkedBinaryTree<String> (e19);
        n27 = new LinkedBinaryTree<String> (e6, n44, n45);

        n46 = new LinkedBinaryTree<String> (e17);
        n47 = new LinkedBinaryTree<String> (e24);
        n28 = new LinkedBinaryTree<String> (e14, n46, n47);

        n48 = new LinkedBinaryTree<String> (e17);
        n30 = new LinkedBinaryTree<String> (e7, n48, n49);

        n17 = new LinkedBinaryTree<String> (e18);
        n8 = new LinkedBinaryTree<String> (e7, n16, n17);

        n9 = new LinkedBinaryTree<String> (e9, n18, n19);

        n20 = new LinkedBinaryTree<String> (e17);
        n21 = new LinkedBinaryTree<String> (e18);
        n10 = new LinkedBinaryTree<String> (e7, n20, n21);

        n22 = new LinkedBinaryTree<String> (e19);
        n11 = new LinkedBinaryTree<String> (e5, n22, n23);

        n24 = new LinkedBinaryTree<String> (e19);
        n12 = new LinkedBinaryTree<String> (e10, n24, n25);

        n13 = new LinkedBinaryTree<String> (e11, n26, n27);

        n29 = new LinkedBinaryTree<String> (e19);
        n14 = new LinkedBinaryTree<String> (e12, n28, n29);

        n31 = new LinkedBinaryTree<String> (e22);
        n15 = new LinkedBinaryTree<String> (e13, n30, n31);

        n4 = new LinkedBinaryTree<String> (e6, n8, n9);
        n5 = new LinkedBinaryTree<String> (e9, n10, n11);

        n6 = new LinkedBinaryTree<String> (e4, n12, n13);
        n7 = new LinkedBinaryTree<String> (e3, n14, n15);

        n2 = new LinkedBinaryTree<String> (e2, n4, n5);
        n3 = new LinkedBinaryTree<String> (e3, n6, n7);

        tree = new LinkedBinaryTree<String> (e1, n2, n3);
    }

    public static void diagnose(){
        BinaryTree<String> current = tree;

        System.out.println("                        What music genre are you?       ");
        while(current.size() > 1)
        {
            System.out.println (current.getRootElement());
            if (scan.nextLine().equalsIgnoreCase("N"))
                current = current.getLeft();
            else
                current =  current.getRight();  
        }

        System.out.println (current.getRootElement());          
    }      
import java.util.Scanner;
公开课音乐专家{
私有静态链接二叉树;
静态扫描仪扫描=新扫描仪(System.in);
public MusicGenreExpert()引发ElementNotFoundException
{
String e1=“您是否环保?”;
字符串E2=“你认为自己是外向的吗?”
字符串E3=“你认为自己是一个内向的人吗?”
String e4=“平均一天,你认为你能接近并解决问题吗?”+
“问题与其他问题不同?”;
String e5=“你经常不安(紧张)吗?”;
String e6=“如果你前面的人在不知情的情况下丢了钱,”+
“你能把它捡起来留着吗?”;
String e7=“温柔是描述你的好词吗?”;
String e8=“你经常担心别人会怎么想吗?”;
String e9=“你是否对自己评价很高,几乎不关心自己”+
“别人会怎么想?”;
String e10=“你对自己在生活中取得的成就感到满意吗”+
“到目前为止?”;
弦乐E11=“你认为自己是过时的吗?”
String e12=“你把你的一切都投入到你所做的每件事中了吗?”;
String e13=“你是否想过一个史诗般的夜晚,与数千人一起狂欢?”+
“人吗?”;
String e14=“你认为鲍勃·马利是最伟大的艺术家之一吗”+
“一直以来?”;
String e15=“你会称自己为外向者吗?”;
String e16=“你知道还记得你什么时候爱上你吗?”+
“嘻哈?”;
String e17=“你是古典音乐流派。你是一个富有创造性的内向者”+
“有很好的自尊感。你对自己也很自在”+
“你周围的世界。”;
String e18=“独立和朋克音乐类型最适合你。你是一个知识分子”+
“和富有创造性的内向者。你不喜欢像其他人那样努力工作,也不那么温柔。”+
“你的自尊是一场持续的战斗。”;
String e19=“摇滚/重金属最能形容你。你是一个有创造力和温柔的人”+
“性格内向,经常与自尊作斗争。然而,你并不陌生”+
“努力工作。”;
String e20=“你是一个高度自尊、诚实守信的人”+
“内向。流行文化就是你的内心。你可能缺乏创造力。”+
“但你通过努力工作来弥补。”;
String e21=“你还记得你爱上嘻哈的那一天。”+
“\n以您的高度自尊和外向性格,您一定会”+
“\n党的生活。但是,你可以更环保,少污染”+
“好斗的。”;
String e22=“你是爵士乐、蓝调和灵魂。你更加外向”+
“比其他人。你轻松的个性、高度的自尊和创造力”+
“让你成为每个人都想认识的人。”;
String e23=“电子/舞蹈类型准确地描述了你是谁。”+
“自信、外向、有创造力、但又有进取心,让每个人都有机会”+
“与你接触是值得爱的。你不像其他人那么善良,但是”+
“嘿,他们还是爱你的。”;
String e24=“音乐是用来讲述故事的,乡村音乐是用来讲述故事的”+
“就是这样。你工作努力,传统,外向。哦,恰恰相反”+
“其他人可能认为你的情绪稳定很好。”;
String e25=“没有比雷鬼音乐更能形容你的了。”+
“你是一个有创造力、外向的人,有很高的自尊心。你在哪里?”+
“你用你的善良和慷慨的方式弥补了你努力的不足。”;
连接的二进制树n2、n3、n4、n5、n6、n7、n8、n9、n10、n11、n12、n13、n14、,
n15、n16、n17、n18、n19、n20、n21、n22、n23、n24、n25、n26、n27、n28、n29、n30、,
n31、n32、n33、n34、n35、n36、n37、n38、n39、n40、n41、n42、n43、n44、n45、n46、,
n47、n48、n49、n50、n51、n52、n53;
n50=新链接的二进制树(e16);
n51=新的链接二叉树(e17);
n33=新的链接二叉树(e8、n50、n51);
n52=新链接的二进制树(e16);
n53=新链接的二进制树(e18);
n49=新的链接二叉树(e8、n52、n53);
n32=新的LinkedBinaryTree(e21);
n16=新链接的二进制树(e15、n32、n33);
n34=新链接的二进制树(e17);
n35=新链接的二进制树(e18);
n18=新链接的二进制树(e7、n34、n35);
n36=新链接的二进制树(e18);
n37=新链接的二进制树(e19);
n19=新链接的二进制树(e12、n36、n37);
n38=新链接的二进制树(e16);
n39=新的链接二叉树(e20);
n23=新连接的二叉树(e16、n38、n39);
n40=新的LinkedBinaryTree(e21);
n41=新的链接二叉树(e23);
n25=新的链接二叉树(e8,n40,n41);
n42=新的链接二叉树(e22);
n43=新的LinkedBinaryTree(e20);
n26=新的链接二叉树(e16、n42、n43);
n44=新的链接二叉树(e23);
n45=新链接的二进制树(e19);
n27=新的链接二叉树(e6、n44、n45);
n46=新链接的二进制树(e17);
n47=新的链接二叉树(e24);
n28=新的链接二叉树(e14,n46,n47);
n48=新的链接二叉树(e17);
n30=新的LinkedBinaryTree(e7,
import java.util.Iterator;

public class LinkedBinaryTree<T> implements BinaryTree<T>
{
   protected BTNode<T> root;

   public LinkedBinaryTree(){
    root = null; 
 }

  public LinkedBinaryTree (T element)
  {
     root = new BTNode<T>(element);
  }

  public LinkedBinaryTree (T element, LinkedBinaryTree<T> left,
          LinkedBinaryTree<T> right)
       {
          root = new BTNode<T>(element);
          root.setLeft(left.root);
          root.setRight(right.root);
       }

   public T getRootElement()
   {
      if (root == null)
      throw new EmptyCollectionException ("Get root operation "
        + "failed. The tree is empty.");

      return root.getElement();
   }

   public LinkedBinaryTree<T> getLeft()
   {
      if (root == null)
      throw new EmptyCollectionException ("Get left operation "
        + "failed. The tree is empty.");

      LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
      result.root = root.getLeft();

       return result;
    }

    public T find (T target)
   {
      BTNode<T> node = null;

      if (root != null)
      node = root.find(target);

      if (node == null)
      throw new ElementNotFoundException("Find operation failed. "
        + "No such element in tree.");

      return node.getElement();
    }

    public int size()
   {
      int result = 0;

      if (root != null)
     result = root.count();

      return result;
   }

   public Iterator<T> iterator() 
   {
    return new ArrayIterator<T>();
    }

  public int height()
   {
     int result = 0;

     if (root == null)
      return result;

     if (root != null)
     result = root.height();  
     return result; 
  }

public BinaryTree<T> getRight() {
// TODO Auto-generated method stub
return null;
}

public boolean contains(T target) {
// TODO Auto-generated method stub
  return false;
}

 public boolean isEmpty() {
// TODO Auto-generated method stub
  return false;
  }

}
public BinaryTree<T> getRight() {
  // TODO Auto-generated method stub
  return null;
}