0% found this document useful (0 votes)
11 views2 pages

Binary Search Tree

The document contains a C program that implements a binary search tree (BST) with functions for inserting, deleting, and searching nodes. It includes an inorder traversal function to display the tree's contents. The main function demonstrates the creation of a BST, deletion of a node, and searching for a specific element.

Uploaded by

Subitsha S
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)
11 views2 pages

Binary Search Tree

The document contains a C program that implements a binary search tree (BST) with functions for inserting, deleting, and searching nodes. It includes an inorder traversal function to display the tree's contents. The main function demonstrates the creation of a BST, deletion of a node, and searching for a specific element.

Uploaded by

Subitsha S
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* newNode(int item) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
Node* insert(Node* node, int key) {
if (node == NULL) return newNode(key);
if (key < node->data)

node->left = insert(node->left, key);


else if (key > node->data)
node->right = insert(node->right, key);
return node;
}
Node* minValueNode(Node* node) {
Node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
Node* deleteNode(Node* root, int key) {
if (root == NULL) return root;
if (key < root->data)
root->left = deleteNode(root->left, key);
else if (key > root->data)
root->right = deleteNode(root->right, key);
else {
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;
}
Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
Node* search(Node* root, int key) {
if (root == NULL || root->data == key)
return root;
if (root->data < key)
return search(root->right, key);
return search(root->left, key);
}

void inorder(Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
int main() {
Node* root = NULL;
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);
printf("Inorder traversal of the given tree: \n");
inorder(root);
printf("\n");
printf("\nDelete 20\n");
root = deleteNode(root, 20);
printf("Inorder traversal of the modified tree: \n");
inorder(root);
printf("\n");
printf("\nSearch for 40 in the BST.\n");
Node* found = search(root, 40);
if (found != NULL) printf("Element 40 is found in the BST.\n");
else printf("Element 40 is not found in the BST.\n");
return 0;
}

You might also like