Binary Tree Traversal in Data Structure Program

Binary Tree Traversal in Data Structure Program. C algorithm for tree traversal using the In order, Pre order, Post order method. These methods in tree traversal using the pre order, post order, and in order used to execute the tree traversal in the c programming language.

Tree Traversal in C Program

Tree traversal is a type of graph traversal in computer science that refers to the process of visiting each node in a tree data structure exactly once. The sequence in which the nodes are visited is used to classify these traversals. 

In Order Traversal in C

Traversal in reverse sequence. In an in order traversal, the left child is visited first, followed by the node, and finally the right child. This traversal is used by the binary search tree to print all nodes in ascending order of value.

Preorder traversal in C

Process all nodes of a tree by starting with the root and working your way down to all subtrees. Prefix traversal is another name for it.

Post Order Traversal in C

The term “tree traversal” refers to visiting all of a tree’s nodes exactly once. Visiting a node entails doing something with it. It may be something as simple as printing the node. One of the many methods for traversing a tree is post-order traversal.

Malloc Program in C

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a void pointer that can be cast into any other pointer type. The reserved space is returned by the malloc() function. The storage space to which the return value points is well suited for storing any type of object. If there is insufficient storage or if size is specified as zero, the return value is NULL.

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* 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);
void printpost(struct node* node)
if (node == NULL)
printf("%d ", node->data);
void printinorder(struct node* node)
if (node == NULL)
printf("%d ", node->data);
void printpre(struct node* node)
if (node == NULL)
printf("%d ", node->data);
int main()
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("\npre traversal of binary tree is \n");
printf("\ninorder traversal of binary tree is \n");
printf("\npost traversal of binary tree is \n");
return 0;



pre traversal of binary tree is
1 2 4 5 3
inorder traversal of binary tree is
4 2 5 1 3
post traversal of binary tree is
4 5 2 3 1