// construct binary tree with odd and even data counters #include #include int e,o,n; // Define a TreeNode structure and use a typedef for convenience typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* createNode(int data) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } TreeNode* insertNode(TreeNode* root, int data) { if (root == NULL) { return createNode(data); } if (data < root->data) { root->left = insertNode(root->left, data); } else if (data > root->data) { root->right = insertNode(root->right, data); } return root; } TreeNode* findMinNode(TreeNode* root) { while (root->left != NULL) { root = root->left; } return root; } TreeNode* deleteNode(TreeNode* root, int key) { if (root == NULL) { return NULL; } if (key < root->data) { root->left = deleteNode(root->left, key); } else if (key > root->data) { root->right = deleteNode(root->right, key); } else // root is found { if (root->left == NULL) { TreeNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { TreeNode* temp = root->left; free(root); return temp; } TreeNode* temp = findMinNode(root->right); root->data = temp->data; root->right = deleteNode(root->right, temp->data); } return root; } void inorderTraversal(TreeNode* root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); if(root->data >= 0){ if(root->data % 2 == 0){e++;} if(root->data % 2 != 0){o++;} }else{ n++; } } } void dispOddEven(){ printf("\nThe number of even nodes is %d.\n",e); printf("The number of odd nodes is %d.\n",o); if(n!=0){ printf("\nERROR: Negative Number encountered. Not considered in either odd or even.\n"); } } int main() { TreeNode* root = NULL; int choice, value; printf("Elements are added to the tree in an inorder sequence."); while (1) { printf("\nMenu:\n"); printf("1. Insert a value\n"); printf("2. Delete a value\n"); printf("3. Odd and Event Count\n"); printf("4. Display the Tree (inorder traversal)\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter a value to insert: "); scanf("%d", &value); root = insertNode(root, value); break; case 2: printf("Enter a value to delete: "); scanf("%d", &value); root = deleteNode(root, value); break; case 3: printf("\nChecking for odd and even characters...\n"); e = 0; o = 0; n=0; printf("In the given tree:\n"); inorderTraversal(root); dispOddEven(); break; case 4: printf("Inorder traversal: "); inorderTraversal(root); printf("\n"); break; case 5: free(root); // Release memory exit(0); default: printf("Invalid choice. Please try again.\n"); } } return 0; }