0% found this document useful (0 votes)
3 views2 pages

Dsa Activity

This document contains a C program that implements a binary tree and provides functions for creating nodes and performing inorder, preorder, and postorder traversals. It includes a user interface to choose the traversal method and displays the output accordingly. The program initializes a sample binary tree and allows the user to visualize different traversal outputs.

Uploaded by

lekhanar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Dsa Activity

This document contains a C program that implements a binary tree and provides functions for creating nodes and performing inorder, preorder, and postorder traversals. It includes a user interface to choose the traversal method and displays the output accordingly. The program initializes a sample binary tree and allows the user to visualize different traversal outputs.

Uploaded by

lekhanar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

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 inorderTraversal(struct Node* root) {


if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

void preorderTraversal(struct Node* root) {


if (root == NULL) return;
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

void postorderTraversal(struct Node* root) {


if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}

void visualizeTraversal(struct Node* root) {


int choice;
do {
printf("\nChoose a traversal method:\n");
printf("1. Inorder\n");
printf("2. Preorder\n");
printf("3. Postorder\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("\nInorder Traversal Output: ");
inorderTraversal(root);
printf("\n");
break;
case 2:
printf("\nPreorder Traversal Output: ");
preorderTraversal(root);
printf("\n");
break;
case 3:
printf("\nPostorder Traversal Output: ");
postorderTraversal(root);
printf("\n");
break;
case 4:
printf("\nExiting the program. Goodbye!\n");
break;
default:
printf("\nInvalid choice! Please try again.\n");
}
} while (choice != 4);
}

int main() {
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

printf("Binary Tree Traversal Visualizer\n");


visualizeTraversal(root);

return 0;

You might also like