Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Hierarchy storage and changing structure

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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

WordWhat it meansExample
HierarchyItems arranged in levels. Each item has one item above it.A chart of who is the boss of whom
TreeThe picture of a hierarchy. It has a root at the top and branches below.A folder tree
RootThe item at the top. Nothing is above it.Head teacher
ParentThe item directly above another item.The head of department
ChildAn item directly below its parent.A teacher
LeafAn item with no children.A file in a folder tree
Flat dataData in simple rows, with the same columns on every row.A spreadsheet
TransformationChanging data from one form to another.Flat rows to a tree
DictionaryIn Python, a set of names with a value for each name.{"Maths": ["Ava"]}
JSONA format with curly brackets, names and values. It is a tree.{"school": {}}
XMLA 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

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

  1. There is one root at the top.
  2. Every other item has exactly one parent.
  3. An item can have many children, or none. An item with none is a leaf.

Where hierarchies are used

ExampleThe rootWhy a tree helps
Folders on a computerThe top folderFiles are grouped and easy to find
A company org chartThe managing directorEverybody knows who their boss is
An XML or JSON fileThe outer tag or bracketData sits inside data, so parts stay together
A website menuThe home pageVisitors move from big topics down to small topics
A music app libraryAll musicArtist, 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

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

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.