C program for binary search tree (BST).

Posted by Mangesh on August 17, 2018

program in c for implementing binary search tree, and inorder traversal, preorder traversal, and postorder traversal in bst.

Description :

A binary search tree is a binary tree with values at the nodes arranged such that the values at the nodes in the right subtree of a node is at least the value of the node and the values at nodes in the left subtree of the node is at most the value of the node.

Program :

#include<stdio.h>
#include<conio.h>
typedef struct node
{
  struct node *left;
  int data;
  struct node *right;
}node;

node *CreateBST(node *, int);
void Inorder(node *root)
{
  if( root != NULL)
  {
    Inorder(root->left);
    printf(" %d ",root->data);
    Inorder(root->right);
  }
}

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

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

void main()
{
  node *root=NULL;
  int opn,data,n,i;

  clrscr();
  root=NULL;
  printf("\nEnter total number of node in BST : ");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    printf("\nEnter the value of %d Node : ",i+1);
    scanf("%d",&data);
    root=CreateBST(root,data);
  }
  printf("\nBST created!!!\n",n);

  printf("\n BST Traversal in inorder : ");
  Inorder(root);
  printf("\n BST Traversal in preorder : ");
  Preorder(root);
  printf("\n BST Traversal in postorder : ");
  Postorder(root);
  getch();
}

node *CreateBST(node *root, int data)
{
  if(root == NULL)
  {
    root=(node *)malloc(sizeof(node));
    root->left= root->right = NULL;
    root->data=data;
    return root;
  }
  else if( data < root->data )
    root->left=CreateBST(root->left,data);
  else if( data > root->data )
    root->right=CreateBST(root->right,data);
  else
    printf(" Duplicate Node !!!");

  return(root);
  
}

Output :

c program for bst executed and tested in turbo c++ 3.2

Written with from Mangesh.

Related Post
1 C program for binary search tree (BST).
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.