## Posted By

vinaykr41 on 11/01/17

# Animal Tree

/ Published in: Python

URL: animaltree

A interactive animal tree

`class Tree:    def __init__(self, cargo, left=None, right=None):        self.cargo = cargo        self.left  = left        self.right = right     def __str__(self):        return str(self.cargo)     def preorder(self):        if self == None: return        print(self.cargo)        if not self.left == None:            self.left.preorder()        if not self.right == None:            self.right.preorder()     def inorder(self):        if self == None: return        if not self.left == None:            self.left.inorder()        print(self.cargo)        if not self.right == None:            self.right.inorder()     def postorder(self):        if self == None: return        if not self.left == None:            self.left.postorder()        if not self.right == None:            self.right.postorder()        print(self.cargo)     def depth(self):        if tree == None: return 0        left_depth = 0        right_depth = 0        if not self.left == None:            left_depth = self.left.depth()        if not self.right == None:            right_depth = self.right.depth()        return max(left_depth,right_depth) + 1     def printTree(self,level=0):        if self == None: return        print("    " * (level) + str(level+1) + ". " + self.cargo.__str__())        if not self.left == None:            self.left.printTree(level+1)        if not self.right == None:            self.right.printTree(level+1) def yes(ques):    ans = input(ques).lower()    return ans[0] == 'y' def animal():    # start with a singleton    root = Tree("pigeon")     # loop until the user quits    while True:        prompt = "Are you thinking of an animal? "         response = yes(prompt)        if not response:            # print("---------PreOrder-------------")            # root.preorder()            # print("---------InOrder-------------")            # root.inorder()            # print("---------PostOrder-------------")            # root.postorder()            print("----------Tree----------")            root.printTree()            break        # walk the tree        tree = root        while tree.right != None:            prompt = tree.cargo + "? "            if yes(prompt):                tree = tree.left            else:                tree = tree.right         # make a guess        guess = tree.cargo        prompt = "Is it a " + guess + "? "        if yes(prompt):            print ("I rule!")            continue         # get new information        prompt  = "What is the animal's name? "        animal  = input(prompt)        prompt  = "What question would distinguish a %s from a %s? "        question = input(prompt % (animal, guess))         # add new information to the tree        tree.cargo = question        prompt = "If the animal were %s the answer would be? "        if yes(prompt % animal):            tree.right = Tree(guess)            tree.left = Tree(animal)        else:            tree.right = Tree(animal)            tree.left = Tree(guess) animal()  '''Sample Input: yndogcan it flynynntigeris it wildyyyneagleis it wildyynynliondoes it have stripsnyyyndragondoes it breathe fireyyynncrowwill it be in a cagenynnncatwill it barknn Sample Output: 1. can it fly    2. is it wild        3. does it breathe fire            4. dragon            4. eagle        3. will it be in a cage            4. pigeon            4. crow    2. is it wild        3. does it have strips            4. tiger            4. lion        3. will it bark            4. dog            4. cat '''`