Skip to content
Related Articles

Related Articles

Improve Article

Binary Tree using dstructure library in Python

  • Last Updated : 09 May, 2021

The dstructure package is a Python library for dealing with data structure and algorithm. In this article, we will discuss how we can implement a binary tree and perform various operations using the dstructure library in Python.

Installation

To install dstructure open the terminal and write the below command:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

pip install dstructure

Binary Tree

A tree whose elements have at most two children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. However, dstructure library helps to directly implement a binary tree.



A Binary Tree node contains the following parts.

  1. Data
  2. Pointer to the left child
  3. Pointer to the right child

Methods Available

Let’s see different methods available to handle binary tree. First, we will make the binary tree and then apply all the methods.

In order to create a binary tree, we first import the dstructure module, create a BTree class object to initialize an empty binary tree, and use the insert() method to insert nodes into the tree. Below are the various methods used to create and perform various operations on a binary tree.

  • insert(int_value): This function takes int value and adds that value to the tree.

Python3




# import module
from dstructure.BTree import BTree
  
# create class
obj=BTree(23)
  
# insert nodes
obj.insert(10
obj.insert(20
obj.insert(30
obj.insert(40
  
# display object
print(obj)

Output:

<dstructure.BTree.BTree object at 0x000001F02815B648>
  • inorder(obj): This function takes an object as an input and returns the inorder traversal of the tree.

Python3




# return inorder in list
inorder = obj.inorder(obj)  
print(inorder)

Output:



[10, 20, 23, 30, 40]
  • preorder(obj): This function takes an object as an input and returns the preorder traversal of the tree.

Python3




# return preorder in list
preorder = obj.preorder(obj) 
print(preorder)

Output:

[23, 10, 20, 30, 40]
  • postorder(obj): This function takes an object as an input and returns the postorder traversal of the tree.

Python3




# return postorder in list
postorder = obj.postorder(obj)  
print(postorder)

Output:

[20, 10, 40, 30, 23]
  • get_bfs() : This function return the breadth-first search traversal of the tree.

Python3




# return Breadth First Search of tree in list
bfs = obj.get_bfs()  
print(bfs)

Output:

[23, 30, 10, 40, 20]
  • get_dfs(): This function returns the depth-first search traversal of the tree.

Python3




# return Depth First Search of tree in list
dfs = obj.get_dfs()  
print(dfs)

Output:

[23, 10, 20, 30, 40]
  • left_view(): This function returns the left view of the tree.

Python3




# return Left View Of Tree in list
left = obj.left_view()
print(left)

Output:

[23, 10, 20]
  • right_view() : This function return the right view of the tree.

Python3




# return Right View Of Tree in list
right = obj.right_view()  
print(right)

Output:

[23, 30, 40]
  • total_nodes() : This function returns the number of nodes in the tree.

Python3




# return the number of nodes present in the Tree
count = obj.total_nodes() 
print(count)

Output:

5



My Personal Notes arrow_drop_up
Recommended Articles
Page :