// Including necessary header files
#include <stdio.h>
#include <stdlib.h>
// Structure for a binary tree node
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
// Function to create a new node
struct TreeNode* createNode(int value) {
// Allocate memory for a new TreeNode
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct
TreeNode));
// Check if memory allocation was successful
if (newNode != NULL) {
// Initialize node data
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
}
// Return the created node
return newNode;
}
// Function to insert a node into the binary tree
struct TreeNode* insertNode(struct TreeNode* root, int value) {
// If the tree is empty, create a new node
if (root == NULL) {
return createNode(value);
}
// If the value is less than the root's data, insert into the left subtree
if (value < root->data) {
root->left = insertNode(root->left, value);
}
// If the value is greater than the root's data, insert into the right
subtree
else if (value > root->data) {
root->right = insertNode(root->right, value);
}
// Return the modified root
return root;
}
// Function to perform in-order traversal of the binary tree
void inOrderTraversal(struct TreeNode* root) {
// Traverse the tree in-order: left subtree, root, right subtree
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}
// Function to free the memory allocated for the binary tree
void freeTree(struct TreeNode* root) {
// Recursively free memory for the entire tree
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
int main() {
// Initialize the root of the binary tree
struct TreeNode* root = NULL;
int nodeValue;
char choice;
// User input loop to insert nodes into the binary tree
do {
// Prompt user for a value to insert into the binary tree
printf("Input a value to insert into the binary tree: ");
scanf("%d", &nodeValue);
// Insert the value into the binary tree
root = insertNode(root, nodeValue);
// Ask user if they want to insert another node
printf("Want to insert another node? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
// Display the in-order traversal of the binary tree
printf("\nIn-order Traversal of the Binary Tree: ");
inOrderTraversal(root);
printf("\n");
// Free allocated memory for the binary tree
freeTree(root);
// Return 0 to indicate successful execution
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a binary tree node
struct Node
{
int key;
struct Node *left, *right;
};
// Function to create a new node with a given value
struct Node *newNodeCreate(int value)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->key = value;
temp->left = temp->right = NULL;
return temp;
}
// Function to search for a node with a specific key in the tree
struct Node *searchNode(struct Node *root, int target)
{
if (root == NULL || root->key == target)
return root;
if (root->key < target)
return searchNode(root->right, target);
return searchNode(root->left, target);
}
// Function to insert a node with a specific value in the tree
struct Node *insertNode(struct Node *node, int value)
{
if (node == NULL)
return newNodeCreate(value);
if (value < node->key)
node->left = insertNode(node->left, value);
else if (value > node->key)
node->right = insertNode(node->right, value);
return node;
}
// Function to perform post-order traversal
void postOrder(struct Node *root)
{
if (root != NULL)
{
postOrder(root->left);
postOrder(root->right);
printf(" %d ", root->key);
}
}
// Function to perform in-order traversal
void inOrder(struct Node *root)
{
if (root != NULL)
{
inOrder(root->left);
printf(" %d ", root->key);
inOrder(root->right);
}
}
// Function to perform pre-order traversal
void preOrder(struct Node *root)
{
if (root != NULL)
{
printf(" %d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
// Function to find the minimum value
struct Node *findMin(struct Node *root)
{
if (root == NULL)
return NULL;
else if (root->left != NULL)
return findMin(root->left);
return root;
}
// Function to delete a node from the tree
struct Node *delete (struct Node *root, int x)
{
if (root == NULL)
return NULL;
if (x > root->key)
root->right = delete (root->right, x);
else if (x < root->key)
root->left = delete (root->left, x);
else
{
if (root->left == NULL && root->right == NULL)
{
free(root);
return NULL;
}
else if (root->left == NULL || root->right == NULL)
{
struct Node *temp;
if (root->left == NULL)
{
temp = root->right;
}
else
{
temp = root->left;
}
free(root);
return temp;
}
else
{
struct Node *temp = findMin(root->right);
root->key = temp->key;
root->right = delete (root->right, temp->key);
}
}
return root;
}
int main()
{
// Initialize the root node
struct Node *root = NULL;
// Insert nodes into the binary search tree
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
// Search for a node with key 60
if (searchNode(root, 60) != NULL)
printf("60 found");
else
printf("60 not found");
printf("\n");
// Perform post-order traversal
postOrder(root);
printf("\n");
// Perform pre-order traversal
preOrder(root);
printf("\n");
// Perform in-order traversal
inOrder(root);
printf("\n");
// Perform delete the node (70)
struct Node *temp = delete (root, 70);
printf("After Delete: \n");
inOrder(root);
return 0;
}