Trees (OCR A Level Computer Science)

Revision Note

James Woodhouse

Expertise

Computer Science

Trees Data Structures

What is a Tree?

  • A tree is a connected, undirected form of a graph with nodes and pointers

  • Trees have a root node which is the top node; we visualise a tree with the roots at the top and the leaves at the bottom

  • Nodes are connected to other nodes using pointers/edges/branches, with the lower-level nodes being the children of the higher-level nodes 

  • The endpoint of a tree is called a leaf

  • The height of a tree is equal to the number of edges that connect the root node to the leaf node that is furthest away from it

trees
  • Nodes are connected by parent-child relationships

  • If a path is marked from the root towards a node, a parent node is the first one and the child node is the next

  • A node can have multiple children

  • A leaf node is a node with no children

What are Trees used for?

Trees can be used for a range of applications:

  • Managing folder structures

  • Binary Trees are used in routes to store routing tables

  • Binary Search Trees can be built to speed up searching

  • Expression trees can be used to represent algebraic and Boolean expressions that simplify the processing of the expression

Traversing Tree Data Structures

What is a Binary Tree?

  • A binary tree is a rooted tree where every node has a maximum of 2 nodes

  • A binary tree is essentially a graph and therefore can be implemented in the same way

  • For your exam, you must understand how to traverse a tree data structure, add new data to a tree and remove data from a tree.

 

binary-trees
  • The most common way to represent a binary tree is by storing each node with a left and right pointer. This information is usually implemented using 2D arrays 

    binary-trees-2

Traversing of a Binary Tree

  • There are 2 methods of traversing a binary; depth-first and breadth-first.

  • Both are important to understand and you should be able to output the order of the nodes using both methods

Depth-First Traversal of a Binary Tree

  • There are 3 methods to traverse a binary tree using a depth-first traversal: Pre-Order, In-Order and Post-Order. For the OCR specification, you are only required to understand post-order traversal

Post-Order Traversal

  1. Left Subtree

  2. Right Subtree

  3. Root Node

  • Using the outline method, imagine there is a dot on the right-hand side of each node

  • Nodes are traversed in the order in which you pass them on the right

    • Start at the bottom left of the binary tree

    • Work your way up the left half of the tree

    • Visit the bottom of the right half of the tree

    • Making your way back up toward the root node at the top

tree-illustration
  • The order of traversal is: 4, 2, 14, 6, 7, 1, 8, 9, 10

Breadth-First Traversal of a Tree

How Do I Traverse a Tree Using a Breadth First Traversal?

  • The breadth-first traversal of a tree simply means to:

    • Begin with the root node

    • Move to the left-most node under the root

    • Output each node, moving from left to right, just as though you were reading a book

    • Continue through each level of the tree

searching-through-a-binary-search-tree
  • Using the image above, a breadth-first traversal would output:

    • 10, 6, 15, 2, 8, 19, 4, 17, 21

Adding Data to a Tree

How Do You Add Data to a Binary Tree?

  • As mentioned above, a tree is a fundamental data structure in Computer Science and students must be able to understand how data can be added and removed in trees

  • To add a value to a binary tree you need to complete the following:

  1. Start with an empty tree or existing tree

    adding-to-a-binary-tree-1
  2. Identify the position where the new value should be inserted according to the rules of a binary tree

    • If the tree is empty, the new value will become the root node

    •  If the value is less than the current node’s value, move to the left child

    • If the value is greater than the current node’s value, move to the right child

    • Repeat this process until you reach a vacant spot where the new value can be inserted

      adding-to-a-binary-tree-2
  3. Insert the new value into the identified vacant spot, creating a new node at that position 

    adding-to-a-binary-tree-3
  4. After insertion, verify that the binary tree maintains its structure and properties

adding-to-a-binary-tree-4

Removing Data From a Tree

How Do You Remove Data from a Binary Tree?

  • To remove a value from a binary tree you need to complete the following:

  1. Start with an existing tree

    removing-from-a-binary-tree-1
  2. Search for the node containing the value you want to remove

    removing-from-a-binary-tree-2
  3. If the node is found:
    a. If the node has no children (leaf node), simply remove it from the tree
    b. If the node has one child, replace the node with its child
    c. If the node has two children, find the replacement node by:

    • Option 1: Find the minimum value in its right subtree (or the maximum value in its left subtree)

    • Option 2: Choose either the leftmost node in the right subtree or the rightmost node in the left subtree. Remove the replacement node from its original location and place it in the position of the node to be deleted.

      removing-from-a-binary-tree-3
  4. After removal, adjust the binary tree structure if necessary to maintain its properties and integrity

Tree Keywords

Keyword

Definition

Node

An item in a tree

Edge

Connects two nodes together and is also known as a branch or pointer

Root

A single node which does not have any incoming nodes

Child

A node with incoming edges

Parent

A node with outgoing edges

Subtree

A subsection of a tree consisting of a parent and all the children of a parent

Leaf

A node with no children

Traversing

The process of visiting each node in a tree data structure, exactly once

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

James Woodhouse

Author: James Woodhouse

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.