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 arranged in levels. Each item has one item above it. | A chart of who is the boss of whom |
| Tree | The picture of a hierarchy. It has a root at the top and branches below. | A folder tree |
| Root | The item at the top. Nothing is 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 on every row. | A spreadsheet |
| Transformation | Changing data from one form to another. | Flat rows to a tree |
| Dictionary | In Python, a set of names with a value for each name. | {"Maths": ["Ava"]} |
| JSON | A format with curly brackets, names and values. It is a tree. | {"school": {}} |
| XML | A format with tags (names in angle brackets) inside other tags. It is a tree. | <school><class/></school> |
The cells students fill in are left blank in this preview.
Quick check: the tree
Have a go
Quick check: the tree
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
The 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.
Good points and weak points
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.
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.