At My Fingertips
Rapid Playground
A tree map visualizes a tree as nested rectangles.
A tree is a recursive data structure. In a language like Haskell, we would model a tree as an algebraic data type:
data Tree a = Leaf a | Branch [Tree a]
A tree is either a leaf or a branch, where a leaf contains some information, and a branch contains a list of trees.
In Python, we can model this using a dataclasses:
Given the three types (Tree, Branch, and Leaf), let's construct an example tree.
Let's write a function to sum up the values of a given tree.
For this we use Python's pattern matching feature (the match
-case
statement).
If the tree is a leaf, we return the value of that leaf.
If the tree is a branch, we sum up the totals of all subtrees.
Now let's compute the total over our example tree:
Now we are ready to develop our tree map visualization.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Tree Map
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version c08406b (Wed, 20 Nov 2024 12:30:00 GMT)