Photo AI

Last Updated Sep 27, 2025

Trees Simplified Revision Notes

Revision notes with simplified explanations to understand Trees quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

385+ students studying

Trees

Overview

A tree is a hierarchical data structure consisting of nodes. Trees are used to represent relationships, such as family trees, organisational charts, and file systems. In Computer Science, trees are widely used for efficient data searching, sorting, and hierarchical data representation.

Structure of a Tree

  • Node: The fundamental unit of a tree, containing data and references to child nodes.
  • Root: The topmost node in the tree.
  • Edge: A connection between a parent and a child node.
  • Parent: A node that has one or more child nodes.
  • Child: A node that has a parent node.
  • Leaf: A node with no children.
  • Subtree: A smaller tree within a larger tree, consisting of a node and its descendants.

Types of Trees

  • Binary Tree: Each node has at most two children, typically referred to as the left and right child.
  • Binary Search Tree (BST): A binary tree where the left child contains values less than the parent, and the right child contains values greater than the parent.
  • Multi-branch Tree: A tree where nodes can have more than two children.

Use Cases for Trees

  • Binary Search Trees: Efficient for searching, insertion, and deletion (O(log n) time complexity in a balanced tree).
  • File Systems: Organising files and directories.
  • Heaps: Priority queues.
  • Trie: Searching words in a dictionary.

Implementing a Tree

Dynamic Implementation Using Linked Nodes

A tree can be implemented dynamically using linked nodes.

lightbulbExample

Example: Pseudocode for a Binary Tree Node:

CLASS Node
    DATA value
    POINTER left
    POINTER right

CLASS BinaryTree
    POINTER root

    METHOD Add(value)
        IF root IS NULL
            root ← NEW Node(value)
        ELSE
            CALL AddRecursive(root, value)

    METHOD AddRecursive(current, value)
        IF value < current.value
            IF current.left IS NULL
                current.left ← NEW Node(value)
            ELSE
                CALL AddRecursive(current.left, value)
        ELSE
            IF current.right IS NULL
                current.right ← NEW Node(value)
            ELSE
                CALL AddRecursive(current.right, value)

Static Implementation Using an Array

A binary tree can also be implemented using an array where:

  • The root is at index 0.
  • The left child of a node at index i is at 2*i + 1.
  • The right child is at 2*i + 2.

Tree Traversals

Depth-First Traversal (Post-Order)

In post-order traversal, the nodes are visited in this order:

Left Subtree → Right Subtree → Root

lightbulbExample

Example Post-Order Traversal:

Given Tree:
      A
     / \\
    B   C
   / \\
  D   E

Post-Order: D → E → B → C → A

Pseudocode for Post-Order Traversal:

METHOD PostOrder(node)
    IF node IS NOT NULL
        CALL PostOrder(node.left)
        CALL PostOrder(node.right)
        OUTPUT node.value

Breadth-First Traversal

Also known as Level-Order Traversal, nodes are visited level by level, from left to right.

lightbulbExample

Example Breadth-First Traversal:

Given Tree:
      A
     / \\
    B   C
   / \\
  D   E

Breadth-First: A → B → C → D → E

Pseudocode for Breadth-First Traversal:

METHOD BreadthFirst(root)
    INITIALISE queue
    ENQUEUE root

    WHILE queue IS NOT EMPTY
        current ← DEQUEUE queue
        OUTPUT current.value
        IF current.left IS NOT NULL
            ENQUEUE current.left
        IF current.right IS NOT NULL
            ENQUEUE current.right

Adding and Removing Nodes in a Binary Search Tree

Adding a Node:

1. Start at the root. 2. Traverse left if the value is smaller or right if it is larger. 3. Insert the node at the appropriate leaf position.

Removing a Node:

1. If the node has no children: Remove it directly. 2. If the node has one child: Replace it with its child. 3. If the node has two children: Replace it with the in-order successor (smallest value in the right subtree).

Note Summary

infoNote

Common Mistakes

  1. Incorrect Traversal Order: Mixing up the order of operations in depth-first traversals (e.g., performing in-order instead of post-order).
  2. Unbalanced Trees: Not considering balancing techniques for binary search trees, which can degrade performance to O(n).
  3. Improper Index Calculation in Arrays: Miscalculating the left or right child index in static implementations.
  4. Null Pointer Errors: Forgetting to check if a node is null before accessing its children.
infoNote

Key Takeaways

  • Trees are hierarchical data structures, with binary trees and multi-branch trees being common types.
  • Depth-first (Post-Order) and Breadth-First Traversals are essential for exploring and processing tree nodes.
  • Trees can be dynamically implemented using linked nodes or statically using arrays.
  • Properly manage insertion and deletion in Binary Search Trees to maintain their efficiency.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Trees

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

120 flashcards

Flashcards on Trees

Revise key concepts with interactive flashcards.

Try Computer Science Flashcards

12 quizzes

Quizzes on Trees

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Trees

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Trees

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Trees

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Trees you should explore

Discover More Revision Notes Related to Trees to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Algorithms for the Main Data Structures

Stacks

user avatar
user avatar
user avatar
user avatar
user avatar

438+ studying

191KViews

96%

114 rated

Algorithms for the Main Data Structures

Queues

user avatar
user avatar
user avatar
user avatar
user avatar

296+ studying

196KViews

96%

114 rated

Algorithms for the Main Data Structures

Linked List

user avatar
user avatar
user avatar
user avatar
user avatar

394+ studying

198KViews

96%

114 rated

Algorithms for the Main Data Structures

Searching and Sorting Algorithms

user avatar
user avatar
user avatar
user avatar
user avatar

481+ studying

196KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered