C program to implement the tree traversal methods using Recursive
C program to implement the tree traversal methods using Recursive
Tree Traversal using Recursive
#include <stdio.h>
#include <stdlib.h>
// Definition of a node in a binary tree
struct Node {
int data;
struct Node *left;
struct Node *right;
}*root=NULL;
// Function to create a new 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;
}
void preOrder(struct Node* root)
{
if(root != NULL) {
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(struct Node* root)
{
if(root != NULL) {
inOrder(root->left);
printf("%d ",root->data);
inOrder(root->right);
}
}
void postOrder(struct Node* root)
{
if(root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}
int main() {
// Constructing a binary tree
// 1
// / \
// 2 3
// / \
// 4 5
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
/* Traversals
printf("Pre-order traversal: ");
preOrder(root);
printf("\n"); */
printf("In-order traversal: ");
inOrder(root);
printf("\n");
/* printf("Post-order traversal: ");
postOrder(root);
printf("\n"); */
return 0;
}
OUTPUT
Pre-order traversal: 1 2 4 5 3
In-order traversal: 4 2 5 1 3
Post-order traversal: 4 5 2 3 1
π» Run This C Program
C program to implement the tree traversal methods using Recursive
The selected C program is loaded automatically.
Edit the code, provide input through STDIN and
click Run.
The compiler is embedded using OneCompiler.
Your program runs in the embedded C environment;
Study Glance does not execute C code directly on
your web server.