From f53cbfe93d660b559b62bb136378499db7946b26 Mon Sep 17 00:00:00 2001 From: Gijs Date: Wed, 27 Oct 2021 16:54:22 +0200 Subject: [PATCH] Added example of generated graph, added requirements to readme. --- README.md | 25 ++- tree-sort.svg | 355 +++++++++++++++++++++++++++++++++ workshop/code/text-to-sort.txt | 2 +- workshop/code/text_tree.py | 4 +- 4 files changed, 379 insertions(+), 7 deletions(-) create mode 100644 tree-sort.svg diff --git a/README.md b/README.md index c002209..7fb653b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Tree sort -Repository for the tree sort algorithm workshop. +![A visualization of the binary search tree, the tree is constructed with the definition of the tree sort as found on Wikipedia](tree-sort.svg) + +Repository for the workshop on the [tree sort algorithm](https://en.wikipedia.org/wiki/Tree_sort). You'll find the workshop material in the `workshop` folder. @@ -54,7 +56,7 @@ In the folder `workshop/code` a simple implementation of the tree sort algorithm Implementation of the binary search tree. When the script is ``` -python workshop/treesort.py +python workshop/code/treesort.py ``` ### workshop/code/tree-visualizer.py @@ -62,7 +64,7 @@ python workshop/treesort.py Visualizes a tree using graphviz ``` -python/tree_visualizer.py +python workshop/code/tree_visualizer.py ``` ### workshop/code/text-tree.py @@ -70,5 +72,18 @@ python/tree_visualizer.py Reads a text (`workshop/text-to-sort.txt`) and generates a visual tree: ``` -python/text-tree.py -``` \ No newline at end of file +python workshop/code/text_tree.py +``` + +## Requirements + +The implementations are written in python and require python 3, find information on how to [install python here](https://www.python.org/downloads/) + +The visualizer and the text_tree both use graphviz to generate the visualization. Find information on how to [install graphviz here](https://graphviz.org/download/) + +The visualizer and the text_tree also require graphviz the [graphviz python bindings](https://pypi.org/project/graphviz). The easiest way to install them is through pip: + +``` +pip3 install graphviz +``` + diff --git a/tree-sort.svg b/tree-sort.svg new file mode 100644 index 0000000..b6070a6 --- /dev/null +++ b/tree-sort.svg @@ -0,0 +1,355 @@ + + + + + + +text-tree + + + +AAA + +a + + + + + + + +AAD + +tree + + + +AAA:ne--AAD:s + + + + +AAE + +sort + + + +AAD:nw--AAE:s + + + + + + + + +AAF + +is + + + +AAE:nw--AAF:s + + + + + + +ABX + +that + + + +AAE:ne--ABX:s + + + + +AAG + +algorithm + + + +AAF:nw--AAG:s + + + + + + +ABJ + +search + + + +AAF:ne--ABJ:s + + + + + + + + +AAJ + +builds + + + +AAG:ne--AAJ:s + + + + +AAK + +binary + + + +AAJ:nw--AAK:s + + + + + + +AAV + +from + + + +AAJ:ne--AAV:s + + + + +AAL + +be + + + +AAK:nw--AAL:s + + + + + + + + +AAM + +and + + + +AAL:nw--AAM:s + + + + + + + + + + + + + + +AAW + +elements + + + +AAV:nw--AAW:s + + + + + + +ABE + +in + + + +AAV:ne--ABE:s + + + + +AAX + +come + + + +AAW:nw--AAX:s + + + + + + + + + + + + + + + + + + + + +ABK + +out + + + +ABJ:nw--ABK:s + + + + + + +ABS + +so + + + +ABJ:ne--ABS:s + + + + +ABL + +order + + + +ABK:nw--ABL:s + + + + + + + + + + + + + + + + + + + + +ABY + +sorted + + + +ABX:nw--ABY:s + + + + + + +ACD + +the + + + +ABX:ne--ACD:s + + + + + + + + + + + + + + +ACG + +to + + + +ACD:ne--ACG:s + + + + +ACH + +then + + + +ACG:nw--ACH:s + + + + + + +ACM + +traverses + + + +ACG:ne--ACM:s + + + + + + + + + + + + + + + + diff --git a/workshop/code/text-to-sort.txt b/workshop/code/text-to-sort.txt index 7f07eeb..9495eca 100644 --- a/workshop/code/text-to-sort.txt +++ b/workshop/code/text-to-sort.txt @@ -1 +1 @@ -"the quality of light by which we scrutinize our lives has direct bearing upon the product which we live, and upon the changes which we hope to bring about through those lives. It is within this light that we form those ideas by which we pursue our magic and make it realized. This is poetry as illumination, for it is through poetry that we give name to those ideas which are - until the poem - nameless and formless, about to be birthed, but already felt. That distillation of experience from which true poetry springs births thought as dream births concept, as feeling births idea, as knowledge births (precedes) understanding." \ No newline at end of file +A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree so that the elements come out in sorted order. \ No newline at end of file diff --git a/workshop/code/text_tree.py b/workshop/code/text_tree.py index 5e3f63d..a1f0abf 100644 --- a/workshop/code/text_tree.py +++ b/workshop/code/text_tree.py @@ -43,6 +43,8 @@ if __name__ == '__main__': words = text.split(' ') cleaned_words = list(filter(not_empty, map(clean, words))) + visualize(make_tree(cleaned_words), 'text-tree') + + # Generate a second tree with words in random order shuffle(cleaned_words) - visualize(make_tree(cleaned_words), 'text-tree-random') \ No newline at end of file