main
onebigear 3 years ago
parent a77966fdac
commit c1ec8f4165

@ -1,7 +1,5 @@
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"]
import random
class letterLeaf:
def __init__(self,letter,wordFruit):
@ -10,14 +8,12 @@ class letterLeaf:
self.letter = letter
self.wordFruit = wordFruit
COUNT = [10]
def print2DUtil_flat(root, space,growth_rate):
def print2DUtil_flat(root, space,growth_rate, COUNT):
if (root == None) :
return
space += COUNT[0]
print2DUtil_flat(root.rightAlphabet, space,growth_rate)
print2DUtil_flat(root.rightAlphabet, space,growth_rate, COUNT)
print()
@ -32,11 +28,11 @@ def print2DUtil_flat(root, space,growth_rate):
print(root.wordFruit)
time.sleep(growth_rate)
print2DUtil_flat(root.leftAlphabet, space,growth_rate)
print2DUtil_flat(root.leftAlphabet, space,growth_rate, COUNT)
def print2D(root, growth_rate):
print("here is a tree that's laying on the ground: ")
print2DUtil_flat(root, 0, growth_rate)
print2DUtil_flat(root, 0, growth_rate, COUNT=[10])
def grepFirstLetter(word):
firstLetter = word[0]
@ -66,25 +62,29 @@ def insertLeaf(root,wordFruit):
return y
import random
if __name__ == "__main__":
root = None
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"]
random_fruit = random.choice(fruit_list)
root = insertLeaf(root, random_fruit)
fruit_list.remove(random_fruit)
len_list = (len(fruit_list))
root = None
while len_list > 0:
random_fruit = random.choice(fruit_list)
insertLeaf(root,random_fruit)
root = insertLeaf(root, random_fruit)
fruit_list.remove(random_fruit)
len_list -= 1
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
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)
growth_rate = ask_input()
print2D(root, growth_rate)

@ -1,303 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# a bonary tree"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#***********************************************************************\n",
"#** a bo-binary tree *\n",
"#** for and inspired by Wang Xiaobo (1952-1997), writer and programmer.*\n",
"#** \"我也自做了词组功能是棵B树我觉得自写的软件自用感觉是最好的。\" *\n",
"#** \"I created a word feature with a binary tree strucutre, *\n",
"#** and I feel best about using a program you created yourself.\" *\n",
"#** read more in archival documentation(Chinese): *\n",
"#** https://blog.csdn.net/weixin_33697898/article/details/93356187 *\n",
"#** *\n",
"#***********************************************************************"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 思维的乐趣 \n",
"### on discourse \n",
"### quotes from 《我的精神家园》\n",
"谈到思想的乐趣,我就想到我父亲的遭遇。我父亲是一位哲学教授,在五六十年代从事思维史的研究。在老年时,他告诉我自己一生的学术经历,就如一部恐怖电影。每当他企图立论时,总要在大一统的思想体系里找到自己的位置,就如一只老母鸡要在一个大搬家的宅院里找地方孵蛋一样。结果他虽然热爱科学而且很努力,在一生中却没有得到思维的乐趣,之收获了无数的恐慌。他一生的探索,只剩下了一些断壁残垣,收到一本名为《逻辑探索》的书里,在他身后出版。\n",
"\n",
"我在沉默中过了很多年:插队、当工人、当大学生,后来又在大学里任过教。当教师的人保持沉默似不可能,但我教的是技术性的课程,在讲台上只讲技术性的话,下了课我就走人。找我看,不管干什么都可以保持沉默。当然,我还有一个终身爱好,就是写小说。但是写好了不拿去发表,同样也保持了沉默。至于沉默的理由,很是简单。那就是信不过话语圈。从我短短的人生经历看,它是一座声名狼藉的疯人院。当时我怀疑的不是说过亩产三十万斤粮、炸过精神原子弹的那个话语圈,而是一切话语圈子。\n",
"\n",
"还有一些人,因为种种原因,对于话语的世界有某种厌恶之情。我属于这最后一种。作为最后一种人,也有义务谈自己的所见所闻。\n",
"\n",
"王小波1997).《我的精神家园》.北京:文化艺术出版社.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Create a proof of concept dictionary tree that looks for hong(3) fu(2)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from anytree import Node, RenderTree\n",
"h = Node(\"h\")\n",
"o = Node(\"o\", parent=h)\n",
"n = Node(\"n\", parent=o)\n",
"g = Node(\"g\", parent=n)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Node('/h')\n",
"Node('/h/o/n/g')\n",
"h\n",
"└── o\n",
" └── n\n",
" └── g\n"
]
}
],
"source": [
"#print node\n",
"print(h)\n",
"print(g)\n",
"\n",
"#print tree\n",
"for pre, fill, node in RenderTree(h):\n",
"\tprint(\"%s%s\" %(pre,node.name))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"f\n",
"└── u\n"
]
}
],
"source": [
"f = Node(\"f\")\n",
"u = Node(\"u\", parent = f)\n",
"for pre, fill, node in RenderTree(f):\n",
"\tprint(\"%s%s\" %(pre,node.name))\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y\n",
"└── e\n"
]
}
],
"source": [
"y = Node(\"y\")\n",
"e = Node(\"e\", parent = y)\n",
"for pre, fill, node in RenderTree(y):\n",
"\tprint(\"%s%s\" %(pre,node.name))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b\n",
"└── e\n",
" └── n\n"
]
}
],
"source": [
"b = Node(\"b\")\n",
"e = Node(\"e\", parent = b)\n",
"n = Node(\"n\", parent = e)\n",
"for pre, fill, node in RenderTree(b):\n",
"\tprint(\"%s%s\" %(pre,node.name))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def printTree(root):\n",
" for pre, fill, node in RenderTree(root):\n",
" print(\"%s%s\" %(pre,node.name))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"h\n",
"└── o\n",
" └── n\n",
" └── g\n",
"红\n",
"\n",
"f\n",
"└── u\n",
"拂\n",
"\n",
"y\n",
"└── e\n",
"夜\n",
"\n",
"b\n",
"└── e\n",
" └── n\n",
"奔\n",
"\n"
]
}
],
"source": [
"tree_list = [h,f,y,b]\n",
"红拂夜奔 = [\"红\",\"拂\",\"夜\",\"奔\"]\n",
"counter = 0\n",
"for tree in tree_list:\n",
" printTree(tree)\n",
" print(红拂夜奔[counter])\n",
" counter = counter + 1\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# an alphabet binary search tree\n",
"import string\n",
"alphabet_s = string.ascii_lowercase\n",
"alphabet_l = list(alphabet_s)\n",
"#print(alphabet_l)\n",
"# add nodes manually, starting from m "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<video src=\"vid/wang_interview.mp4\" controls >\n",
" Your browser does not support the <code>video</code> element.\n",
" </video>"
],
"text/plain": [
"<IPython.core.display.Video object>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import IPython.display \n",
"Video('vid/wang_interview.mp4')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# transcription of wang's interview in Chinese\n",
"# autotranslate that into English for non-Chinese readers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"数据结构是思维的形状的一种表现方式\n",
"the structure of data may be one representation of the form of thought "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wang Xiaobo (1952-1997), writer and programmer. <br>\n",
"\"我也自做了词组功能是棵B树我觉得自写的软件自用感觉是最好的。\" <br>\n",
"\"I created a word feature with a binary tree strucutre, and I feel best about using a program you created yourself.\"\n",
"read more in archival documentation(Chinese):<br>\n",
"https://blog.csdn.net/weixin_33697898/article/details/93356187"
]
}
],
"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.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

@ -1,79 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "666a35e1-435d-4555-8193-9258ff568e21",
"metadata": {},
"outputs": [],
"source": [
"class letterLeaf:\n",
" def __init__(self,question):\n",
" self.yes = None\n",
" self.no = None\n",
" self.question = question\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "254b714a-2504-4cc0-9a78-21d406ddbb2f",
"metadata": {},
"outputs": [],
"source": [
"# tree traversals, here to print stuff \n",
"def inorder(treeName):\n",
" if treeName:\n",
" inorder(treeName.yes)\n",
" print(treeName.question)\n",
" inorder(treeName.no)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8512457-7944-4604-92c7-29f90ab6c3db",
"metadata": {},
"outputs": [],
"source": [
"# interactive user input utility \n",
"def saysYes(ques):\n",
" while True:\n",
" ans = input(ques)\n",
" ans = ans[0:1].lower()\n",
" if ans == 'y': return True\n",
" else : return False"
]
},
{
"cell_type": "markdown",
"id": "8415d1d2-211a-4e82-b331-85527e8c1510",
"metadata": {},
"source": [
"reference\n",
"https://web.stonehill.edu/compsci/CS211/Assignmenta%202015/TreeAssignment.pdf"
]
}
],
"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.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -1,750 +0,0 @@
{
"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
}

@ -1,405 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4708c5dc-a1e8-42e2-8702-ecea9513d54b",
"metadata": {},
"source": [
"# a binary tree of leaves of letters"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "906fc548-a2a5-411b-ad60-9e93e88bf7f5",
"metadata": {},
"outputs": [],
"source": [
"# http://syllabus.cs.manchester.ac.uk/ugt/2021/COMP26912/lab/ex5.html\n",
"# https://web.stanford.edu/class/archive/cs/cs106x/cs106x.1174/assn/twentyOneQuestions.html\n",
"# https://www.openbookproject.net/py4fun/animal/animal.html"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "578e93f8-e04f-4b01-a482-ebc78cf2c103",
"metadata": {},
"outputs": [],
"source": [
"# each leaf stores a letter to be asked\n",
"class letterLeaf:\n",
" def __init__(self,letter):\n",
" self.leftAlphabet = None\n",
" self.rightAlphabet = None\n",
" self.letter = letter\n",
" # try using a list structure to contain the words in this node? \n",
" self.words = []"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fa8e3d45-ea21-47e1-9a5a-256e5e7ebed1",
"metadata": {},
"outputs": [],
"source": [
"# tree traversals, here to print stuff \n",
"def inorder(treeName):\n",
" if treeName:\n",
" inorder(treeName.leftAlphabet)\n",
" print(treeName.letter)\n",
" inorder(treeName.rightAlphabet)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0c75f820-46c4-46af-b5d9-b837cb055b55",
"metadata": {},
"outputs": [],
"source": [
"# printing tree utility \n",
"# this segment is modified from Shubham Singh(SHUBHAMSINGH10)'s contribution \n",
"\n",
"# spacer\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",
" # Process right leaf/branch/child first\n",
" print2DUtil_flat(root.rightAlphabet, space)\n",
" print()\n",
" for i in range(COUNT[0], space):\n",
" print(end = \" \")\n",
" print(root.letter)\n",
" # Process left child\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",
" print2DUtil_flat(root, 0)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5f27d6d6-3019-4714-98d0-9650f1e087bb",
"metadata": {},
"outputs": [],
"source": [
"# utility to read the first letter of the input word \n",
"# press enter key, otherwise notebook will be pending\n",
"def grepFirstLetter():\n",
" word = input()\n",
" firstLetter = word[0]\n",
" return firstLetter\n",
" #print(\"the letter starts with : {}, and will be inserted under the {} leaf\".format(firstLetter, firstLetter))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "11081ff6-0295-4329-885c-6e6ddfddfcbf",
"metadata": {},
"outputs": [],
"source": [
"def insertLeaf(root,firstLetter):\n",
" #create new leaf \n",
" letterLeaf(firstLetter)\n",
" # python pointer implementation\n",
" # a root pointer \n",
" x = root\n",
" # pointer y maintains the trailing\n",
" # pointer of x\n",
" y = None\n",
" # traversal \n",
" # while x != none means the pointer can keep traversing\n",
" while(x != None):\n",
" y = x #trailing pointer points to x \n",
" if (firstLetter < x.letter):\n",
" x = x.leftAlphabet\n",
" else:\n",
" x = x.rightAlphabet\n",
" # insert leaf at root when tree is empty \n",
" if (y == None):\n",
" y = letterLeaf \n",
" # do string comparison, insert at left side of the alphabetTree \n",
" elif (firstLetter < x.letter):\n",
" x.leftAlphabet = letterLeaf\n",
" # insert at right side of the alphabetTree\n",
" else:\n",
" x.rightAlphabet = letterLeaf\n",
" return y\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "22301756-70e0-4ff7-b9bb-4f59c458058b",
"metadata": {},
"outputs": [],
"source": [
"# print binary tree in 2D copied from sample program\n",
"COUNT = [10]\n",
"def print2DUtil(root, space) :\n",
"\n",
" # Base case\n",
" if (root == None) :\n",
" return\n",
"\n",
" # Increase distance between levels\n",
" space += COUNT[0]\n",
"\n",
" # Process right child first\n",
" print2DUtil(root.rightAlphabet, space)\n",
"\n",
" # Print current node after space\n",
" # count\n",
" print()\n",
" for i in range(COUNT[0], space):\n",
" print(end = \" \")\n",
" print(root.letter)\n",
"\n",
" # Process left child\n",
" print2DUtil(root.leftAlphabet, space)\n",
"\n",
"# Wrapper over print2DUtil()\n",
"def print2D(root) :\n",
" \n",
" # space=[0]\n",
" # Pass initial space count as 0\n",
" print2DUtil(root, 0)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "883373d0-cccc-4497-b9ff-af45670cb1ce",
"metadata": {},
"outputs": [],
"source": [
"# A utility function to insert a new\n",
"# Node with given key in BST\n",
"def insert(root, letter):\n",
"\n",
" # Create a new Node containing\n",
" # the new element\n",
" newleaf = letterLeaf(letter)\n",
"\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 (letter < x.letter):\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 (letter < y.letter):\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",
"# A utility function to do inorder\n",
"# traversal of BST"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0f0bed93-2858-471b-b827-35f66da519b8",
"metadata": {},
"outputs": [],
"source": [
"def Inorder(root) :\n",
"\n",
" if (root == None) :\n",
" return\n",
" else:\n",
" Inorder(root.leftAlphabet)\n",
" print( root.letter, end = \" \" )\n",
" Inorder(root.rightAlphabet)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e1884b95-8b53-49d6-b56c-a7226f14937e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z']\n",
"\n",
" z\n",
"\n",
" y\n",
"\n",
" x\n",
"\n",
" w\n",
"\n",
"v\n",
"\n",
" u\n",
"\n",
" t\n",
"\n",
" s\n",
"\n",
" r\n",
"\n",
" q\n",
"\n",
" p\n",
"\n",
" o\n",
"\n",
" n\n",
"\n",
" m\n",
"\n",
" l\n",
"\n",
" k\n",
"\n",
" j\n",
"\n",
" i\n",
"\n",
" h\n",
"\n",
" g\n",
"\n",
" f\n",
"\n",
" e\n",
"\n",
" d\n",
"\n",
" c\n",
"\n",
" b\n",
"\n",
" a\n"
]
}
],
"source": [
"#Driver Code\n",
"import random\n",
"#if __name__ == '__main__':\n",
"\n",
"alphabet = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\"]\n",
"root = None\n",
"# pick a random letter in the alphabet\n",
"random_letter = random.choice(alphabet)\n",
"#print(random_letter)\n",
"#insert it into the tree, insert the first one \n",
"root = insert(root, random_letter)\n",
"# remove that letter from list\n",
"alphabet.remove(random_letter)\n",
"print(alphabet)\n",
"len_list = (len(alphabet))\n",
"#print(len_list)\n",
"while len_list > 0:\n",
" random_letter = random.choice(alphabet)\n",
" insert(root,random_letter)\n",
" alphabet.remove(random_letter)\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"
]
},
{
"cell_type": "code",
"execution_count": 329,
"id": "d222d70f-2771-45be-9ac1-7ac579a242cb",
"metadata": {},
"outputs": [],
"source": [
"# print a vertical standing tree \n",
"# https://www.geeksforgeeks.org/print-level-order-traversal-line-line/\n",
"# https://www.geeksforgeeks.org/level-order-tree-traversal/"
]
},
{
"cell_type": "code",
"execution_count": 323,
"id": "39d68b3e-064e-4ed6-b10d-c7250a7034e3",
"metadata": {},
"outputs": [],
"source": [
"# a morse code tree\n",
"# https://www.101computing.net/morse-code-using-a-binary-tree/"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7205015-dd4b-48bf-94c4-be0f96d8988a",
"metadata": {},
"outputs": [],
"source": [
"#firstLetter = grepFirstLetter()\n",
"#print(firstLetter)"
]
}
],
"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
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 418 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 320 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 196 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 194 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 214 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 83 KiB

@ -1,6 +1,5 @@
remove empty projects
more housekeeping work:
clean up programs
clean up a_longan_tree

@ -2,62 +2,54 @@ import sys
import time
import random
# hard coded in
# hard coded, default is 1000
# fix naming of variables for the unix dict program
sys.setrecursionlimit(1000000000)
print(sys.getrecursionlimit())
corpus = []
with open('corpus/words') as f:
with open('words') as f:
corpus = f.readlines()
corpus = [word.strip() for word in corpus]
fruit_list = corpus
#print(fruit_list)
class letterLeaf:
def __init__(self,letter,wordFruit):
class word_leaf:
def __init__(self,letter,word):
self.leftAlphabet = None
self.rightAlphabet = None
self.letter = letter
self.wordFruit = wordFruit
COUNT = [10]
self.word = word
def print2DUtil_flat(root, space,growth_rate):
def print2DUtil_flat(root, space, interval, COUNT):
if (root == None) :
return
space += COUNT[0]
print2DUtil_flat(root.rightAlphabet, space,growth_rate)
print2DUtil_flat(root.rightAlphabet, space, interval, COUNT)
print()
for i in range(COUNT[0], space):
print(end = " ")
print(root.letter)
print(root.word)
for i in range(COUNT[0], space):
print(end = " ")
print(root.wordFruit)
time.sleep(growth_rate)
print2DUtil_flat(root.leftAlphabet, space,growth_rate)
time.sleep(interval)
print2DUtil_flat(root.leftAlphabet, space,interval, COUNT)
def print2D(root, growth_rate):
print("here is a tree that's laying on the ground: ")
print2DUtil_flat(root, 0, growth_rate)
def print2D(root, interval):
print2DUtil_flat(root, 0, interval, COUNT=[10])
def grepFirstLetter(word):
firstLetter = word[0]
return firstLetter
def grep_first_letter(word):
first_letter = word[0]
return first_letter
def insertLeaf(root,wordFruit):
letter = grepFirstLetter(wordFruit)
newleaf = letterLeaf(letter,wordFruit)
def insert_leaf(root,word):
letter = grep_first_letter(word)
new_leaf = word_leaf(letter,word)
x = root
y = None
@ -69,36 +61,33 @@ def insertLeaf(root,wordFruit):
x = x.rightAlphabet
if (y == None):
y = newleaf
y = new_leaf
elif (letter < y.letter):
y.leftAlphabet = newleaf
y.leftAlphabet = new_leaf
else:
y.rightAlphabet = newleaf
y.rightAlphabet = new_leaf
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)
interval = int(input("Enter growth rate value for tree in seconds: "))
return interval
if __name__ == "__main__":
root = None
random_word = random.choice(corpus)
print("Taking some time to process corpus...")
root = insert_leaf(root, random_word)
corpus.remove(random_word)
len_list = (len(corpus))
while len_list > 0:
random_word = random.choice(corpus)
insert_leaf(root,random_word)
corpus.remove(random_word)
len_list -= 1
interval = ask_input()
print2D(root, interval)

Loading…
Cancel
Save