gpt4 book ai didi

java - 决策树执行

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:45 26 4
gpt4 key购买 nike

我正在使用二元决策树开发一项调查。用户会收到一个问题,他们回答"is"或“否”,答案决定接下来要问什么问题。

我的问题是代码只能在左侧工作,这意味着用户只能回答“否”,代码才能正常工作。如果用户给出的答案是"is",则会抛出 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());
}

}

这是实现 BinaryTree 的 LinkedBinaryTree:

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;
}

}

最佳答案

现在您已经提供了 LinkedBinaryTree 的源代码,问题就清楚了。唯一一次调用 getRight() 是当您对其中一个决策回答 Y 或"is"时,该决策将执行这段代码:

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

由于它返回 null,因此下次尝试取消引用 current 时(这发生在代码 while(current.size() > 1) 中),它会导致 NullPointerException

getRight() 方法需要类似于 getLeft() 实现。

关于java - 决策树执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com