-
Hajipur, Bihar, 844101
A binary tree is a tree where each node can have at most two children. These children are usually called the left child and the right child. Binary trees are one of the most important data structures because they build the base for several advanced structures like binary search trees, heaps, AVL trees and expression trees. They help in searching, sorting, evaluating expressions and organizing data in an efficient way. Once you understand binary trees, the rest of the tree-based structures become much easier to learn.
In this chapter, you will learn what binary trees are, how nodes are arranged, different types of binary trees, how to build binary tree nodes in Python and how traversal methods work. You will practice several examples and see how binary trees appear in real systems around you.
A binary tree is a hierarchical structure where each node contains:
A value
A reference to its left child
A reference to its right child
A simple binary tree looks like this:
A
/ \
B C
/ \
D E
Each node has up to two children. Some nodes may have one child or none.
The first node of the tree.
The child on the left side of a node.
The child on the right side of a node.
A node with no children.
A node that has at least one child.
Distance from the root to a specific node.
Longest path from a node down to a leaf.
These terms help when analyzing or building tree operations.
Binary trees help with:
Searching and sorting
Building efficient indexing systems
Expression evaluation
Decision-making algorithms
Memory management
Organizing structured data
Because each node has only two children, binary trees make traversal and manipulation predictable and fast.
Binary trees come in many variations. These are the most common ones:
Every node has either 0 or 2 children.
Every level is filled except possibly the last level.
All internal nodes have 2 children, and all leaf nodes are at the same level.
The height difference between left and right subtrees is small.
Each node has only one child, making the tree behave like a linked list.
Understanding these variations helps when you study BSTs and AVL trees later.
Here’s a simple class for a binary tree node:
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
Creating a small binary tree:
root = Node("A")
root.left = Node("B")
root.right = Node("C")
root.left.left = Node("D")
root.left.right = Node("E")
You now have a working structure you can use for traversing and practicing.
Traversal means visiting all nodes in a specific order. Binary trees support four popular traversal methods.
This is the most common traversal for binary search trees because it prints values in sorted order.
def inorder(node):
if not node:
return
inorder(node.left)
print(node.value)
inorder(node.right)
Useful for copying trees or saving them to a file.
def preorder(node):
if not node:
return
print(node.value)
preorder(node.left)
preorder(node.right)
Used in expression tree evaluation.
def postorder(node):
if not node:
return
postorder(node.left)
postorder(node.right)
print(node.value)
Visits nodes level by level.
from collections import deque
def level_order(root):
queue = deque([root])
while queue:
node = queue.popleft()
print(node.value)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
This method is simple and extremely useful in many scenarios.
Binary trees appear in many parts of software systems:
Compilers use binary trees to evaluate mathematical expressions.
AI decision trees commonly use binary structure for branching choices.
Binary trees help navigation systems optimize path checking.
Operating systems store memory segments using tree structures.
Some file systems use balanced tree variations for indexing.
The simple two-child rule makes binary trees flexible and powerful.
root = Node("Grandparent")
root.left = Node("Parent A")
root.right = Node("Parent B")
root.left.left = Node("Child A1")
root.left.right = Node("Child A2")
root.right.left = Node("Child B1")
You can print this structure using an inorder traversal or level order.
def print_tree(node, level=0):
if node:
print_tree(node.right, level + 1)
print(" " * level + str(node.value))
print_tree(node.left, level + 1)
This rotates the tree and displays it sideways, making the structure easy to see.
Call it like this:
print_tree(root)
node = Node("X")
node.left = Node("Y")
node.right = Node("Z")
root = Node(10)
root.left = Node(5)
root.right = Node(15)
inorder(root)
preorder(root)
postorder(root)
level_order(root)
root.left.left = Node(2)
root.left.right = Node(7)
root.right.left = Node(12)
root.right.right = Node(20)
print_tree(root)
A binary tree is a structured way to store data where each node can have up to two children. You learned about root, left child, right child, leaves, depth and different types of binary trees. You also created a binary tree node class in Python and explored traversal methods like inorder, preorder, postorder and level order. Binary trees are used in compilers, search engines, AI systems, memory management and many other technologies. With this foundation, you can now move on to binary search trees, where ordering rules make searching extremely fast.
Q1. Write a Python program to create a binary tree manually by defining node class and linking nodes.
Q2. Write a Python program to perform Inorder traversal on the binary tree and print the result.
Q3. Write a Python program to define and use Preorder and Postorder traversal functions.
Q4. Write a Python program to count all nodes present in the binary tree.
Q5. Write a Python program to insert a node into the correct position in a Binary Search Tree.
Q6. Write a Python program to check if the binary tree is empty.
Q7. Write a Python program to find the maximum value present in the binary tree.
Q8. Write a Python program to print all leaf nodes in the binary tree.
Q9. Write a Python program to use level-order traversal (BFS) to print the tree from top to bottom.
Q10. Write a Python program to build a binary tree from a list of numbers (in level order).