• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Program to Create Binary Tree in Python | Explained with Example

Aniruddha Chaudhari/12489/0
CodePython

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,

Subscribe CSEstack Youtube Channel

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.

Python Interview Questions eBook

Binary Tree
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Binary Tree (BT)

  1. BT- Introduction
  2. BT- Create Binary Tree
  3. BT- Pre/In/Post Order Traversal
  4. BT- Height 
  5. BT- Level Order Traversal
  6. BT- Min and Max Heap
  7. BT- Parent of a Given Node
  8. BT- Ancestors of Given Node
  9. BT- Lowest Common Ancestor
  10. BT- Check Sub Tree

Binary Search Tree (BST)

  1. BST- Introduction
  2. BST- Search Node
  3. BST- Insert Node

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard