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.
structure_trees/a_fruit_tree_with_hanging_p...

751 lines
19 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"id": "9da34557-037a-436f-bbaa-d332057cbe18",
"metadata": {},
"source": [
"# a fruit tree with a variety of fruits hanging, each enclosed in a picture frame. "
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "01acaa97-796a-4e34-9ee3-a46e71493d9d",
"metadata": {},
"outputs": [],
"source": [
"fruit_list = [\"apricot\",\"blood_orange\",\"currant\",\"durian\",\"egg_fruit\",\"fig\",\"guava\",\n",
" \"hawthorne\",\"jujube\",\"kiwi\",\"lychee\",\"mandarin\",\"nectarine\",\"olive\",\"persimmon\",\"quandong\",\"rambutan\",\"star_fruit\",\n",
" \"tangor\",\"ugli_fruit\",\"vanilla\",\"water_chestnut\",\"ximenia\",\"yuzu\",\"zhe\"]\n",
"# additionally: longan yumberry sugarcane "
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "08ab8673-9b5c-4bd0-9fac-72796e831b94",
"metadata": {},
"outputs": [],
"source": [
"# build the fruit motley tree with extra utilities than the letter tree"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "9c7187e4-0c49-4908-a169-775e6e475f94",
"metadata": {},
"outputs": [],
"source": [
"class letterLeaf:\n",
" def __init__(self,wordFruit,path):\n",
" self.leftAlphabet = None\n",
" self.rightAlphabet = None\n",
" self.wordFruit = wordFruit\n",
" # try using a list structure to contain the words in this node? \n",
" self.path = path"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "11dbf280-6c61-4020-bfe7-e85a723697db",
"metadata": {},
"outputs": [],
"source": [
"# printing tree utility \n",
"# the display cannot indent?\n",
"from IPython.display import Image\n",
"COUNT = [10]\n",
"\n",
"# print a flat lying tree\n",
"# speculation this is a recursion that prints the right leaf until there is nothing left\n",
"def print2DUtil_flat(root, space) :\n",
" # Base case\n",
" if (root == None) :\n",
" return\n",
" # Increase distance between levels\n",
" space += COUNT[0]\n",
" print2DUtil_flat(root.rightAlphabet, space)\n",
" print()\n",
" \n",
" for i in range(COUNT[0], space):\n",
" print(end = \" \")\n",
" url_tree = root.path\n",
" i = Image(url= url_tree, width=30, height=30)\n",
" display(i)\n",
"\n",
" for i in range(COUNT[0], space):\n",
" print(end = \" \")\n",
" print(root.wordFruit)\n",
" print2DUtil_flat(root.leftAlphabet, space)\n",
"\n",
" # Wrapper over print2DUtil()\n",
"def print2D(root) :\n",
" #Pass initial space count as 0\n",
" print(\"here is a tree that's laying on the ground: \")\n",
" # import image module\n",
" # get the image\n",
" print2DUtil_flat(root, 0)\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "d0bcd376-491e-48e9-8bd9-e10d91346d7f",
"metadata": {},
"outputs": [],
"source": [
"def urljoin(wordFruit):\n",
" url = \"images/pic_tree_fruit_img/\" + wordFruit + \".png\"\n",
" return url "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7a12a00f-f7b0-4234-a06c-54c6f3d1daf1",
"metadata": {},
"outputs": [],
"source": [
"def insertLeaf(root,wordFruit):\n",
" #create new leaf \n",
" url = urljoin(wordFruit)\n",
" newleaf = letterLeaf(wordFruit,url)\n",
" #print(\"creating leaf with word = {} url = {}\".format(newleaf.wordFruit,newleaf.path))\n",
"\n",
" # python pointer implementation\n",
" # a root pointer \n",
" x = root\n",
" # pointer y maintains the trailing\n",
" # pointer of x\n",
" # Pointer to start traversing from root\n",
" # and traverses downward path to search\n",
" # where the new node to be inserted\n",
" x = root\n",
"\n",
" # Pointer y maintains the trailing\n",
" # pointer of x\n",
" y = None\n",
"\n",
" while (x != None):\n",
" y = x\n",
" if (wordFruit < x.wordFruit):\n",
" x = x.leftAlphabet\n",
" else:\n",
" x = x.rightAlphabet\n",
" \n",
" # If the root is None i.e the tree is\n",
" # empty. The new node is the root node\n",
" if (y == None):\n",
" y = newleaf\n",
"\n",
" # If the new key is less then the leaf node key\n",
" # Assign the new node to be its left child\n",
" elif (wordFruit < y.wordFruit):\n",
" y.leftAlphabet = newleaf\n",
"\n",
" # else assign the new node its\n",
" # right child\n",
" else:\n",
" y.rightAlphabet = newleaf\n",
"\n",
" # Returns the pointer where the\n",
" # new node is inserted\n",
" return y\n",
"\n",
"\n",
"# A utility function to do inorder\n",
"# traversal of BST"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "dc2230e9-0831-4e3c-93b8-c96d20fd0525",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"here is a tree that's laying on the ground: \n",
"\n"
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/zhe.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"zhe\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/yuzu.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" yuzu\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/ximenia.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" ximenia\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/water_chestnut.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" water_chestnut\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/vanilla.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" vanilla\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/ugli_fruit.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" ugli_fruit\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/tangor.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" tangor\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/star_fruit.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" star_fruit\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/rambutan.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" rambutan\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/quandong.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" quandong\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/persimmon.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" persimmon\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/olive.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" olive\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/nectarine.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" nectarine\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/mandarin.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" mandarin\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/lychee.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" lychee\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/kiwi.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" kiwi\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/jujube.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" jujube\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/hawthorne.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" hawthorne\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/guava.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" guava\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/fig.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" fig\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/egg_fruit.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" egg_fruit\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/durian.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" durian\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/currant.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" currant\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/blood_orange.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" blood_orange\n",
"\n",
" "
]
},
{
"data": {
"text/html": [
"<img src=\"images/pic_tree_fruit_img/apricot.png\" width=\"30\" height=\"30\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" apricot\n"
]
}
],
"source": [
"# same deal, insert everything in the list until it's empty \n",
"import random\n",
"\n",
"root = None\n",
"# pick a random letter in the alphabet\n",
"random_fruit = random.choice(fruit_list)\n",
"#print(random_letter)\n",
"#insert it into the tree, insert the first one \n",
"root = insertLeaf(root, random_fruit)\n",
"# remove that letter from list\n",
"fruit_list.remove(random_fruit)\n",
"#print(fruit_list)\n",
"len_list = (len(fruit_list))\n",
"#print(len_list)\n",
"while len_list > 0:\n",
" random_fruit = random.choice(fruit_list)\n",
" insertLeaf(root,random_fruit)\n",
" fruit_list.remove(random_fruit)\n",
" #print(\"inserting and removing letter {} \".format(random_letter))\n",
" len_list -= 1\n",
"# keep inserting until the list is empty \n",
"# print tree \n",
"print2D(root)\n",
"# can try multiple times for different tree configurations\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}