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;

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 {
  clrscr();
  printf("n ### Binary Search Tree Operations ### nn");
  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-Exitn");
  printf("n       Your option ? ");
  scanf("%d", &opn);
  switch (opn) {
  case 1:
   root = NULL;
   printf("nnBST 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);
   break;
  case 2:
   printf("n BST Traversal in INORDER n");
   Inorder(root);
   break;
  case 3:
   printf("n BST Traversal in PREORDER n");
   Preorder(root);
   break;
  case 4:
   printf("n BST Traversal in POSTORDER n");
   Postorder(root);
   break;
  case 5:
   printf("nn Terminating nn");
   break;
  default:
   printf("nnInvalid Option !!! Try Again !! nn");
   break;
  }
  printf("nnnn  Press a Key to Continue . . . ");
  getch();
 } 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);
  else
   printf(" Duplicate Element !! Not Allowed !!!");

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

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

void Postorder(TNODE *root) {
 if (root != NULL) {
  Postorder(root->left);
  Postorder(root->right);
  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 Follow @c_program

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!

(c) www.c-program-example.com

4 comments on “C Program for Binary Search Tree Creation and Traversals

  • INSERTION SORT PROGRAMME>>>

    Q-:WRITE A PROGRAMME TO DO INSERTION SORT?

    ANSWER-:

    #include
    #include
    int a[10],b;
    int i=0,count;
    void order()
    {
    for(i=0;i>ch;
    while(ch=='y')
    {
    cout<<"enter the element";
    cin>>a[i];
    count=i++;
    cout<<"Press Y to enter the element in the arrayn";
    cin>>ch;
    }
    order();
    for(i=0;i<count+1;i++)
    {
    cout<<a[i]<<" ";
    }
    getch();
    }

    Reply
    • Sandeepa Nadahalli says:

      Hi!
      Sure. As you may be aware, I’m doing some site-wide changes. I will take up program request soon after that’s done.

      Encryption programs are on the list.

      Thanks,
      SN

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>