At My Fingertips

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:

Loading...

Given the three types (Tree, Branch, and Leaf), let's construct an example tree.

Loading...

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.

Loading...

Now let's compute the total over our example tree:

Loading...

Now we are ready to develop our tree map visualization.

Loading...

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 c3a8286 (Mon, 14 Oct 2024 13:30:28 GMT)