At My Fingertips

Documentation

Tree Map

A tree map visualizes a tree as nested rectangles.

Representing a Tree

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

An Example Tree

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

Loading...

Computing over the 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.

Loading...

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

Loading...

Visualizing Trees with Tree Maps

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

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)