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.

11 KiB

a letter tree, with fruits hanging, each starts with a different letter

In [129]:
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"]
# additionally: longan yumberry sugarcane 
In [130]:
# build the fruit motley tree with extra utilities than the letter tree
In [131]:
class letterLeaf:
    def __init__(self,letter,wordFruit):
        self.leftAlphabet = None
        self.rightAlphabet = None
        self.letter = letter
        # try using a list structure to contain the words in this node? 
        self.wordFruit = wordFruit
In [132]:
# printing tree utility 
# this segment is modified from Shubham Singh(SHUBHAMSINGH10)'s contribution 

# spacer
COUNT = [10]

# print a flat lying tree
# speculation this is a recursion that prints the right leaf until there is nothing left
def print2DUtil_flat(root, space) :
    # Base case
    if (root == None) :
        return
    # Increase distance between levels
    space += COUNT[0]
    # Process right leaf/branch/child first
    print2DUtil_flat(root.rightAlphabet, space)
    print()
    for i in range(COUNT[0], space):
        print(end = " ")
    print(root.letter)
    
    for i in range(COUNT[0], space):
        print(end = " ")
    #print(root.letter) 
    print(root.wordFruit)
    # Process left child
    print2DUtil_flat(root.leftAlphabet, space)

    # Wrapper over print2DUtil()
def print2D(root) :
    #Pass initial space count as 0
    print("here is a tree that's laying on the ground: ")
    print2DUtil_flat(root, 0)
In [133]:
#the input was for an interactive version like text input used by wang, save for later 
def grepFirstLetter(word):
    #word = input()
    firstLetter = word[0]
    return firstLetter
    #print("the letter starts with : {}, and will be inserted under the {} leaf".format(firstLetter, firstLetter))
In [134]:
# it will be parsed from the fruit basket
# pick a fruit
# hang onto tree
# parse the string letter by using the grepFirstLetter
def insertLeaf(root,wordFruit):
    #create new leaf 
    letter = grepFirstLetter(wordFruit)
    #print("first letter of {} is : {} ".format(wordFruit, letter))
    #creating a new node containing firstLetter and wordFruit
    newleaf = letterLeaf(letter,wordFruit)
    #print("test print attributes {} {}".format(newleaf.letter, newleaf.wordFruit))
    # python pointer implementation
    # a root pointer 
    x = root
    # pointer y maintains the trailing
    # pointer of x
    # Pointer to start traversing from root
    # and traverses downward path to search
    # where the new node to be inserted
    x = root

    # Pointer y maintains the trailing
    # pointer of x
    y = None

    while (x != None):
        y = x
        if (letter < x.letter):
            x = x.leftAlphabet
        else:
            x = x.rightAlphabet
    
    # If the root is None i.e the tree is
    # empty. The new node is the root node
    if (y == None):
        y = newleaf

    # If the new key is less then the leaf node key
    # Assign the new node to be its left child
    elif (letter < y.letter):
        y.leftAlphabet = newleaf

    # else assign the new node its
    # right child
    else:
        y.rightAlphabet = newleaf

    # Returns the pointer where the
    # new node is inserted
    return y


# A utility function to do inorder
# traversal of BST
In [135]:
# same deal, insert everything in the list until it's empty 
import random

root = None
# pick a random letter in the alphabet
random_fruit = random.choice(fruit_list)
#print(random_letter)
#insert it into the tree, insert the first one 
root = insertLeaf(root, random_fruit)
# remove that letter from list
fruit_list.remove(random_fruit)
#print(fruit_list)
len_list = (len(fruit_list))
#print(len_list)
while len_list > 0:
    random_fruit = random.choice(fruit_list)
    insertLeaf(root,random_fruit)
    fruit_list.remove(random_fruit)
    #print("inserting and removing letter {} ".format(random_letter))
    len_list -= 1
# keep inserting until the list is empty 
# print tree 
print2D(root)
# can try multiple times for different tree configurations
here is a tree that's laying on the ground: 

                    z
                    zhe

          y
          yuzu

                    x
                    ximenia

w
water chestnut

                                        v
                                        vanilla

                              u
                              ugli fruit

                                                  t
                                                  tangor

                                                            s
                                                            star fruit

                                        r
                                        rambutan

                    q
                    quandong

                                        p
                                        persimmon

                              o
                              olive

                                        n
                                        nectarine

          m
          mandarin

                                                  l
                                                  lychee

                                                            k
                                                            kiwi

                                        j
                                        jujube

                              h
                              hawthorne

                                        g
                                        guava

                                                                      f
                                                                      fig

                                                            e
                                                            egg fruit

                                                  d
                                                  durian

                    c
                    currant

                                        b
                                        blood orange

                              a
                              apricot
In [136]:
# fruits in structured presetations:
# https://zhuanlan.zhihu.com/p/113457497
# https://www.wordmom.com/fruits/that-start-with-w
In [ ]:
# more fun stuff
# https://en.wikipedia.org/wiki/Flora_Sinensis

During a potluck dinner in Beijing Adel brought an dish made from pomegrante seeds. It was in December, the crowd was not used to the fruit salad dish. Adel was the only Iranian there. A talented cook as Adel was, the dish was barely touched. Adel, I think you would agree with me that international potlucks are as bad as they can be. Let's hang the fruits high up - trees are good to store and access memories. For the pomegrantes seeds that I've missed that evening.