0% found this document useful (0 votes)
8 views

Binary Tree C Code Implementation

Uploaded by

sharayud2806
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Binary Tree C Code Implementation

Uploaded by

sharayud2806
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Binary Tree

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.

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

4.
4. Degenerate or Pathological Tree

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

5. 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
Binary Tree Representation

Each node in a Binary Tree has three parts:

• Data

• Pointer to the left child

• Pointer to the right child

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 Binary Tree Traversal in C

Inorder traversal is a DFS traversal technique where we try to traverse as deep


as possible in the tree from the current node. In the inorder traversal, we first
visit all the left subtree, then visit the current node and at last, we visit the right
subtree.

Inorder Traversal

1. Visit the left subtree.

2. Visit the root node.

3. Visit the right subtree.

Consider the following example,


Here’s the step-by-step explanation of the inorder traversal:

1. Start at the root node (1).

2. Visit the left subtree of the root (2).

3. Visit the left subtree of 2(4) then 4 does not have any children so print it.

4. Backtrack to the node (2) and print it.

5. Move to the right subtree of the node 2 (5). Print it.

6. Backtrack to the root node (1).

7. Move to the right subtree of the node 1 (3).

8. Visit the left subtree of 3 and there is no nodes.

9. Visit the node (3) and print it.

The in-order traversal of the tree is: 4, 2, 5, 1, 3

// C program to show how to implement the binary tree

// traversal

#include <stdio.h>

#include <stdlib.h>

// node of the tree

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// utility function to create a node


struct Node* createNode(int data)

struct Node* newNode

= (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Function to perform inorder traversal

void inorderTraversal(struct Node* root)

// cheking if the current node is NULL

if (!root)

return;

// traversing left subtree

inorderTraversal(root->left);

// traversing current node

printf("%d ", root->data);

// traversing right subtree

inorderTraversal(root->right);

int main()
{

// Example tree creation

struct Node* root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

printf("Inorder Traversal: ");

inorderTraversal(root);

return 0;

Output
Inorder Traversal: 4 2 5 1 3

Postorder Tree Traversal in Binary Tree in


C
Postorder Tree Traversal in Binary Tree in C

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:

Workflow of Postorder Traversal


Algorithm for Postorder Traversal in C

Following is the algorithm for the postorder traversal of the binary tree in C:

1. Start

2. Traverse left subtree using recursion.

3. Traverse right subtree using recursion

4. Visit the root node

5. Repeat steps 3-5 until root node != NULL

6. Stop

C Program for Postorder Traversal in a Binary Tree

The following program demonstrates how we can implement the postorder traversal in a
binary tree in C:

// C Program for Postorder Traversal in a Binary Tree

#include <stdio.h>

#include <stdlib.h>
// __________ CODE FOR BINARY TREE IMPLEMENTATION __________

// Define the structure for a binary tree node

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new node in a Binary Tree

struct Node* createNode(int data)

// Allocate memory for the new node

struct Node* newNode

= (struct Node*)malloc(sizeof(struct Node));

// Initialize node data and children pointers

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// __________ CODE FOR POSTORDER TRAVERSAL __________


// Function to perform postorder traversal

void postorderTraversal(struct Node* root)

if (root != NULL) {

postorderTraversal(root->left);

postorderTraversal(root->right);

printf("%d ", root->data);

// driver code

int main()

struct Node* root = NULL;

// Create the binary tree

/* 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);

// Perform postorder traversal

printf("Postorder traversal of the binary tree is:\n");

postorderTraversal(root);

printf("\n");

return 0;

Output
Postorder traversal of the binary tree is:
4526731

Preorder Traversal of Binary Tree


In pre-order traversal, the node is visited first, followed by its left child and then its right
child. This can be visualized as Root – Left – Right.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;


struct Node* right;

};

void preOrderTraversal(struct Node* root) {

if (root == NULL) return;

// Visit the root node

printf("%d ", root->data);

// Traverse the left subtree

preOrderTraversal(root->left);

// Traverse the right subtree

preOrderTraversal(root->right);

struct Node* newNode(int data) {

struct Node* node = (struct Node*)malloc(sizeof(struct Node));

node->data = data;

node->left = NULL;

node->right = NULL;

return node;

int main() {
// Create the following binary tree

// 1

// /\

// 2 3

// / \

// 4 5

struct Node* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("Pre-Order Traversal: ");

preOrderTraversal(root);

printf("\n");

return 0;

Output
Pre-Order Traversal: 1 2 4 5 3

You might also like