C program to implement binary search tree
C program to implement binary search tree
#include <stdio.h>
#include <stdlib.h>
struct BinaryTreeNode {
int key;
};
= (struct BinaryTreeNode*)malloc(
sizeof(struct BinaryTreeNode));
temp->key = value;
return temp;
// tree
struct BinaryTreeNode*
return root;
}
if (root->key < target) {
// tree
struct BinaryTreeNode*
if (node == NULL) {
return newNodeCreate(value);
return node;
if (root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf(" %d ", root->key);
if (root != NULL) {
inOrder(root->left);
inOrder(root->right);
if (root != NULL) {
preOrder(root->left);
preOrder(root->right);
if (root == NULL) {
return NULL;
}
return findMin(root->left);
return root;
int x)
if (root == NULL)
return NULL;
if (x > root->key) {
else {
free(root);
return NULL;
|| root->right == NULL) {
if (root->left == NULL) {
temp = root->right;
else {
temp = root->left;
free(root);
return temp;
else {
= findMin(root->right);
root->key = temp->key;
return root;
int main()
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
printf("60 found");
else {
printf("\n");
postOrder(root);
printf("\n");
preOrder(root);
printf("\n");
inOrder(root);
printf("\n");
inOrder(root);
// Free allocated memory (not done in this code, but
return 0;