Hey, all. I'm writing a progam that uses Binary search trees to create a game similar to the 20 questions game. Unfortunately, I'm not quite sure how the code works completely. I think it should probably have recursion in it somewhere as we just got done covering that, but I can't seem to find where. Currently, what I have is a binarytreenode class to create the nodes and then a decisiontree class that is supposed to hide the implementation of the binarytreenode class. The problem comes in that I'm not quite sure how to write the code. Any help even with psuedocode would be helpful and appreciated. I'd like to have it so if the questions is true then it moves to the left child but if it is false then it moves to the right child.

Currently have,

class BinaryTreeNode(object):
def __init__(self, data, left = none, right = none):
self.data = data
self.left = left
self.right = right

'''Then from here I'm not sure what else to put into the BinaryTreeNode class, it doens't seem like this is all there should be but I can't figure out what else might go in here. Possibly an insert function, but I believe with Add in the decisiontree class that is taken care of'''

import pickle
import BinaryTreeNode

class DecisionTree(object):
def __init__(self, category):
outfile = open("database.txt", 'w')
root = BinaryTreeNode(data, left, right)
self.root = root
currnode = self.root
self.currnode = currnode
category =

'''I need some type of cursor, like currnode I think to start at root and then be implemented into a main program to move throughout the bst based on the user's answer. I'm not sure what to do with category or if it is even necessary. Or is it just something that is added by the user and doesn't need a category =?. Also, I know that this program needs to use pickle somewhere, but I'm not quite sure where... I think it is in the Add function."

def Add(self, question, newcategory, answer):
newcategory = BinaryTreeNode(data, left, right)
self.currnode.data = question
self.currnode.left = answer
mything = self.currnode
outfile = open("database.txt", 'w')
pickle.dump(mything, outfile)
outfile.close

'''This one I am not sure on, needs to add a question and answer if the user's answer is not in the game so next time they play the game gets better and better. I believe that the loop can be taken care of in the main program to get there, so I think you just create it here, but I don't really know and I'm not sure if this needs to return something. Once again, I don't know what to do with new category. All I could think of was that it creates a new binarytreenode.'''

def Reset(self):
return self.currnode = self.root
''' resets the cursor back to the top of the BST'''

def GetCurrent(self):
return self.current.data


''' I believe all it does is supposed to return the currnodes data it might need to print out the current data so in the main program it asks the question to the user'''

def Movedown(self, direction):
if direction == left:
return self.currnode.left
if direction == right:
return self.currnode.right
else:
raiseexception

'''Needs to move the cursor down through the program. I think it just uses the left and right, I'd like to have it so if it's not left or right then it asks for it again. I think its like raiseexception or something but I'm not sure. And I'm not sure if I need the double =='''

def Atleaf(self, answer):
if answer == self.currnode.data:
return true
if answer == self.currnode.left:
return true
else:
return false

'''Should return a boolean expression for the current leaf.'''


import BinaryTreeNode
import DecisionTree
def main()
''' needs to take a file from a database and use that as a binarytree. I believe the file is already established in the constructor of decisiontree so it doens't need to be established here'''

print "Welcome to the 20 questions game. You pick an object, and I will try to guess what it is. You can help me get better at the game by giving me more information when I make a mistake."

print "The more you play, the better I get."

print "Think of an object and I will try to guess what it is."

''' need some type of loop here and I'm not sure how to start the program.'''
while GetCurrent != none:
print GetCurrent()
questionanswer = raw_input
Atleaf(answer)
Movedown(direction)
answer = raw_input("Help me get better, what is your object?")
Add(question, newcategory, answer)

''' I need it to be an if else look probably recursively, but I can't figure out how to do it. This clearly wont work cause it isn't going to the end of the BST before it says it doesn't know the answer.'''

playagain = raw_input("Do you want to play again?")
if playagain == "yes":
reset()
else:
main.close
''' is there some type of code that closes the program?'''

''' need to get this loop so that when the left or right of the currnode is none that it asks, is your object a(n) ""? '''

main()

Please use code tags around your Python code to preserve the proper indentations. Check the message in the background of the Quick Reply message box for the code tags.

class BinaryTreeNode(object):
    def __init__(self, data, left = none, right = none):
       self.data = data
       self.left = left
       self.right = right


import pickle
import BinaryTreeNode

class DecisionTree(object):
    def __init__(self, category):
        outfile = open("database.txt", 'w')
        root = BinaryTreeNode(data, left, right)
        self.root = root
        currnode = self.root
        self.currnode = currnode
        category =

def Add(self, question, newcategory, answer):
        newcategory = BinaryTreeNode(data, left, right)
        self.currnode.data = question
        self.currnode.left = answer
        mything = self.currnode
        outfile = open("database.txt", 'w')
        pickle.dump(mything, outfile)
        outfile.close

def Reset(self):
        return self.currnode = self.root
    ''' resets the cursor back to the top of the BST'''

def GetCurrent(self):
        return self.current.data

def Movedown(self, direction):
        if direction == left:
            return self.currnode.left
        if direction == right:
            return self.currnode.right
       else:
            raiseexception

def Atleaf(self, answer):
        if answer == self.currnode.data:
           return true
        if answer == self.currnode.left:
           return true  
        else:
           return false

import BinaryTreeNode
import DecisionTree
def main()
    ''' needs to take a file from a database and use that as a binarytree.  I believe the file is already established in the constructor of decisiontree so it doens't need to be established here'''

    print "Welcome to the 20 questions game.  You pick an object, and I will try to guess what it is.  You can help me get better at the game by giving me more information when I make a mistake."

    print "The more you play, the better I get."

    print "Think of an object and I will try to guess what it is."
    
   ''' need some type of loop here and I'm not sure how to start the program.'''
   while GetCurrent != none:
       print GetCurrent()
       questionanswer = raw_input
       Atleaf(answer)
       Movedown(direction)
       answer = raw_input("Help me get better, what is your object?")
       Add(question, newcategory, answer)

    ''' I need it to be an if else look probably recursively, but I can't figure out how to do it.  This clearly wont work cause it isn't going to the end of the BST before it says it doesn't know the answer.'''      

    playagain = raw_input("Do you want to play again?")
    if playagain == "yes":
        reset()
    else:
        main.close
   ''' is there some type of code that closes the program?'''
       
    ''' need to get this loop so that when the left or right of the currnode is none that it asks, is your object a(n) ""? '''
    
main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.