Who is your boss?
At school, your teacher is above you. The head of department is above your teacher. The head teacher is above the head of department. ("Above" means in charge of.)
Draw it and you get a shape like a tree. One person at the top. More people below. Computers store data in this shape too.
Key words
| Word | What it means | Example |
|---|---|---|
| Hierarchy | Items in levels, each with one item above it. | A chart of who is the boss of whom |
| Tree | The picture of a hierarchy, with the root at the top. | A folder tree |
| Root | The top item, with nothing above it. | Head teacher |
| Parent | The item directly above another item. | The head of department |
| Child | An item directly below its parent. | A teacher |
| Leaf | An item with no children. | A file in a folder tree |
| Flat data | Data in simple rows with the same columns. | A spreadsheet |
| Transformation | Changing data from one form to another. | Flat rows to a tree |
| Dictionary | In Python, names that each hold a value. | {"Maths": ["Ava"]} |
| JSON | A tree format of names and values in curly brackets. | {"school": {}} |
| XML | A tree format of tags (names in angle brackets) inside tags. | <school><class/></school> |
The cells students fill in are left blank in this preview.
A school as a tree
Quick check: the tree
Have a go
Quick check: the tree
3 questionsThe rules of a hierarchy
A hierarchy has three rules.
- There is one root at the top.
- Every other item has exactly one parent.
- An item can have many children, or none. An item with none is a leaf.
Where hierarchies are used
| Example | The root | Why a tree helps |
|---|---|---|
| Folders on a computer | The top folder | Files are grouped and easy to find |
| A company org chart | The managing director | Everybody knows who their boss is |
| An XML or JSON file | The outer tag or bracket | Data sits inside data, so parts stay together |
| A website menu | The home page | Visitors move from big topics down to small topics |
| A music app library | All music | Artist, then album, then song: each song sits in one album |
The cells students fill in are left blank in this preview.
A tree is quick but cannot share
A hierarchy is quick when you go down from the top. Start at Maths and you find Ava and Cy at once.
It is weak when an item belongs in two places. A teacher of Maths and Art cannot have two parents. You would repeat the teacher. Later you will learn other ways to store data that fix this.
Try it: a tree in Python
school = {
"Maths": ["Ava", "Cy"],
"Art": ["Ben"],
}
print(school["Maths"])
print(len(school["Maths"]))
for department in school:
for name in school[department]:
print(department, "->", name)
# Add "Music": ["Dee"] to the dictionary and run it again.
Make a free account to save your work and track your progress.
Flat to tree and back
A spreadsheet is flat. Every row repeats the department name. A tree writes the department once and puts the names below it.
You change structure when you move between the two. Going flat to tree groups rows. Going tree to flat repeats the parent on every row.
Worked example: flat to tree
| Flat rows | Step | Tree |
|---|---|---|
| (before the first row) | Start with an empty tree | Nothing yet |
| Maths, Ava | Maths is new, so add it with Ava | Maths: Ava |
| Art, Ben | Art is new, so add it with Ben | Maths: Ava. Art: Ben |
| Maths, Cy | Maths is already there, so add Cy under it | Maths: Ava, Cy. Art: Ben |
The cells students fill in are left blank in this preview.
Match the job to the structure
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Your turn: flat to tree and back
A school keeps rows of department and teacher. Fill in the four functions. Two change the structure from flat to tree and back. One counts. One follows the parents up to the top. Press Run Tests to check.
def to_tree(rows):
# rows is a flat list. Each row is a list of two items: [department, name].
# Give back a dictionary: each department points to a list of its names, in order.
# [["Maths", "Ava"], ["Art", "Ben"], ["Maths", "Cy"]]
# gives {"Maths": ["Ava", "Cy"], "Art": ["Ben"]}
return None
def to_flat(tree):
# The opposite. Give back a flat list of [department, name] rows.
# Go through the departments in order, and through each list of names in order.
return None
def count_names(tree, department):
# Give back how many names are in that department. Give back 0 if there is no such department.
return None
def chain_to_top(parents, name):
# parents says who each person reports to. The top person has None.
# {"Head": None, "Ava": "Head", "Ben": "Ava"}
# chain_to_top(parents, "Ben") gives ["Ben", "Ava", "Head"]
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Your turn: draw a tree
A sports club has a chair. Under the chair are a treasurer and a coach. Under the coach are two team captains. Change the diagram to show this tree. Label every box.
flowchart TD
chair[Chair]
chair --> a[Change me]
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Watch: hierarchy storage
Press Play on the short film below. It shows a school as a tree, where trees are used, flat data and a tree, then one myth.
Film: hierarchy storage
Hierarchy storage and changing structure
Every scene, as text
- Who is your boss? Head teacher, with an arrow down to Head of department, then Your teacher, then You (Head teacher: one person at the top). Computers store data in this shape too.
- A school as a tree. Head teacher, with arrows down to Head of Maths and Head of Art; Head of Maths, with arrows down to Ava and Cy; Head of Art, with an arrow down to Ben (Head teacher: the root, nothing above; Head of Art: parent of Ben; Ava, Cy and Ben: leaves, no children). An item can have many children, or none.
- Where hierarchies are used. Folders on a computer (root: the top folder). An XML or JSON file (root: the outer tag or bracket). A company org chart (root: the managing director). A website menu (root: the home page).
- Flat to tree and back. Flat rows: Maths, Ava; Art, Ben; Maths, Cy (flat data: simple rows, same columns). An arrow down, flat to tree, and an arrow up, tree to flat (flat to tree: groups the rows; tree to flat: department name on every row). The tree: Maths, with arrows down to Ava and Cy; Art, with an arrow down to Ben (each department: written once, names below).
- Myth (false): a teacher of two departments can have two parents. That is not true: a hierarchy has one root, and every other item has one parent. Maths, with an arrow down to Teacher of Maths and Art, and Art, with an arrow down to Teacher of Maths and Art (Teacher of Maths and Art: written twice). A hierarchy is weak when an item belongs in two places.
Final check
Have a go
Final check
5 questionsRemember
Important
A hierarchy has one root, and every other item has one parent. Folders, org charts and XML are trees. Moving between flat and tree needs grouping or repeating.