Algorithm in C – Binary Tree

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 C.

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

C Examples

// Tree traversal in C

#include <stdio.h>
#include <stdlib.h>

struct node {
  int item;
  struct node* left;
  struct node* right;

// Inorder traversal
void inorderTraversal(struct node* root){
  if (root == NULL) return;
  printf("%d ->", root->item);

// Preorder traversal
void preorderTraversal(struct node* root){
  if (root == NULL) return;
  printf("%d ->", root->item);

// Postorder traversal
void postorderTraversal(struct node* root){
  if (root == NULL) return;
  printf("%d ->", root->item);

// Create a new Node
struct node* createNode(value){
  struct node* newNode = malloc(sizeof(struct node));
  newNode->item = value;
  newNode->left = NULL;
  newNode->right = NULL;

  return newNode;

// Insert on the left of the node
struct node* insertLeft(struct node* root, int value){
  root->left = createNode(value);
  return root->left;

// Insert on the right of the node
struct node* insertRight(struct node* root, int value){
  root->right = createNode(value);
  return root->right;

int main(){
  struct node* root = createNode(1);
  insertLeft(root, 2);
  insertRight(root, 3);
  insertLeft(root->left, 4);

  printf("Inorder traversal n");

  printf("nPreorder traversal n");

  printf("nPostorder traversal n");

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