Binary Tree
Binary Tree
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
return root;
}
displayLeafNodes(root->left);
displayLeafNodes(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;
}