Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Binary Tree that Grows as the Program Runs


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Alatar   Members   -  Reputation: 303

Like
0Likes
Like

Posted 02 November 2012 - 01:00 AM

Hi everyone,
It has been years since I have actually tried to write any kind of somewhat complex program, and now that I'm back in school I;m finding those to be kind of frustrating.
I have been assigned to write a guessing and learning program using a custom binary tree. The root of the tree is the very first guess. If the guess is incorrect, the program prompts the user to enter the correct thing and a yes/no question to ques between the two. Then it rotates the nodes so that the question is first, the yes branch has the correct answer, and the no branch originally is the wrong guess, but after the next cycle becomes the new quesing question. Hope that made sense. After it has this new information, it asks if the user wants to start over. If they do, it begins from the top, but this time the root is the first distinuserInterfacesing question.
The way we have to set it up, we have a DTNode class, that then has two subclasses, TNode and QNode.
Example run (with original root of TNode("Tiger"):
Game asks: Is it a tiger?
User enters: no.
Game asks: What were you thinking of?
User enters: lion
Game asks: Please enter a yes/no question to ques between a tiger and a lion
User enters: does it have a mane?
Game asks: Would you like to play again?
User says: yes
Game asks: Does it have a mane?
User says: yes
Game asks: is it a lion?
and so on. Hopefully that is clear enough.
I have the Node classes all written and working, but my problem is in the implementation of the tree.

I was trying to write a runner program to run a sample of the game and this is what I came up with so far. I am totally lost on where to go from here.
[source lang="java"]import java.util.*;public class Tree{public static void main(String[] args){USERINTERFACE userInterface = new USERINTERFACE();TNode root = new TNode("Tiger");do{traverse(root);}while (userInterface.confirmation("Would you like to play again?"));userInterface.outputInfo("Thank you for playing and helping me learn!");}public static void traverse(DTNode current){if (userInterface.confirmation("Is it a " + current.getString() + "?")){userInterface.outputInfo("I guessed it!");}else{String thing = userInterface.promptUser("I give up!\nWhat kind of animal were you thinking of?");TNode tNode = new TNode(thing);String ques = userInterface.promptUser("Please enter a yes/no question that ques between a " + current.getString() + " and a " + tNode.getThing());QNode qNode = new QNode(ques, current, tNode);current = qNode;}}}[/source]
I have stared at it for hours and tried many things, and cannot for the life of me figure out where to go from here. I have spoken with my professor, and he has not helped much.
Any advice would be greatly appreciated. I am on a time crunch, and helpful posts will be rated as such.
Thank you mighty programmers of gamedev.net

Edited by Alatar, 05 November 2012 - 12:12 PM.

"All you have to decide is what to do with the time that is given to you." - Gandalf

Sponsor:

#2 Ashaman73   Members   -  Reputation: 4605

Like
0Likes
Like

Posted 02 November 2012 - 01:14 AM

Why should we do your homework ? What would happen if you fail ?

1. Nothing, it isn't important to succeed, I do it just for learning. => well, then try harder, you will learn !
2. OMG, I need it to get accepted. => well, if an other student did made it, it is justified that he got the place instead of you, sorry to be honest.

I have spoken with my professor, and he has not helped much.

This is the best hint, that you should solve it yourself. Why do you do it when you don't want to learn it at all ?

Thank you mighty programmers of gamedev.net

Honey didn't help you, nor your dignity.

helpful posts will be rated as such.

hhmmm... I'm still getting a good rating ?

#3 Alatar   Members   -  Reputation: 303

Like
0Likes
Like

Posted 02 November 2012 - 01:28 AM

Well, I never actually said I wanted you to do it for me now did I? I said any advice on where to go from here would be appreciated. This is a forum to get help and pointers. If you would care to reread my post without getting high and mighty you may see that.

Now as for my comment that you seem to think is sugar...I have not logged in and actively browsed the forums in quite a long time, but I recall a time when phrases like this showed up in quite a few posts. I meant no "sugar" from it. It was no bribe, just a way of showing respect to the people who would courteously help me. Perhaps that has died in the time since I last used this site. Again, not asking for the answer here, just some pointers. Offering to rate an answer is also not sucking up to the people who may try to help. If they do help, they should be appreciated for it. That's the reason there is a reputation scale here. If you would care to hover your cursor over those rating arrows, you would see that it says "this post provides useful information to the conversation and demonstrates knowledge of the subject matter." The other one says "this response is not useful and does not improve the conversation." Guess where yours falls?

Did it ever cross your mind that I am trying to learn here? I am struggling with this assignment, and I turned to what I remember as being a helpful community. There comes a point when you need a pointer in the right direction, and I'm not getting that from my professor. Yes, I get that I am supposed to figure it out, and I am trying. BUT...there is absolutely nothing wrong with asking for advice from more experienced people.

So thank you for your reply, but maybe you should pay more attention to what you read next time before you respond.

Edited by Alatar, 05 November 2012 - 11:32 AM.

"All you have to decide is what to do with the time that is given to you." - Gandalf

#4 Nypyren   Members   -  Reputation: 1867

Like
2Likes
Like

Posted 02 November 2012 - 02:02 AM

You need to turn this:

Parent Node -> Old Thing Node (which isn't what the user is thinking of)

...into this...

New Question Node -> no -> Old Thing Node
New Question Node -> yes -> New Thing Node

Parent Node -> New Question Node


As far as I can tell you're just missing the last step.

Hint: Your line "current = qNode;" will NOT set the parent-to-new-question link.

Edited by Nypyren, 02 November 2012 - 02:05 AM.


#5 Alatar   Members   -  Reputation: 303

Like
0Likes
Like

Posted 02 November 2012 - 07:14 AM

Thank you for your reply.

Parent Node -> Old Thing Node (which isn't what the user is thinking of)
...into this...
New Question Node -> no -> Old Thing Node
New Question Node -> yes -> New Thing Node

I thought that's what I was doing when I said QuestionNode qNode = new QuestionNode(distinguish, current, tNode)

because the QuestionNode's constructor is (String question, DecisionTreeNode no, DecisionTreeNode yes). I take it that's not really what my code is doing?

Edited by Alatar, 05 November 2012 - 11:33 AM.

"All you have to decide is what to do with the time that is given to you." - Gandalf

#6 Nypyren   Members   -  Reputation: 1867

Like
1Likes
Like

Posted 02 November 2012 - 10:17 PM

Yes, your constructor does the first two steps. You still need the third step (making the existing tree point TO your QuestionNode).

Right now, your local variables 'qNode' and 'current' are the only things pointing to your new QuestionNode. When your traverse method returns, both of those variables go out of scope and your new node will no longer be reachable by the program.

You have two options to hook the binary tree up to your new node:

A. Pass in the parent node that you want to rewrite and the link that you followed to get to the current node.
B. Return the newly generated node to the caller and let the caller rewrite the link.

Option B is clearly superior. Consider the possibility that you need to replace the root node itself. There is no parent that you can pass into the traverse method to do this, BUT you could assign a return value to any variable:

root = traverse(gui, root);

Choosing option B, you should be able to rewrite your main and traverse methods so that they properly traverse and rewrite the tree.


Another important thing missing from your current traverse function is that it is missing the case where the current node is a QuestionNode - the function should traverse whichever link (yes/no) the user replied with.

Edited by Nypyren, 02 November 2012 - 10:23 PM.


#7 Alatar   Members   -  Reputation: 303

Like
0Likes
Like

Posted 03 November 2012 - 06:34 PM

Thank you for the help you have given me so far. I really do appreciate it.
I almost have it figured out, I am just running into an issue with the output. It runs through properly when there are two animals in the tree (say, lion and tiger). The problem is that once I try to add a third, it seems to forget about the others, and basically starts over.
Here is what I have now:
[source lang=java]public class Tree{public static void main(String[] args){USERINTERFACE userInterface = new USERINTERFACE();DTNode root = new TNode("Daisy");do{root = traverse(root);}while (userInterface.confirmation("Would you like to play again?"));userInterface.outputInfo("Thank you for playing and helping me learn!");}public static DTNode traverse(DTNode current){if (current instanceof TNode){if (userInterface.confirmation("Is it a " + current.getString() + "?")){userInterface.outputInfo("I guessed it!");return current;}else{String thing = userInterface.promptUser("I give up!\nWhat kind of flower were you thinking of?");TNode tNode = new TNode(thing);String ques = userInterface.promptUser("Please enter a yes/no question that queses between a " + current.getString() + " and a " + tNode.getThing());QNode qNode = null;if (userInterface.confirmation("What is the answer for a " + tNode.getThing() + "?")){qNode = new QNode(ques, current, tNode);}else{qNode = new QNode(ques, tNode, current);}current = qNode;return traverse(current);}}else{if (userInterface.confirmation(current.getString())){current = current.rLink();}else{current = current.lLink();}return traverse(current);}}}[/source]
Can you help me narrow down where my new bug is?
Again, thank you very much for all the help you have given me so far.

Edited by Alatar, 05 November 2012 - 12:14 PM.

"All you have to decide is what to do with the time that is given to you." - Gandalf

#8 Nypyren   Members   -  Reputation: 1867

Like
1Likes
Like

Posted 03 November 2012 - 07:09 PM

In your code to handle ThingNodes, where you create the new QuestionNode, you don't need to traverse any further from there (because the program has "lost" the round). You want to have the program return all the way back to your play-again loop. So, just return your new qNode - once the function begins returning, each of the recursive steps will also return.

In your code that traverses QuestionNodes, you must actually set the appropriate link to point to the result of the traverse call. For example:

current.setRightLink( traverse(gui, current.getRightLink() );
return current;

Edited by Nypyren, 03 November 2012 - 07:11 PM.


#9 Alatar   Members   -  Reputation: 303

Like
0Likes
Like

Posted 05 November 2012 - 11:11 AM

Thank you very much for your help. I was able to finally get it working with your pushes in the right direction.
"All you have to decide is what to do with the time that is given to you." - Gandalf




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS