Skip to content
ComputerCS
Start learning

Computational Structures · Lesson 4.1.4

Trees and Hierarchical Data

Understand rooted trees, parent–child relationships, leaves, depth and traversal, and recognise tree structures in file systems and search indexes.

01

Learning objectives

  • Identify nodes, edges, roots, leaves, parents and children.
  • Explain the difference between a general tree and a binary tree.
  • Trace preorder, inorder and postorder traversal on a small tree.
  • Describe how hierarchical structures support search and organisation.
02

Lesson notes

A tree is a hierarchical structure made of nodes connected by edges. A rooted tree has one designated root; each other node has one parent and may have zero or more children. A node without children is a leaf. The number of edges on the path from the root to a node is its depth; the longest downward path helps define tree height.

A binary tree restricts each node to at most two children, commonly called left and right. A binary search tree adds an ordering rule: values in the left subtree are smaller than the node and values in the right subtree are larger, subject to the chosen duplicate policy. This can make search efficient when the tree remains balanced, but a badly skewed tree can behave like a linked list.

Traversal is a systematic way to visit nodes. Preorder visits root, left, right; inorder visits left, root, right; postorder visits left, right, root. In a binary search tree, inorder traversal visits keys in sorted order. Traversal is useful for displaying, searching, copying or deleting hierarchical data.

Trees naturally represent folders, organisational charts, XML/HTML documents, decision processes and indexes. The structure makes relationships explicit. The shape matters for efficiency: a balanced tree has height roughly proportional to log n, while a chain of n nodes has height n.

03

Worked code examples

Worked example

The recursive inorder traversal visits the left subtree, then the node, then the right subtree.

tree = {"value": 8, "left": {"value": 3, "left": None, "right": None}, "right": {"value": 10, "left": None, "right": None}}
def inorder(node):
    if node is None:
        return []
    return inorder(node["left"]) + [node["value"]] + inorder(node["right"])
print(inorder(tree))
Output
[3, 8, 10]
04

Key terminology

Root

The designated starting node of a rooted tree.

Leaf

A node with no children.

Depth

The number of edges between the root and a node.

Inorder traversal

A binary-tree traversal that visits left subtree, node, then right subtree.

05

Common misconceptions

Common mistake

A tree can contain cycles and still be a tree.

Correct understanding

A tree is acyclic and connected; adding a cycle violates the tree structure.

Common mistake

Every binary tree is automatically a binary search tree.

Correct understanding

A binary search tree must also satisfy an ordering invariant.

06

Check your understanding

1. What is a leaf node?

2. Which traversal of a binary search tree normally produces sorted order?

3. A binary tree node has at most:

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.