Added example of generated graph, added requirements to readme.

main
Gijs 3 years ago
parent b19dd63069
commit f53cbfe93d

@ -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
```
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
```

@ -0,0 +1,355 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.40.1 (20161225.0304)
-->
<!-- Title: text&#45;tree Pages: 1 -->
<svg width="1628pt" height="692pt"
viewBox="0.00 0.00 1627.84 692.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 688)">
<title>text&#45;tree</title>
<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-688 1623.8449,-688 1623.8449,4 -4,4"/>
<!-- AAA -->
<g id="node1" class="node">
<title>AAA</title>
<ellipse fill="none" stroke="#000000" cx="980.8973" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="980.8973" y="-14.3" font-family="Times,serif" font-size="14.00" fill="#000000">a</text>
</g>
<!-- AAB -->
<!-- AAA&#45;&#45;AAB -->
<!-- AAC -->
<!-- AAA&#45;&#45;AAC -->
<!-- AAD -->
<g id="node4" class="node">
<title>AAD</title>
<ellipse fill="none" stroke="#000000" cx="1054.8973" cy="-90" rx="27" ry="18"/>
<text text-anchor="middle" x="1054.8973" y="-86.3" font-family="Times,serif" font-size="14.00" fill="#000000">tree</text>
</g>
<!-- AAA&#45;&#45;AAD -->
<g id="edge3" class="edge">
<title>AAA:ne&#45;&#45;AAD:s</title>
<path fill="none" stroke="#000000" d="M995.8973,-33C995.8973,-33 1054.8973,-72 1054.8973,-72"/>
</g>
<!-- AAE -->
<g id="node5" class="node">
<title>AAE</title>
<ellipse fill="none" stroke="#000000" cx="980.8973" cy="-162" rx="27" ry="18"/>
<text text-anchor="middle" x="980.8973" y="-158.3" font-family="Times,serif" font-size="14.00" fill="#000000">sort</text>
</g>
<!-- AAD&#45;&#45;AAE -->
<g id="edge4" class="edge">
<title>AAD:nw&#45;&#45;AAE:s</title>
<path fill="none" stroke="#000000" d="M1039.8973,-105C1039.8973,-105 980.8973,-144 980.8973,-144"/>
</g>
<!-- ACQ -->
<!-- AAD&#45;&#45;ACQ -->
<!-- ACR -->
<!-- AAD&#45;&#45;ACR -->
<!-- AAF -->
<g id="node6" class="node">
<title>AAF</title>
<ellipse fill="none" stroke="#000000" cx="817.8973" cy="-234" rx="27" ry="18"/>
<text text-anchor="middle" x="817.8973" y="-230.3" font-family="Times,serif" font-size="14.00" fill="#000000">is</text>
</g>
<!-- AAE&#45;&#45;AAF -->
<g id="edge5" class="edge">
<title>AAE:nw&#45;&#45;AAF:s</title>
<path fill="none" stroke="#000000" d="M965.8973,-177C965.8973,-177 817.8973,-216 817.8973,-216"/>
</g>
<!-- ABW -->
<!-- AAE&#45;&#45;ABW -->
<!-- ABX -->
<g id="node50" class="node">
<title>ABX</title>
<ellipse fill="none" stroke="#000000" cx="1144.8973" cy="-234" rx="27" ry="18"/>
<text text-anchor="middle" x="1144.8973" y="-230.3" font-family="Times,serif" font-size="14.00" fill="#000000">that</text>
</g>
<!-- AAE&#45;&#45;ABX -->
<g id="edge49" class="edge">
<title>AAE:ne&#45;&#45;ABX:s</title>
<path fill="none" stroke="#000000" d="M995.8973,-177C995.8973,-177 1144.8973,-216 1144.8973,-216"/>
</g>
<!-- AAG -->
<g id="node7" class="node">
<title>AAG</title>
<ellipse fill="none" stroke="#000000" cx="416.8973" cy="-306" rx="45.4919" ry="18"/>
<text text-anchor="middle" x="416.8973" y="-302.3" font-family="Times,serif" font-size="14.00" fill="#000000">algorithm</text>
</g>
<!-- AAF&#45;&#45;AAG -->
<g id="edge6" class="edge">
<title>AAF:nw&#45;&#45;AAG:s</title>
<path fill="none" stroke="#000000" d="M802.8973,-249C802.8973,-249 416.8973,-288 416.8973,-288"/>
</g>
<!-- ABI -->
<!-- AAF&#45;&#45;ABI -->
<!-- ABJ -->
<g id="node36" class="node">
<title>ABJ</title>
<ellipse fill="none" stroke="#000000" cx="864.8973" cy="-306" rx="33.2948" ry="18"/>
<text text-anchor="middle" x="864.8973" y="-302.3" font-family="Times,serif" font-size="14.00" fill="#000000">search</text>
</g>
<!-- AAF&#45;&#45;ABJ -->
<g id="edge35" class="edge">
<title>AAF:ne&#45;&#45;ABJ:s</title>
<path fill="none" stroke="#000000" d="M832.8973,-249C832.8973,-249 864.8973,-288 864.8973,-288"/>
</g>
<!-- AAH -->
<!-- AAG&#45;&#45;AAH -->
<!-- AAI -->
<!-- AAG&#45;&#45;AAI -->
<!-- AAJ -->
<g id="node10" class="node">
<title>AAJ</title>
<ellipse fill="none" stroke="#000000" cx="463.8973" cy="-378" rx="32.4942" ry="18"/>
<text text-anchor="middle" x="463.8973" y="-374.3" font-family="Times,serif" font-size="14.00" fill="#000000">builds</text>
</g>
<!-- AAG&#45;&#45;AAJ -->
<g id="edge9" class="edge">
<title>AAG:ne&#45;&#45;AAJ:s</title>
<path fill="none" stroke="#000000" d="M433.8973,-323C433.8973,-323 463.8973,-360 463.8973,-360"/>
</g>
<!-- AAK -->
<g id="node11" class="node">
<title>AAK</title>
<ellipse fill="none" stroke="#000000" cx="285.8973" cy="-450" rx="33.2948" ry="18"/>
<text text-anchor="middle" x="285.8973" y="-446.3" font-family="Times,serif" font-size="14.00" fill="#000000">binary</text>
</g>
<!-- AAJ&#45;&#45;AAK -->
<g id="edge10" class="edge">
<title>AAJ:nw&#45;&#45;AAK:s</title>
<path fill="none" stroke="#000000" d="M447.8973,-394C447.8973,-394 285.8973,-432 285.8973,-432"/>
</g>
<!-- AAU -->
<!-- AAJ&#45;&#45;AAU -->
<!-- AAV -->
<g id="node22" class="node">
<title>AAV</title>
<ellipse fill="none" stroke="#000000" cx="509.8973" cy="-450" rx="28.6953" ry="18"/>
<text text-anchor="middle" x="509.8973" y="-446.3" font-family="Times,serif" font-size="14.00" fill="#000000">from</text>
</g>
<!-- AAJ&#45;&#45;AAV -->
<g id="edge21" class="edge">
<title>AAJ:ne&#45;&#45;AAV:s</title>
<path fill="none" stroke="#000000" d="M479.8973,-394C479.8973,-394 509.8973,-432 509.8973,-432"/>
</g>
<!-- AAL -->
<g id="node12" class="node">
<title>AAL</title>
<ellipse fill="none" stroke="#000000" cx="182.8973" cy="-522" rx="27" ry="18"/>
<text text-anchor="middle" x="182.8973" y="-518.3" font-family="Times,serif" font-size="14.00" fill="#000000">be</text>
</g>
<!-- AAK&#45;&#45;AAL -->
<g id="edge11" class="edge">
<title>AAK:nw&#45;&#45;AAL:s</title>
<path fill="none" stroke="#000000" d="M269.8973,-466C269.8973,-466 182.8973,-504 182.8973,-504"/>
</g>
<!-- AAS -->
<!-- AAK&#45;&#45;AAS -->
<!-- AAT -->
<!-- AAK&#45;&#45;AAT -->
<!-- AAM -->
<g id="node13" class="node">
<title>AAM</title>
<ellipse fill="none" stroke="#000000" cx="107.8973" cy="-594" rx="27" ry="18"/>
<text text-anchor="middle" x="107.8973" y="-590.3" font-family="Times,serif" font-size="14.00" fill="#000000">and</text>
</g>
<!-- AAL&#45;&#45;AAM -->
<g id="edge12" class="edge">
<title>AAL:nw&#45;&#45;AAM:s</title>
<path fill="none" stroke="#000000" d="M167.8973,-537C167.8973,-537 107.8973,-576 107.8973,-576"/>
</g>
<!-- AAQ -->
<!-- AAL&#45;&#45;AAQ -->
<!-- AAR -->
<!-- AAL&#45;&#45;AAR -->
<!-- AAN -->
<!-- AAM&#45;&#45;AAN -->
<!-- AAO -->
<!-- AAM&#45;&#45;AAO -->
<!-- AAP -->
<!-- AAM&#45;&#45;AAP -->
<!-- AAW -->
<g id="node23" class="node">
<title>AAW</title>
<ellipse fill="none" stroke="#000000" cx="420.8973" cy="-522" rx="42.4939" ry="18"/>
<text text-anchor="middle" x="420.8973" y="-518.3" font-family="Times,serif" font-size="14.00" fill="#000000">elements</text>
</g>
<!-- AAV&#45;&#45;AAW -->
<g id="edge22" class="edge">
<title>AAV:nw&#45;&#45;AAW:s</title>
<path fill="none" stroke="#000000" d="M494.8973,-465C494.8973,-465 420.8973,-504 420.8973,-504"/>
</g>
<!-- ABD -->
<!-- AAV&#45;&#45;ABD -->
<!-- ABE -->
<g id="node31" class="node">
<title>ABE</title>
<ellipse fill="none" stroke="#000000" cx="583.8973" cy="-522" rx="27" ry="18"/>
<text text-anchor="middle" x="583.8973" y="-518.3" font-family="Times,serif" font-size="14.00" fill="#000000">in</text>
</g>
<!-- AAV&#45;&#45;ABE -->
<g id="edge30" class="edge">
<title>AAV:ne&#45;&#45;ABE:s</title>
<path fill="none" stroke="#000000" d="M524.8973,-465C524.8973,-465 583.8973,-504 583.8973,-504"/>
</g>
<!-- AAX -->
<g id="node24" class="node">
<title>AAX</title>
<ellipse fill="none" stroke="#000000" cx="339.8973" cy="-594" rx="30.5947" ry="18"/>
<text text-anchor="middle" x="339.8973" y="-590.3" font-family="Times,serif" font-size="14.00" fill="#000000">come</text>
</g>
<!-- AAW&#45;&#45;AAX -->
<g id="edge23" class="edge">
<title>AAW:nw&#45;&#45;AAX:s</title>
<path fill="none" stroke="#000000" d="M403.8973,-539C403.8973,-539 339.8973,-576 339.8973,-576"/>
</g>
<!-- ABB -->
<!-- AAW&#45;&#45;ABB -->
<!-- ABC -->
<!-- AAW&#45;&#45;ABC -->
<!-- AAY -->
<!-- AAX&#45;&#45;AAY -->
<!-- AAZ -->
<!-- AAX&#45;&#45;AAZ -->
<!-- ABA -->
<!-- AAX&#45;&#45;ABA -->
<!-- ABF -->
<!-- ABE&#45;&#45;ABF -->
<!-- ABG -->
<!-- ABE&#45;&#45;ABG -->
<!-- ABH -->
<!-- ABE&#45;&#45;ABH -->
<!-- ABK -->
<g id="node37" class="node">
<title>ABK</title>
<ellipse fill="none" stroke="#000000" cx="785.8973" cy="-378" rx="27" ry="18"/>
<text text-anchor="middle" x="785.8973" y="-374.3" font-family="Times,serif" font-size="14.00" fill="#000000">out</text>
</g>
<!-- ABJ&#45;&#45;ABK -->
<g id="edge36" class="edge">
<title>ABJ:nw&#45;&#45;ABK:s</title>
<path fill="none" stroke="#000000" d="M848.8973,-322C848.8973,-322 785.8973,-360 785.8973,-360"/>
</g>
<!-- ABR -->
<!-- ABJ&#45;&#45;ABR -->
<!-- ABS -->
<g id="node45" class="node">
<title>ABS</title>
<ellipse fill="none" stroke="#000000" cx="947.8973" cy="-378" rx="27" ry="18"/>
<text text-anchor="middle" x="947.8973" y="-374.3" font-family="Times,serif" font-size="14.00" fill="#000000">so</text>
</g>
<!-- ABJ&#45;&#45;ABS -->
<g id="edge44" class="edge">
<title>ABJ:ne&#45;&#45;ABS:s</title>
<path fill="none" stroke="#000000" d="M880.8973,-322C880.8973,-322 947.8973,-360 947.8973,-360"/>
</g>
<!-- ABL -->
<g id="node38" class="node">
<title>ABL</title>
<ellipse fill="none" stroke="#000000" cx="706.8973" cy="-450" rx="29.4969" ry="18"/>
<text text-anchor="middle" x="706.8973" y="-446.3" font-family="Times,serif" font-size="14.00" fill="#000000">order</text>
</g>
<!-- ABK&#45;&#45;ABL -->
<g id="edge37" class="edge">
<title>ABK:nw&#45;&#45;ABL:s</title>
<path fill="none" stroke="#000000" d="M770.8973,-393C770.8973,-393 706.8973,-432 706.8973,-432"/>
</g>
<!-- ABP -->
<!-- ABK&#45;&#45;ABP -->
<!-- ABQ -->
<!-- ABK&#45;&#45;ABQ -->
<!-- ABM -->
<!-- ABL&#45;&#45;ABM -->
<!-- ABN -->
<!-- ABL&#45;&#45;ABN -->
<!-- ABO -->
<!-- ABL&#45;&#45;ABO -->
<!-- ABT -->
<!-- ABS&#45;&#45;ABT -->
<!-- ABU -->
<!-- ABS&#45;&#45;ABU -->
<!-- ABV -->
<!-- ABS&#45;&#45;ABV -->
<!-- ABY -->
<g id="node51" class="node">
<title>ABY</title>
<ellipse fill="none" stroke="#000000" cx="1097.8973" cy="-306" rx="32.4942" ry="18"/>
<text text-anchor="middle" x="1097.8973" y="-302.3" font-family="Times,serif" font-size="14.00" fill="#000000">sorted</text>
</g>
<!-- ABX&#45;&#45;ABY -->
<g id="edge50" class="edge">
<title>ABX:nw&#45;&#45;ABY:s</title>
<path fill="none" stroke="#000000" d="M1129.8973,-249C1129.8973,-249 1097.8973,-288 1097.8973,-288"/>
</g>
<!-- ACC -->
<!-- ABX&#45;&#45;ACC -->
<!-- ACD -->
<g id="node56" class="node">
<title>ACD</title>
<ellipse fill="none" stroke="#000000" cx="1293.8973" cy="-306" rx="27" ry="18"/>
<text text-anchor="middle" x="1293.8973" y="-302.3" font-family="Times,serif" font-size="14.00" fill="#000000">the</text>
</g>
<!-- ABX&#45;&#45;ACD -->
<g id="edge55" class="edge">
<title>ABX:ne&#45;&#45;ACD:s</title>
<path fill="none" stroke="#000000" d="M1159.8973,-249C1159.8973,-249 1293.8973,-288 1293.8973,-288"/>
</g>
<!-- ABZ -->
<!-- ABY&#45;&#45;ABZ -->
<!-- ACA -->
<!-- ABY&#45;&#45;ACA -->
<!-- ACB -->
<!-- ABY&#45;&#45;ACB -->
<!-- ACE -->
<!-- ACD&#45;&#45;ACE -->
<!-- ACF -->
<!-- ACD&#45;&#45;ACF -->
<!-- ACG -->
<g id="node59" class="node">
<title>ACG</title>
<ellipse fill="none" stroke="#000000" cx="1396.8973" cy="-378" rx="27" ry="18"/>
<text text-anchor="middle" x="1396.8973" y="-374.3" font-family="Times,serif" font-size="14.00" fill="#000000">to</text>
</g>
<!-- ACD&#45;&#45;ACG -->
<g id="edge58" class="edge">
<title>ACD:ne&#45;&#45;ACG:s</title>
<path fill="none" stroke="#000000" d="M1308.8973,-321C1308.8973,-321 1396.8973,-360 1396.8973,-360"/>
</g>
<!-- ACH -->
<g id="node60" class="node">
<title>ACH</title>
<ellipse fill="none" stroke="#000000" cx="1320.8973" cy="-450" rx="27" ry="18"/>
<text text-anchor="middle" x="1320.8973" y="-446.3" font-family="Times,serif" font-size="14.00" fill="#000000">then</text>
</g>
<!-- ACG&#45;&#45;ACH -->
<g id="edge59" class="edge">
<title>ACG:nw&#45;&#45;ACH:s</title>
<path fill="none" stroke="#000000" d="M1381.8973,-393C1381.8973,-393 1320.8973,-432 1320.8973,-432"/>
</g>
<!-- ACL -->
<!-- ACG&#45;&#45;ACL -->
<!-- ACM -->
<g id="node65" class="node">
<title>ACM</title>
<ellipse fill="none" stroke="#000000" cx="1486.8973" cy="-450" rx="42.4939" ry="18"/>
<text text-anchor="middle" x="1486.8973" y="-446.3" font-family="Times,serif" font-size="14.00" fill="#000000">traverses</text>
</g>
<!-- ACG&#45;&#45;ACM -->
<g id="edge64" class="edge">
<title>ACG:ne&#45;&#45;ACM:s</title>
<path fill="none" stroke="#000000" d="M1411.8973,-393C1411.8973,-393 1486.8973,-432 1486.8973,-432"/>
</g>
<!-- ACI -->
<!-- ACH&#45;&#45;ACI -->
<!-- ACJ -->
<!-- ACH&#45;&#45;ACJ -->
<!-- ACK -->
<!-- ACH&#45;&#45;ACK -->
<!-- ACN -->
<!-- ACM&#45;&#45;ACN -->
<!-- ACO -->
<!-- ACM&#45;&#45;ACO -->
<!-- ACP -->
<!-- ACM&#45;&#45;ACP -->
</g>
</svg>

After

Width:  |  Height:  |  Size: 13 KiB

@ -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."
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.

@ -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')
Loading…
Cancel
Save