0% found this document useful (0 votes)
4 views

binary search in c

The document contains C code for implementing a binary search tree (BST) with functionalities to create nodes, insert, search, and delete nodes, as well as perform an inorder traversal. It includes functions for creating a node, inserting data, searching for a value, finding the minimum value, and deleting a node while handling different cases. The main function demonstrates the usage of these operations by inserting elements, searching for a specific value, and deleting a node from the tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

binary search in c

The document contains C code for implementing a binary search tree (BST) with functionalities to create nodes, insert, search, and delete nodes, as well as perform an inorder traversal. It includes functions for creating a node, inserting data, searching for a value, finding the minimum value, and deleting a node while handling different cases. The main function demonstrates the usage of these operations by inserting elements, searching for a specific value, and deleting a node from the tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>

typedef struct node {


int data;
struct node *left;
struct node *right;
} node;

node *create_node(int data) {


node *new_node = (node*) malloc(sizeof(node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}

node *insert(node *root, int data) {


if (root == NULL) {
return create_node(data);
} else if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}

node *search(node *root, int data) {


if (root == NULL || root->data == data) {
return root;
} else if (data < root->data) {
return search(root->left, data);
} else {
return search(root->right, data);
}
}

node *find_minimum(node *root) {


node *current = root;
while (current->left != NULL) {
current = current->left;
}
return current;
}

node *delete(node *root, int data) {


if (root == NULL) {
return root;
} else if (data < root->data) {
root->left = delete(root->left, data);
} else if (data > root->data) {
root->right = delete(root->right, data);
} else {
// Case 1: No child or only one child
if (root->left == NULL) {
node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
node *temp = root->left;
free(root);
return temp;
}
// Case 2: Two children
node *temp = find_minimum(root->right);
root->data = temp->data;
root->right = delete(root->right, temp->data);
}
return root;
}

void inorder_traversal(node *root) {


if (root != NULL) {
inorder_traversal(root->left);
printf("%d ", root->data);
inorder_traversal(root->right);
}
}

int main() {
node *root = NULL;

// Insert elements into the tree


root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);

// Print the inorder traversal of the tree


printf("Inorder traversal of the tree: ");
inorder_traversal(root);
printf("\n");

// Search for an element in the tree


int key = 60;
node *result = search(root, key);
if (result == NULL) {
printf("%d not found in the tree\n", key);
} else {
printf("%d found in the tree\n", key);
}

// Delete an element from the tree


int data = 40;
root = delete(root, data);
printf("Inorder traversal after deleting %d: ", data);
inorder_traversal(root);
printf("\n");

You might also like