# Grasshopper Data Tree Editing

## Introduction

When you connected a panel to an output for the first time in Grasshopper, you may have noticed an interesting structure. During the tutorials we explained before, we used these structures to get information from one node to another one. In this intermezzo we will dive into the topic of so called data trees. When we are speaking about data trees, we are relating this to the structure of data in Grasshopper. This structure can be easily visualized like a tree, and therefore is called a data tree.

By connecting a Param Viewer to the output of a node or panel, Grasshopper visualizes the data tree like the following illustration:

## Data tree structure

Data trees in Grasshopper are built from three different structures:

- Branches;
- Lists;
- Items

Branches are defined by a numerical structure between brackets, like {0;0;0}. The amount of numbers between the brackets, indicates the depth of the tree. In this example, the data tree has three levels. One branch, which splits in a branch, which splits in a branch again. The branch of a tree can be found in the gray lines of a panel.

Within a branch of a data tree, sometimes a list can be found or just one item. As explained earlier, these items can have different data types.

To access a specific item in a data tree, we use the index in front of the item. This is a crucial difference to normal programming languages: instead of using the name of the item, we are using an index in a branch.

When we have multiple data trees in script, which is the case in most algorithms, the data trees need to interact with each other in the correct order. Therefore you will need to learn how to manipulate data trees in Grasshopper.

## Changing data trees

When you execute a certain function on a set of data, the data tree may change. Therefore, the structure of the branches is changed as well. Let’s imagine we have a data tree with just one branch: {0;0}. If this branch contains data that is grafted, the branch structure will change to {0;0;0}: an extra branch is added.

The numbers in a branch structure, indicate where the data from a branch originates from. For example: a branch with structure {2;1;0} contains the first of the second of the third branch of the original data set.

## Interaction between data trees

In the following chapter we will discuss several examples of data tree interaction. In the first scenario we have data tree that contains numbers. For each item in the data tree, we want to increase the value by 1. In the second scenario, each branch will contain items that are increased by different amounts. Finally, we have the most complex situation where data manipulation really is important: a data tree with numbers where each item is increase by a different amount.

### Data tree interacting with one item

The first scenario starts with two data sets. The first data set contains a complex data tree with four branches. All branches start with {0;x} which indicates that the data comes from the same origin. Each branch also contains a list of values. The last branch only contains on single item.

The second data set only contains one value. Although it may seem a bit misleading, this structure is in fact also a data tree. As you can see, the data set only has one root branch, called {0}.

Now we will add these data sets together. First, imagine what you think will happen. As you can see, each branch interacts with the same item, since it only has one value.

As you can see, changing the name of the second data set does not matter. The tree only contains one item and therefore interacts with all the branches of the first tree.

### Data tree interacting with a list

In the second example, instead of using a single item in the second data set, we are using a list of values. If we repeat the addition from the last example, you can see that corresponding indices of a branch interact with each other. Item 0 of data set 1 add together with item 0 of data set 2.

You may notice that in the last branch two items are added. This has to do with the fact that each item in the branches has to be counted. Therefore, in the last branch, to all numbers 10 is added.

Now let’s experiment and change the structure of data set 2. Instead of using one list, we add each number to an individual branch. What happens? Each branch interacts with its corresponding branch, with an exception for the last branch. No data is available for the last branch in the second data set. Therefore, the data of the previous branch is used.

### Data tree interacting with another data tree

Based on the last experiment in the previous chapter, we can expect what will happen when we are adding two complex data trees together. Each corresponding items in the corresponding branches add together.

Now what happens if we add another depth level to one of the trees. The result will be based on the tree with the highest depth. Therefore each branch structure contains three values like {a;b;c}. Branch {0;0;0} interacts with the first branch of data set 1, which results in a list of three values. The process continues until {0;0;2}. Now a problem occurs: there are not enough branches in data set 1 to interact with all the extra branches of data set 2. Therefore, the last branch of data set 1 is used to interact with all other branches of data set 2. This results in branches with the items 14, 15, 16, 17, 18, 19 and 20.

## How to continue?

You now have seen some examples of how data trees interact with each other. During the intermediate tutorials on the Grasshopper Toipedia, we try to avoid data tree manipulating as much as possible. However, when you are trying to develop more complex algorithms, this will not always be possible anymore. In the next intermezzo: Basic Data Tree Actions, we will elaborate all available data tree manipulation tools.