You are on page 1of 2

16.

write a program to implement Binary search tree(BST)

#include <stdio.h>
#include <stdlib.h>

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

struct node* create_node(int value)


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

struct node* insert(struct node* root, int value)


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

void inorder_traversal(struct node* root)


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

int main()
{
struct node* root = NULL;
root = insert(root, 5);
insert(root, 3);
insert(root, 7);
insert(root, 1);
insert(root, 9);
printf("Inorder traversal of BST: ");
inorder_traversal(root);
printf("\n");
return 0;
}

You might also like