import time import random class letterLeaf: def __init__(self,letter,wordFruit): self.leftAlphabet = None self.rightAlphabet = None self.letter = letter self.wordFruit = wordFruit def print2DUtil_flat(root, space,growth_rate, COUNT): if (root == None) : return space += COUNT[0] print2DUtil_flat(root.rightAlphabet, space,growth_rate, COUNT) print() for i in range(COUNT[0], space): print(end = " ") print(root.letter) for i in range(COUNT[0], space): print(end = " ") print(root.wordFruit) time.sleep(growth_rate) print2DUtil_flat(root.leftAlphabet, space,growth_rate, COUNT) def print2D(root, growth_rate): print("here is a tree that's laying on the ground: ") print2DUtil_flat(root, 0, growth_rate, COUNT=[10]) def grepFirstLetter(word): firstLetter = word[0] return firstLetter def insertLeaf(root,wordFruit): letter = grepFirstLetter(wordFruit) newleaf = letterLeaf(letter,wordFruit) x = root y = None while (x != None): y = x if (letter < x.letter): x = x.leftAlphabet else: x = x.rightAlphabet if (y == None): y = newleaf elif (letter < y.letter): y.leftAlphabet = newleaf else: y.rightAlphabet = newleaf return y if __name__ == "__main__": fruit_list = ["apricot","blood orange","currant","durian","egg fruit","fig","guava", "hawthorne","jujube","kiwi","lychee","mandarin","nectarine","olive","persimmon","quandong","rambutan","star fruit", "tangor","ugli fruit","vanilla","water chestnut","ximenia","yuzu","zhe"] root = None random_fruit = random.choice(fruit_list) root = insertLeaf(root, random_fruit) fruit_list.remove(random_fruit) len_list = (len(fruit_list)) while len_list > 0: random_fruit = random.choice(fruit_list) insertLeaf(root,random_fruit) fruit_list.remove(random_fruit) len_list -= 1 def ask_input(): growth_rate = int(input("Enter growth rate value for tree in seconds: ")) return growth_rate growth_rate = ask_input() print2D(root, growth_rate)