Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Trees quickly and effectively.
478+ students studying
A tree is a hierarchical data structure that consists of nodes connected by edges. It is widely used for representing hierarchical relationships such as organisational structures, file systems, and decision-making processes. Trees are a specific type of graph without cycles, where one node is designated as the root, and every other node is a descendant of the root.
i
:
2*i + 1
2*i + 2
(i-1) // 2
Example:
# Binary Tree represented as an array
tree = [None] * 7 # Fixed-size tree
def add_to_tree(value, index):
if index < len(tree):
tree[index] = value
add_to_tree(10, 0) # Root
add_to_tree(5, 1) # Left child of root
add_to_tree(15, 2) # Right child of root
print(tree) # Output: [10, 5, 15, None, None, None, None]
Using nodes with pointers to the left and right children.
Example:
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
# Creating nodes
root = Node(10)
root.left = Node(5)
root.right = Node(15)
A class-based implementation makes it easier to manage trees.
Example:
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BinaryTree:
def __init__(self):
self.root = None
def insert(self, value):
if not self.root:
self.root = TreeNode(value)
else:
self._insert(self.root, value)
def _insert(self, current, value):
if value < current.value:
if current.left:
self._insert(current.left, value)
else:
current.left = TreeNode(value)
else:
if current.right:
self._insert(current.right, value)
else:
current.right = TreeNode(value)
Traversal involves visiting all the nodes in a tree in a specific order.
Visit the root, then recursively visit the left and right subtrees.
Example:
def preorder(node):
if node:
print(node.value, end=" ")
preorder(node.left)
preorder(node.right)
Visit the left subtree, then the root, then the right subtree. Commonly used for BSTs to retrieve sorted data.
Example:
def inorder(node):
if node:
inorder(node.left)
print(node.value, end=" ")
inorder(node.right)
Visit the left and right subtrees first, then the root.
Example:
def postorder(node):
if node:
postorder(node.left)
postorder(node.right)
print(node.value, end=" ")
Visit nodes level by level using a queue.
Example:
from collections import deque
def level_order(root):
if not root:
return
queue = deque([root])
while queue:
current = queue.popleft()
print(current.value, end=" ")
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
In a Binary Search Tree (BST):
Example:
def insert(node, value):
if not node:
return TreeNode(value)
if value < node.value:
node.left = insert(node.left, value)
else:
node.right = insert(node.right, value)
return node
Removing a node requires handling three cases:
Example:
def delete_node(root, value):
if not root:
return root
if value < root.value:
root.left = delete_node(root.left, value)
elif value > root.value:
root.right = delete_node(root.right, value)
else:
# Node with one or no child
if not root.left:
return root.right
elif not root.right:
return root.left
# Node with two children
temp = min_value_node(root.right)
root.value = temp.value
root.right = delete_node(root.right, temp.value)
return root
def min_value_node(node):
current = node
while current.left:
current = current.left
return current
None
nodes in procedural or pointer-based implementations.Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
90 flashcards
Flashcards on Trees
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards9 quizzes
Quizzes on Trees
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Trees
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Trees
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Trees
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Trees to Deepen Your Understanding and Improve Your Mastery
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!
Report Improved Results
Recommend to friends
Students Supported
Questions answered