from cgitb import html
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 = [3]
empty_string = " "

def print2DUtil_flat(root, space, growth_rate, html_file):
    if (root == None) :
        return
    
    space += COUNT[0]
    print2DUtil_flat(root.rightAlphabet, space,growth_rate, html_file)
        

    html_file.write("<span>") 
    #for i in range(COUNT[0], space):
    #    html_file.write(" ")

    html_file.write(root.letter + "<br>")
    html_file.write("</span>")

    html_file.write("\n")


    html_file.write(root.wordFruit + "<br>")
    html_file.write("\n")

    print2DUtil_flat(root.leftAlphabet, space, growth_rate, html_file)

def print2D(root, growth_rate, html_file):
    print("here is a tree that's laying on the ground: ")
    print2DUtil_flat(root, 0, growth_rate, html_file)

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()

html_file = open("a_letter_tree_web.html","w")

html_file.write("<html>\n<head>\n")
html_file.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n")
html_file.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n")
html_file.write("</head>\n")
html_file.write("<body>\n")
html_file.write("<title> A Letter Tree </title>\n")
html_file.write("<h1> A Letter Tree </h1>")

print2D(root, growth_rate, html_file)

html_file.write("\n</body>\n</html>")
html_file.close()



