A binary tree is a type of data structure in which each node has at most two children, referred to as the left child and the right child. Binary trees are used in various aspects of computer science including sorting and searching. In this tutorial, we will create a binary tree in Python and complete Python code to print a binary tree.
Step 1: Define the Binary Tree
Firstly, we need to create the binary tree structure. Here, we’ll define a simple Node class:
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
This class represents a node in the binary tree, which contains some data and pointers to its left and right children.
Step 2: Insert Data Into the Binary Tree
To insert data into the tree, we’ll define an insert function:
def insert(root, data):
if root is None:
return Node(data)
else:
if data <= root.data:
root.left = insert(root.left, data)
else:
root.right = insert(root.right, data)
return root
In this function, if the tree is empty (i.e., the root is None), we create a new node with the given data and return it. If the tree is not empty, we recursively insert the data into the left or right subtree depending on its value.
Step 3: Print the Binary Tree
There are several ways you can “print” a binary tree. Here, we’ll implement in-order, pre-order, and post-order traversal methods:
- In-Order Traversal: In this traversal method, the left subtree is visited first, then the root and later the right subtree.
def print_inorder(root):
if root:
print_inorder(root.left)
print(root.data, end=" ")
print_inorder(root.right)
- Pre-Order Traversal: In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
def print_preorder(root):
if root:
print(root.data, end=" ")
print_preorder(root.left)
print_preorder(root.right)
- Post-Order Traversal: In this traversal method, the root node is visited last. First, we traverse the left subtree, then the right subtree, and finally the root node.
Step 4: Python Code to Print a Binary Tree
Here is the complete code:
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert(root, data):
if root is None:
return Node(data)
else:
if data <= root.data:
root.left = insert(root.left, data)
else:
root.right = insert(root.right, data)
return root
def print_inorder(root):
if root:
print_inorder(root.left)
print(root.data, end=" ")
print_inorder(root.right)
def print_preorder(root):
if root:
print(root.data, end=" ")
print_preorder(root.left)
print_preorder(root.right)
def print_postorder(root):
if root:
print_postorder(root.left)
print_postorder(root.right)
print(root.data, end=" ")
root = Node(50)
root = insert(root, 30)
root = insert(root, 20)
root = insert(root, 40)
root = insert(root, 70)
root = insert(root, 60)
root = insert(root, 80)
print("In-order traversal:")
print_inorder(root)
print("\nPre-order traversal:")
print_preorder(root)
print("\nPost-order traversal:")
print_postorder(root)
When you run this code, it will create a binary tree and print the tree using in-order, pre-order, and post-order traversals.
Conclusion
In this Python tutorial, I explained binary tree in Python and Python Code to Print a Binary Tree.
You may also like:
- Python program to display the current date and time
- Write a Python program to print the number of elements present in an array
- Calculate the Factorial of a Number in Python
- add zeros before a number in Python
- Add two binary numbers in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.