Python Data Structure and Algorithm Tutorial – Binary Tree

Hits: 0

Binary Tree

 

In this tutorial, you will learn about binary tree and its different types. Also, you will find working examples of binary tree in Python.

A binary tree is a tree data structure in which each parent node can have at most two children.

For example: In the image below, each element has at most two children.

Binary Tree
Binary Tree

Types of Binary Tree

Full Binary Tree

A full Binary tree is a special type of binary tree in which every parent node/internal node has either two or no children.

Full binary tree
Full Binary Tree

 

Perfect Binary Tree

A perfect binary tree is a type of binary tree in which every internal node has exactly two child nodes and all the leaf nodes are at the same level.

Perfect binary tree
Perfect Binary Tree

 

Complete Binary Tree

A complete binary tree is just like a full binary tree, but with two major differences

  1. Every level must be completely filled
  2. All the leaf elements must lean towards the left.
  3. The last leaf element might not have a right sibling i.e. a complete binary tree doesn’t have to be a full binary tree.
Complete Binary Tree
Complete Binary Tree

Degenerate or Pathological Tree

A degenerate or pathological tree is the tree having a single child either left or right.

Degenerate Binary Tree
Degenerate Binary Tree

Skewed Binary Tree

A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated by the left nodes or the right nodes. Thus, there are two types of skewed binary tree: left-skewed binary tree and right-skewed binary tree.

Skewed Binary Tree
Skewed Binary Tree

Balanced Binary Tree

It is a type of binary tree in which the difference between the left and the right subtree for each node is either 0 or 1.

Balanced Binary Tree
Balanced Binary Tree

 


Binary Tree Representation

A node of a binary tree is represented by a structure containing a data part and two pointers to other structures of the same type.

struct node
{
 int data;
 struct node *left;
 struct node *right;
};
Binary tree
Binary Tree Representation

Python Examples

/* Binary Tree in Python */

class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

    /* Traverse preorder */
    def traversePreOrder(self):
        print(self.val, end=' ')
        if self.left:
            self.left.traversePreOrder()
        if self.right:
            self.right.traversePreOrder()

    /* Traverse inorder */
    def traverseInOrder(self):
        if self.left:
            self.left.traverseInOrder()
        print(self.val, end=' ')
        if self.right:
            self.right.traverseInOrder()

    /* Traverse postorder */
    def traversePostOrder(self):
        if self.left:
            self.left.traversePostOrder()
        if self.right:
            self.right.traversePostOrder()
        print(self.val, end=' ')


root = Node(1)

root.left = Node(2)
root.right = Node(3)

root.left.left = Node(4)

print("Pre order Traversal: ", end="")
root.traversePreOrder()
print("nIn order Traversal: ", end="")
root.traverseInOrder()
print("nPost order Traversal: ", end="")
root.traversePostOrder()

Binary Tree Applications

  • For easy and quick access to data
  • In router algorithms
  • To implement heap data structure
  • Syntax tree

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars