Program to Create Binary Tree in Python | Explained with Example

Program to Create Binary Tree in Python | Explained with Example

In an earlier tutorial, we have seen what is a binary tree. In this tutorial, we are going to write a program to create binary tree.

If you prefer learning by watching, check this video or keep continue reading.

For more programming tips for cracking interviews,

Now let’s continue reading.

Let’s take the same example of a binary tree.

reorder, inorder and post order traversal

How to Add and Read Binary Tree Nodes?

Every node in the binary tree has three parameters.

  • value of the node (says val)
  • link to the left child (says left)
  • link to the right child (says right)

A node can be represented as a class or structure with the three-parameter members in it.

Child and parent nodes are connected using link pointers (left and right).

If there is no left child to the node, the left (pointer/link) will be null/none. If there is no right child to the node, the right (pointer/link) will be null/none.

For the leaf node, both left and right links will be null/none.

Here is a simple program to demonstrate the binary tree.

Python Program

Prerequisite: To implement a binary tree in Python, you should know the basic Python syntax.

Here is the simple Python program to create a binary tree, add nodes and read the value of a particular node in the binary tree.

#structure/class for a node
#by defult, left and right pointers are None 
class node:
  def __init__(self, val, left=None, right=None):
    self.left=left
    self.val=val
    self.right=right


#adding element in the binary tree
 
#create root node
root=node(4)
 
#add left child node to the root node
root.left=node(1)
 
#add right child node to the root node
root.right=node(5)
 
#similarly add other elements in the binary tree
root.left.left=node(7)
root.left.right=node(3)
root.right.right=node(6)
 
#reading value of binary tree nodes
 
#root node
print("Root node: ", root.val)
 
#left child of binary tree
print("Left child of root node: ", root.left.val)
 
#right child of binary tree
print("Right child of root node: ", root.right.val)
 
#similarly reding values of other binary tree nodes
print("Other node", root.left.left.val)
print("Other node", root.left.right.val)
print("Other node"

Output:

Root node: 4
Left child of root node: 1
Right child of root node: 5
Other node 7
Other node 3
Other node 6

Similarly, you can implement this logic in any other programming language like C/C++ or Java.

In the above example, we have seen how we can access or read any element binary tree node using the pointer.

What’s Next?

This is a simple program to create binary tree. We have added one element at a time. You can take the array of elements as input and create a binary tree from it.

While solving programming questions, you need to traverse all the elements in the binary tree. There are different binary tree traversal algorithms you can use. Check this next.

Leave a Reply

Your email address will not be published. Required fields are marked *