Oct 11, 2011

C Program for Binary Search Tree Creation and Traversals

C Program for Binary Search Tree Creation and Traversals. Binary Search Tree is a Tree which has the following properties, 1.The left sub tree of a node contains smaller nodes than a root node. 2.The right sub tree of a node contains greater nodes than a root node. 3.Both the left and right sub trees must also be binary search trees. There are three types of tree traversals, Preorder, Postorder, and Inorder. Read more about C Programming Language .

* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*                                  Happy Coding

#include <stdlib.h>
typedef struct tnode {
 int data;
 struct tnode *right, *left;

TNODE *CreateBST(TNODE *, int);
void Inorder(TNODE *);
void Preorder(TNODE *);
void Postorder(TNODE *);

main() {
 TNODE *root = NULL; /* Main Program */
 int opn, elem, n, i;
 do {
  printf("\n ### Binary Search Tree Operations ### \n\n");
  printf("\n Press 1-Creation of BST");
  printf("\n       2-Traverse in Inorder");
  printf("\n       3-Traverse in Preorder");
  printf("\n       4-Traverse in Postorder");
  printf("\n       5-Exit\n");
  printf("\n       Your option ? ");
  scanf("%d", &opn);
  switch (opn) {
  case 1:
   root = NULL;
   printf("\n\nBST for How Many Nodes ?");
   scanf("%d", &n);
   for (i = 1; i <= n; i++) {
    printf("\nRead the Data for Node %d ?", i);
    scanf("%d", &elem);
    root = CreateBST(root, elem);
   printf("\nBST with %d nodes is ready to Use!!\n", n);
  case 2:
   printf("\n BST Traversal in INORDER \n");
  case 3:
   printf("\n BST Traversal in PREORDER \n");
  case 4:
   printf("\n BST Traversal in POSTORDER \n");
  case 5:
   printf("\n\n Terminating \n\n");
   printf("\n\nInvalid Option !!! Try Again !! \n\n");
  printf("\n\n\n\n  Press a Key to Continue . . . ");
 } while (opn != 5);

TNODE *CreateBST(TNODE *root, int elem) {
 if (root == NULL) {
  root = (TNODE *) malloc(sizeof(TNODE));
  root->left = root->right = NULL;
  root->data = elem;
  return root;
 } else {
  if (elem < root->data)
   root->left = CreateBST(root->left, elem);
  else if (elem > root->data)
   root->right = CreateBST(root->right, elem);
   printf(" Duplicate Element !! Not Allowed !!!");

  return (root);
void Inorder(TNODE *root) {
 if (root != NULL) {
  printf(" %d ", root->data);

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

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

Read more Similar C Programs
Data Structures
Breadth First Search(BFS)
Learn C Programming
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!