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

Free lesson

6.6 (2 of 3): Hierarchy storage and changing structure

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 in levels, each with one item above it.A chart of who is the boss of whom
TreeThe picture of a hierarchy, with the root at the top.A folder tree
RootThe top item, with nothing 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.A spreadsheet
TransformationChanging data from one form to another.Flat rows to a tree
DictionaryIn Python, names that each hold a value.{"Maths": ["Ava"]}
JSONA tree format of names and values in curly brackets.{"school": {}}
XMLA 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

A tree: arrows go from "Head teacher: root" to "Head of Maths" and "Head of Art", from "Head of Maths" to "Ava: teacher" and "Cy: teacher", and from "Head of Art" to "Ben: teacher".A tree: arrows go from "Head teacher: root" to "Head of Maths" and "Head of Art", from "Head of Maths" to "Ava: teacher" and "Cy: teacher", and from "Head of Art" to "Ben: teacher".

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

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.

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

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.

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 rowsStepTree
(before the first row)Start with an empty treeNothing yet
Maths, AvaMaths is new, so add it with AvaMaths: Ava
Art, BenArt is new, so add it with BenMaths: Ava. Art: Ben
Maths, CyMaths is already there, so add Cy under itMaths: 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.

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

  1. 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.
  2. 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.
  3. 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).
  4. 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).
  5. 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 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

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.