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.
19 KiB
19 KiB
a fruit tree with a variety of fruits hanging, each enclosed in a picture frame.¶
In [20]:
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 [21]:
# build the fruit motley tree with extra utilities than the letter tree
In [22]:
class letterLeaf: def __init__(self,wordFruit,path): self.leftAlphabet = None self.rightAlphabet = None self.wordFruit = wordFruit # try using a list structure to contain the words in this node? self.path = path
In [23]:
# printing tree utility # the display cannot indent? from IPython.display import Image 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] print2DUtil_flat(root.rightAlphabet, space) print() for i in range(COUNT[0], space): print(end = " ") url_tree = root.path i = Image(url= url_tree, width=30, height=30) display(i) for i in range(COUNT[0], space): print(end = " ") print(root.wordFruit) 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: ") # import image module # get the image print2DUtil_flat(root, 0)
In [24]:
def urljoin(wordFruit): url = "images/pic_tree_fruit_img/" + wordFruit + ".png" return url
In [25]:
def insertLeaf(root,wordFruit): #create new leaf url = urljoin(wordFruit) newleaf = letterLeaf(wordFruit,url) #print("creating leaf with word = {} url = {}".format(newleaf.wordFruit,newleaf.path)) # 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 (wordFruit < x.wordFruit): 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 (wordFruit < y.wordFruit): 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 [26]:
# 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:
zhe
yuzu
ximenia
water_chestnut
vanilla
ugli_fruit
tangor
star_fruit
rambutan
quandong
persimmon
olive
nectarine
mandarin
lychee
kiwi
jujube
hawthorne
guava
fig
egg_fruit
durian
currant
blood_orange
apricot