Title : Operation on Binary Tree.
Name : Lohade Om Manoj
Roll No : 3 Seda IT C
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct
TreeNode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a new node into the Binary Tree
struct TreeNode* insert(struct TreeNode* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
void inorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
void preorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
struct TreeNode* mirrorImage(struct TreeNode* root) {
if (root == NULL) {
return NULL;
}
struct TreeNode* temp = root->left;
root->left = mirrorImage(root->right);
root->right = mirrorImage(temp);
return root;
}
int height(struct TreeNode* root) {
if (root == NULL) {
return 0;
} else {
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
}
void displayLeafNodes(struct TreeNode* root) {
if (root == NULL) {
return;
}
if (root->left == NULL && root->right == NULL) {
printf("%d ", root->data);
}
displayLeafNodes(root->left);
displayLeafNodes(root->right);
}
int countNodes(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
int main() {
struct TreeNode* root = NULL;
int choice, data;
do {
printf("\nBinary Tree Operations:\n");
printf("1. Insert\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Mirror Image\n");
printf("6. Height\n");
printf("7. Display Leaf Nodes\n");
printf("8. Count Nodes\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
break;
case 5:
root = mirrorImage(root);
printf("Mirror Image created.\n");
break;
case 6:
printf("Height of the Binary Tree: %d\n", height(root));
break;
case 7:
printf("Leaf Nodes: ");
displayLeafNodes(root);
printf("\n");
break;
case 8:
printf("Number of Nodes: %d\n", countNodes(root));
break;
case 9:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 9);
return 0;
}