Python binary tree implementation

In this python tutorial, you will learn about binary tree implementation in python with examples. Here we will check:

  • What is a binary tree in python?
  • How to install binary tree in python
  • Python binary tree implementation
  • Python build a binary tree from the list
  • Python build a random binary tree
  • Python create a random binary tree of a given height
  • Python create a random perfect binary tree of a given height

What is a binary tree in python?

  • A binary tree is a data structure in which elements have at most two children is called a binary tree.
  • Each element in a binary tree can have only two children.
  • Each tree consists of a root node as the parent node, and the left node and right node as child nodes.
  • A binary tree can be represented in different ways with a different data structure such as list etc.
  • Binary tree libraries help to directly implement a binary tree.

You may like Groupby in Python Pandas and How to subtract two numbers in Python.

How to install binary tree in python

To install binary tree in python we have to type the below command in the terminal. Once it is installed then you are ready to go.

Pip install binarytree

Python binary tree implementation

Here, we can see python binary tree implementation.

Firstly, we need to import “from binarytree import Node” and the node class represents the structure of a particular node in the binary tree. The attribute of this class are values left, right.

Example:

from binarytree import Node 
root = Node(5) 
root.left = Node(8) 
root.right = Node(10)  
print('Binary tree :', root) 

To get the output, I have used print(‘Binary tree:’, root). You can refer to the below screenshot for the output.

Python binary tree implementation
Python binary tree implementation

You may like, How to swap two numbers in Python.

Python build a binary tree from the list

Now, we will see python build a binary tree from the list.

In this example, we will use build() method to convert a list of values into a binary tree instead of node method. The given list contains the nodes of tree and it will build a binary tree.

Example:

from binarytree import build 
nodes =[2, 5, 7, 4, 10, None, 14] 
binary_tree = build(nodes) 
print('Binary tree from list :\n', binary_tree) 
print('\nThe list from binary tree :', binary_tree.values) 

To get the output, I have used print(binary_tree) and (binary_tree.values). You can refer to the below screenshot for the output.

Python build a binary tree from the list
Python build a binary tree from the list

Python build a random binary tree

Let’s see python build a random binary tree.

To built a random binary tree in python we will use tree() and it will generates a random binary tree and returns its root node.

Example:

from binarytree import tree 
root = tree() 
print("Binary tree of random height :") 
print(root) 

To get the output, I have used print(root). You can refer to the below screenshot for the output.

Python build a random binary tree
Python build a random binary tree

Python create a random binary tree of a given height

Now, we will see how to create a random binary tree of a given height.

To create a random binary tree of a given height we have to specify the parameter as height, and its value can be between the range 0-9.

Example:

from binarytree import tree 
my_root = tree(height = 2) 
print("Binary tree of given height :") 
print(my_root) 

To get the output, I have used print(my_root). You can refer to the below screenshot for the output.

Python create a random binary tree of a given height
Python create a random binary tree of a given height

Python create a random perfect binary tree of a given height

Now, we will see how to create a random perfect binary tree of a given height.

To create a random perfect binary tree of a given height we will use is-perfect = True then it will create a perfect binary tree with the given height.

Example:

from binarytree import tree 
my_root = tree(height = 3, is_perfect = True) 
print("The Perfect binary tree of given height :") 
print(my_root)

To get the output, I have used print(my_root). You can refer to the below screenshot for the output.

Python create a random perfect binary tree of a given height
Python create a random perfect binary tree of a given height

You may like the following Python tutorials:

In this tutorial, we have learned about Python binary tree implementation, and also we have covered these topics:

  • What is a binary tree in python?
  • How to install binary tree in python
  • Python binary tree implementation
  • Python build a binary tree from the list
  • Python build a random binary tree
  • Python create a random binary tree of a given height
  • Python create a random perfect binary tree of a given height