You are on page 1of 15

FLOW CHART:

EXP NO:

DATE:

AIM:
To write a c program to perform implementation of binary tree operation.

OBJECTIVE:
The objective of implementing a binary tree in a C program typically involves creating a data
structure that allows for efficient insertion, deletion, and retrieval of elements. It should enable
traversal operations such as inorder, preorder, and postorder traversal. Additionally, the
implementation should provide functionality for searching, modifying, and displaying the
elements of the binary tree.

ALGORITHM:
1. Start the program.

2. Create a structure definition for a binary tree node (struct Node) with data, left, and right
fields.

3. Create a function createNode(data) that allocates memory for a new node, initializes it
with data, and sets left and right pointers to NULL. Return the new node.

4. Create a function insert(root, data) to insert a value into the binary tree:

• If the root is NULL, create a new node with the data and return it.
• If data is less than root's data, call insert recursively on the left subtree.
• If data is greater than root's data, call insert recursively on the right subtree.  Return the
root node.
5. Create a function search(root, data) to search for a value in the binary tree:

• If the root is NULL or has the target data, return the root.
• If data is less than root's data, recursively search in the left subtree.
• If data is greater than root's data, recursively search in the right subtree.
6. Create a function findMin(root) to find the minimum value node in a binary tree:

• start from the root and traverse left until the left child is NULL. Return the current node.
7. Create a function findMax(root) to find the maximum value node in a binary tree:
• start from the root and traverse right until the right child is NULL. Return the current
node.

8. Create a function deleteNode(root, data) to delete a value from the binary tree:

• If the root is NULL, return it.


If data is less than root's data, recursively delete from the left subtree

 If data is greater than root's data, recursively delete from the right subtree.  If the data
matches, handle three cases:
a. If the root has no left child, replace the root with its right child and free the root.

b. If the root has no right child, replace the root with its left child and free the root.

c. If the root has both left and right children, find the minimum value node in the right subtree,
replace the root's data with this minimum value, and recursively delete the minimum value
node from the right subtree.

5. Return the root node.


9. Create three tree traversal functions: preOrderTraversal(root), postOrderTraversal(root), and
inOrderTraversal(root). These functions perform pre-order, post-order, and in-order traversals,
respectively, and print the nodes.
10. In the main function:

• Initialize root to NULL and declare choice and data variables.


• Enter a while loop with a menu for various binary tree operations:
1. Insert: Prompt for data, call insert function, and update the root.

2. Delete: Prompt for data, call deleteNode function, and update the root.

3. Search: Prompt for data, call the search function, and display the result.

4. Minimum Value: Call findMin function and display the minimum value.

5. Maximum Value: Call findMax function and display the maximum value.

6. Pre-order Traversal: Call preOrderTraversal function and print the result.

7. Post-order Traversal: Call postOrderTraversal function and print the result.

8. In-order Traversal: Call inOrderTraversal function and print the result.

9. Quit: Exit the program.

10. Handle invalid choices.

11. End the program.


PROGRAM:
#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;
}
struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
struct Node* search(struct Node* root, int data) {
if (root == NULL || root->data == data) {
return root;
}
if (data < root->data) {
return search(root->left, data);
}
return search(root->right, data);
}
struct Node* findMin(struct Node* root) {
while (root->left != NULL) {
root = root->left;
}
return root;
}
struct Node* findMax(struct Node* root) {
while (root->right != NULL) {
root = root->right;
}
return root;
}
struct Node* deleteNode(struct Node* root, int data) {
if (root == NULL) {
return root;
}
if (data < root->data) {
root->left = deleteNode(root->left, data);
} else if (data > root->data) {
root->right = deleteNode(root->right, data);
} else {
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}
struct Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
void preOrderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}
void postOrderTraversal(struct Node* root) {
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->data);
}
}
void inOrderTraversal(struct Node* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}
int main() {
struct Node* root = NULL;
int choice, data;

while (1) {
printf("\nBinary Tree Operations:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Minimum Value\n");
printf("5. Maximum Value\n");
printf("6. Pre-order Traversal\n");
printf("7. Post-order Traversal\n");
printf("8. In-order Traversal\n");
printf("9. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 3:
printf("Enter value to search: ");
scanf("%d", &data);
if (search(root, data) != NULL) {
printf("Value found in the tree.\n");
} else {
printf("Value not found in the tree.\n");
}
break;
case 4:
if (root == NULL) {
printf("Tree is empty.\n");
} else {
struct Node* minNode = findMin(root);
printf("Minimum Value: %d\n", minNode->data);
}
break;
case 5:
if (root == NULL) {
printf("Tree is empty.\n");
} else {
struct Node* maxNode = findMax(root);
printf("Maximum Value: %d\n", maxNode->data);
}
break;
case 6:
printf("Pre-order Traversal: ");
preOrderTraversal(root);
printf("\n");
break;
case 7:
printf("Post-order Traversal: ");
postOrderTraversal(root);
printf("\n");
break;
case 8:
printf("In-order Traversal: ");
inOrderTraversal(root);
printf("\n");
break;
case 9:
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}

You might also like