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 0d7866a (Fri, 20 Dec 2024 07:46:38 GMT)