You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.1 KiB
Python
91 lines
2.1 KiB
Python
import time
|
|
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"]
|
|
|
|
class letterLeaf:
|
|
def __init__(self,letter,wordFruit):
|
|
self.leftAlphabet = None
|
|
self.rightAlphabet = None
|
|
self.letter = letter
|
|
self.wordFruit = wordFruit
|
|
|
|
COUNT = [10]
|
|
|
|
def print2DUtil_flat(root, space,growth_rate):
|
|
if (root == None) :
|
|
return
|
|
|
|
space += COUNT[0]
|
|
print2DUtil_flat(root.rightAlphabet, space,growth_rate)
|
|
|
|
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)
|
|
|
|
def print2D(root, growth_rate):
|
|
print("here is a tree that's laying on the ground: ")
|
|
print2DUtil_flat(root, 0, growth_rate)
|
|
|
|
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
|
|
|
|
import random
|
|
|
|
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)
|