Trees
Overview
A tree shows items that belong inside other items. The folders on a computer are a tree: a project folder holds a source code folder, and the source code folder holds files. In this tutorial, you will build a tree of colors on a page. Each color can hold lighter and darker shades. You will open and close parts of the tree, react when a person picks a color, find the path from the top of the tree to any item, and add a small picture to each color.
Goals
You have the following goals for this tutorial:
By the end of this lesson, you can
- Add a tree to a page with
AddTree, and fill it withTreeItemobjects inside other items - Use a tree with the keyboard
- Run a behavior when a person activates an item
- Find the path from the top of a tree to an item
Warm up
Open the file browser on your computer, or picture one. Your files are inside folders, and folders are inside other folders. Draw this as a diagram on paper, with the top folder at the top and lines down to what is inside it. You may also use physical contraptions like blocks, paper, or notes, to build it. Why do you think computers show folders this way instead of one long list of every file?
Vocabulary
You will be learning about the following vocabulary words:
| Term | Definition |
|---|---|
| Tree | A control that shows items inside other items, like folders inside folders. |
| Tree item | One entry in a tree. In Quorum, each entry is a TreeItem object. |
| Hierarchy | A way of organizing things in levels, where each thing can hold other things below it. |
| Parent | An item that holds other items. Blue is the parent of Cyan in this tutorial. |
| Child | An item held by another item. Cyan is a child of Blue. |
| Subtree | An item together with everything inside it. |
| Open and close | Showing or hiding the children of an item. This is also called expanding and collapsing. |
| Path | The list of items from the top of the tree down to one item, such as Blue, then Purple, then Dark Purple. |
| Cardinality | How many items there are, such as the 3 items in the path to Dark Purple. |
| Ordinality | An item's position in order, such as first, second or third. Blue is the first item in the path to Dark Purple. |
| Icon | A small picture shown next to the text of an item. |
New Code to Learn
You will be using the following new pieces of code:
| Quorum Code | Code Example | Explanation |
|---|---|---|
| AddTree(text name) | Tree tree = page:AddTree("Colors") | Adds a tree to a page. The name is what a screen reader calls the tree. |
| TreeItem name | TreeItem red | Creates a new item for a tree. |
| SetName(text name) | red:SetName("Red") | Sets the text that is shown and read aloud for an item. |
| Add(TreeItem item) | tree:Add(red) | Adds an item to the top level of the tree, or as a child of another item. |
| SetBehavior(Behavior behavior) | red:SetBehavior(behavior) | Sets the behavior that runs when this item is activated. |
| Open() | blue:Open() | Opens an item so its children show. |
| GetTreePath() | Array<TreeItem> path = item:GetTreePath() | Returns the items from the top of the tree down to this item, in order. |
| SetIcon(TextureRegion icon) | red:SetIcon(icon) | Shows a small picture next to the item. |
| SetIconColor(ColorGroup color) | item:SetIconColor(shade) | Sets the color the item's picture is drawn in. |
| CreateGame() | action CreateGame | In a class that is a Form, an action Quorum calls once the window is open. Pictures can be made here. |
CSTA Standards
This tutorial covers the following CSTA standards:
For teachers · 2026 CSTA K–12 Computer Science Standards
High School · Programming
Analyze how a segment of code works, including the role of parameters, return values, and data structures.
Also in 6 other lessons
Middle School · Programming
Analyze the roles of iteration, selection, variables, and procedures in a segment of code.
Also in 3 other lessons
Specialty II · Software Development
Apply associative and hierarchical data structures to solve computational problems.
Only this lesson so far
Specialty I · Software Development
Refine a user interface design using accessibility and responsive design tools.
Also in 13 other lessons
High School · Algorithms & Design
Design an algorithm using appropriate data structures to solve a problem or express an idea.
Also in 2 other lessons
Explore
What is a tree?
A tree shows items in a hierarchy: levels, where each item can hold other items. Quorum Studio, for example, shows each project as a tree. A project holds a Source Code folder, and the folder holds three files.
An item that holds other items is a parent. The items it holds are its children. A parent and everything inside it is a subtree. A person can open a parent to show its children or close it to hide them.
In Quorum, a tree is a Tree object, and every entry in it is a
TreeItem. You add TreeItem objects to the tree, and you add
TreeItem objects to other TreeItem objects to make children.
Making a tree of colors
Make a new Form project in Quorum Studio. You will need two files.
First, add a class named OutBehavior.quorum. For now, it only writes
the name of the item that was activated:
use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.TreeItem
class OutBehavior is Behavior
action Run(BehaviorEvent event)
TreeItem item = cast(TreeItem, event:GetItem())
text colorName = item:GetName()
output colorName
end
end
event:GetItem() returns the item that was activated as a general
Item. The cast tells Quorum to treat it as a TreeItem, so you can
ask for its name.
Next, replace the code in Main.quorum with this program:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Tree
use Libraries.Interface.Controls.TreeItem
class Main
OutBehavior outBehavior
action Main
Form form
Page page = form:GetMainPage()
page:SetTitle("Colors")
Tree tree = page:AddTree("Colors")
tree:SetDescription("Colors and their shades. Open a color to see its shades.")
TreeItem red = MakeItem("Red")
TreeItem pink = MakeItem("Pink")
TreeItem blue = MakeItem("Blue")
TreeItem cyan = MakeItem("Cyan")
TreeItem purple = MakeItem("Purple")
TreeItem darkPurple = MakeItem("Dark Purple")
TreeItem lightPurple = MakeItem("Light Purple")
TreeItem green = MakeItem("Green")
tree:Add(red)
tree:Add(blue)
tree:Add(green)
red:Add(pink)
blue:Add(cyan)
blue:Add(purple)
purple:Add(darkPurple)
purple:Add(lightPurple)
form:Display()
end
action MakeItem(text name) returns TreeItem
TreeItem item
item:SetName(name)
item:SetBehavior(outBehavior)
return item
end
end
Here is what the program does:
page:AddTree("Colors")adds a tree to the page. The name is what a screen reader calls the tree, andSetDescriptiontells it a little more.MakeItemis an action you write yourself. It makes oneTreeItem, sets its name and gives it the behavior. Writing this once saves you from repeating three lines for each of the eight colors.SetNamesets the text shown on the item's row. A screen reader reads the same text.tree:Add(red)puts Red at the top level of the tree.red:Add(pink)puts Pink inside Red, so Red becomes a parent.
Run the program. Only Red, Blue and Green show at first, because their children are closed. The tree sits at the left of the page, so it doesn't move sideways as items open and close.
Using a tree with the keyboard
A tree can be used fully from the keyboard:
- Up Arrow and Down Arrow move to the item above or below.
- Right Arrow opens a closed item. If it's already open, it moves to the first child.
- Left Arrow closes an open item. If it's already closed, it moves up to the parent.
- Home and End jump to the first or last item.
- Typing a letter jumps to the next item that starts with it.
- Enter activates the selected item.
With the mouse, clicking an item selects it. Double-clicking an item opens or closes it and activates it.
Try activating Purple. The program writes "Purple".
If you want a part of the tree to start open, call Open on that item
after you add its children:
blue:Open()
Finding the path to an item
Sometimes the name of an item is not enough. "Dark Purple" makes more sense when you know it is inside Purple, which is inside Blue. The list of items from the top of the tree down to one item is its path.
Every TreeItem has a GetTreePath action. It returns the path as an
Array of TreeItem objects, starting at the top and ending with the item
itself. To learn more about arrays, refer to the
Arrays tutorial.
Change OutBehavior.quorum to this:
use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.TreeItem
use Libraries.Containers.Array
class OutBehavior is Behavior
action Run(BehaviorEvent event)
TreeItem item = cast(TreeItem, event:GetItem())
Array<TreeItem> path = item:GetTreePath()
text message = ""
integer counter = 0
repeat while counter < path:GetSize()
TreeItem pathItem = path:Get(counter)
if counter > 0
message = message + ", then "
end
message = message + pathItem:GetName()
counter = counter + 1
end
output message
end
end
The repeat while loop visits each item in the path, in order, and adds
its name to message. To learn more about loops, refer to the
Repeat tutorial.
Run the program, open Blue and Purple, and activate Dark Purple. The program writes "Blue, then Purple, then Dark Purple".
Counting and ordering
Two ideas help when you work with the items in a tree or a path.
Cardinality is how many items there are. The path to Dark Purple has a
cardinality of 3, and path:GetSize() returns it. Ordinality is an
item's position in order: first, second, third. In the path, Blue is first,
Purple is second and Dark Purple is third. People count positions from 1,
but an Array counts them from 0, so path:Get(0) is the first item, Blue.
A tree has ordinality too: a parent's children have an order, and a person
moving with the Down key meets them in that order.
Screen readers and braille displays bring these ideas to people too. When a person moves through a tree or a list, many screen readers say an item's position and the number of items, such as "2 of 3," and a braille display shows the same words under the person's fingers. That is ordinality and cardinality, given by speech or by touch.
Adding pictures to the items
An item can show a small picture, called an icon, before its name. Here, each color gets a circle of its own color.
Pictures can only be made once the program's window is open. Until now,
your programs built the whole page in Main and then called
form:Display(), which opens the window. To make pictures, the page has to
be built after the window opens. A class that is a Form has an action for
that: Quorum calls its CreateGame action once the window is open.
Change Main.quorum to this program. OutBehavior.quorum stays the same.
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Icon
use Libraries.Interface.Controls.Tree
use Libraries.Interface.Controls.TreeItem
use Libraries.Game.Graphics.Color
class Main is Form
OutBehavior outBehavior
action Main
Display()
end
action CreateGame
Page page = GetMainPage()
page:SetTitle("Colors")
Tree tree = page:AddTree("Colors")
tree:SetDescription("Colors and their shades. Open a color to see its shades.")
Color color
TreeItem red = MakeItem("Red", color:Red())
TreeItem pink = MakeItem("Pink", color:Pink())
TreeItem blue = MakeItem("Blue", color:Blue())
TreeItem cyan = MakeItem("Cyan", color:Cyan())
TreeItem purple = MakeItem("Purple", color:Purple())
TreeItem darkPurple = MakeItem("Dark Purple", color:CustomColor(0.25, 0, 0.25, 1))
TreeItem lightPurple = MakeItem("Light Purple", color:CustomColor(0.75, 0, 0.75, 1))
TreeItem green = MakeItem("Green", color:Green())
tree:Add(red)
tree:Add(blue)
tree:Add(green)
red:Add(pink)
blue:Add(cyan)
blue:Add(purple)
purple:Add(darkPurple)
purple:Add(lightPurple)
parent:Form:CreateGame()
end
action MakeItem(text name, Color shade) returns TreeItem
Icon icon
icon:LoadFilledCircle(32, shade)
TreeItem item
item:SetName(name)
item:SetIcon(icon)
item:SetIconColor(shade)
item:SetBehavior(outBehavior)
return item
end
end
Here is what changed:
class Main is Formmakes the program itself a form.MaincallsDisplay()to open the window, the same asform:Display()before.- The page is built in
CreateGameinstead ofMain.GetMainPage()works the same way as before, withoutform:in front. - The last line,
parent:Form:CreateGame(), lets the form do its own setup after your code: it shows the page and gives the first control the focus. LoadFilledCircle(32, shade)makes a filled circle with a radius of 32 pixels. The row shrinks it to the height of a line, which keeps its edge smooth.SetIconputs it on the item's row, andSetIconColorgives it the colorshade.CustomColor(red, green, blue, alpha)mixes a color from four numbers between 0 and 1. The last number, alpha, is how solid the color is. Refer to the Color class documentation for more.
An icon is only a picture. A screen reader still reads the item's name, so make sure the name says everything a person needs to know. Here, the name already says the color.
Engage
In this activity, you build a tree for something that has levels. The goal is to organize information in a hierarchy and to move through a tree with the keyboard. Choose something with levels, such as your school (grades, then classes, then students), a family of animals, or the parts of a video game. Draw it on paper first or create it with physical items. Then build it as a tree with at least three levels, and when an item is activated, have the program write its full path.
Wrap up
A tree is a way to show data that has levels. Think back to your drawing
from the warm up. Which parts of your drawing became TreeItem objects,
and which lines became calls to Add? When would a tree be a better
choice than a list, and when would a list be better?