Binary Tree C Code Implementation
Binary Tree C Code Implementation
A binary tree is a tree data structure in which each parent node can have at most two children.
Each node of a binary tree consists of three items:
• data item
• address of left child
• address of right child
•
Types of Binary Tree
1. 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.
2. 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.
A complete binary tree is just like a full binary tree, but with two major
differences
4.
4. Degenerate or Pathological Tree
A degenerate or pathological tree is the tree having a single child either left or right.
• Data
struct node
{
int data;
struct node *left;
struct node *right;
};
To create a binary tree, we have to first create a node having a data, pointer to
left child and pointer to right child using the below structure format:
Inorder Tree Traversal in Binary Tree in C
A binary Tree is a hierarchical data structure in which each node has at most
two children and it can referred to as the left child and right child. Due to being
a non-linear data structure, different traversal techniques are possible for it.
Inorder tree traversal is one of the techniques used to visit each node of the
binary tree. In this article, we will learn how to implement the in-order traversal
technique for binary tree traversal in C. We will also discuss its time and space
complexities.
Inorder Traversal
3. Visit the left subtree of 2(4) then 4 does not have any children so print it.
// traversal
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (!root)
return;
inorderTraversal(root->left);
inorderTraversal(root->right);
int main()
{
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
inorderTraversal(root);
return 0;
Output
Inorder Traversal: 4 2 5 1 3
The postorder traversal is a way of visiting all the nodes of a binary tree in a specific order. It
involves visiting the left subtree first, followed by the right subtree, and finally the root node.
Consider the below diagram to understand the workflow of postorder traversal:
Following is the algorithm for the postorder traversal of the binary tree in C:
1. Start
6. Stop
The following program demonstrates how we can implement the postorder traversal in a
binary tree in C:
#include <stdio.h>
#include <stdlib.h>
// __________ CODE FOR BINARY TREE IMPLEMENTATION __________
struct Node {
int data;
};
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
// driver code
int main()
/* 1
/ \
2 3
/\ /\
4 56 7
*/
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
postorderTraversal(root);
printf("\n");
return 0;
Output
Postorder traversal of the binary tree is:
4526731
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
preOrderTraversal(root->left);
preOrderTraversal(root->right);
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
int main() {
// Create the following binary tree
// 1
// /\
// 2 3
// / \
// 4 5
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
preOrderTraversal(root);
printf("\n");
return 0;
Output
Pre-Order Traversal: 1 2 4 5 3