Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:14:36+00:00 2026-06-13T20:14:36+00:00

I’m having a bit of trouble with a custom tree. I’m writing a program

  • 0

I’m having a bit of trouble with a custom tree. I’m writing a program that “learns” about animals. Lets start with the nodes. I started with a class called BTNode. It holds a string value and a “right” and “left” BTNode. I then extended BTNode to an abstract class called DecisionTreeNode. DecisionTreeNode is extended by ThingNode and QuestionNode. Thing node is intended to only be leaf nodes, with its string value being the name of an animal. QuestionNode will hold questions that differentiate between animals, and should always have two child nodes.

So, the idea is that it will learn by asking user-supplied questions. So, it will start by simply asking the user to think of an animal, then ask, “Is it a ?”, and if I answer no, it will ask for my answer, and a question that differentiates the two.

For example, I can “seed” the tree with a single thingNode(“mouse”), and if I’m thinking of a fish, then I will answer its first question “no”, and supply a question, “Does it live in water?”. Now the next time through, it will start by asking, “Does it live in water?” and a “yes” will result in, “is it a fish”, and a “no” will result in “is it a mouse?”.

So, I will post my “learn” method below. I think I am having a referencing problem. I pass a DecisionTreeNode into the learn method, but the changes I make to it are not cascading back up. Not sure if I’m explaining that right. The method works for the first run through, you will see where I change the root node from a Thing to a Question and set its child nodes to the two animals. This however does not work for “current”(the leaf node passed into the method), the last line of the learn method is the line that is not acting as expected.

Sorry for the wall of text, let me know if you would like more code posted or any other info.
Thanks in advance.

“root” is passed into the play method initially.

public static void play(DecisionTreeNode current) {
    while(!current.isLeaf()) {
        if(queryUser(current.getValue())) {
            current = current.getYesLink();
        }
        else {

            current = current.getNoLink();
        }
    }
    System.out.println("Is it a " + current.getValue() + "?");
    if(!queryUser("Correct?")) {
        learn(current);
    }
    else {
        System.out.println("I win!");
    }
}




public static void learn(DecisionTreeNode current) {
        String currentGuess;
        String correctGuess;
        String newQuestion;
        ThingNode tempNode;

        currentGuess = current.getValue();
        System.out.println("I give up, what animal were you thinking of?");

        correctGuess = stdin.nextLine();
        System.out.println("Please enter a yes or no question that distinguishes a " + correctGuess + " from a " + currentGuess +": ");

        newQuestion = stdin.nextLine();

        tempNode = (ThingNode)current;
        if(current == root) {
            if(queryUser("")) {
                root = new QuestionNode(newQuestion, tempNode, new ThingNode(correctGuess));
            }
            else {
                root = new QuestionNode(newQuestion, new ThingNode(correctGuess), tempNode);
            }
        }
        else {
            current = new QuestionNode(newQuestion, tempNode, new ThingNode(correctGuess));
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:14:37+00:00Added an answer on June 13, 2026 at 8:14 pm

    Your problem is here:

    else {
        current = new QuestionNode(newQuestion, tempNode, new ThingNode(correctGuess));
    }
    

    Current is the parameter to the method. It a reference to a DecisionTreeNode passed by value.
    Setting current here does not change the current which is passed to the method.

    You can either pass in the parent of current to the function and tweak its reference to current or create a replace(Node, Node) method you can call like parent.replace(leaf, createNewLearnedNode(leaf)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.