UrbanPro
true

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Binary Search Tree in C

B
Balaji K
16/11/2020 0 0

#include <stdio.h>

#include <stdlib.h>

 

struct node

{

   int element;

   struct node *left;

   struct node *right;

};

struct node *root = NULL;

 

 

struct node *insert(struct node *root,struct node *newnode)

{

  if(root == NULL)

  {

    root = newnode;

 

  }

  else

  {

    if(newnode->element < root->element)

    {

        if (root->left == NULL)

            root->left = newnode;

        else

           insert(root->left,newnode);

    }

    else if(newnode->element > root->element)

    {

        if(root->right == NULL)

            root->right = newnode;

        else

         insert(root->right,newnode);

    }

    else

        printf("\n\t Element already exists \n\n");

 

  }

return root;

 

}

 

void find(int val,struct node *root)

{

 

 

   if(root == NULL)

    printf("\n\n\t\tElement not found");

   else

   {

     if(val<root->element)

        find(val,root->left);

     else if(val>root->element)

        find(val,root->right);

     else

        printf("\n\n\tElement Found");

    }

}

 

 

void preorder(struct node *root)

{

  if(root != NULL)

  {

   printf("%d --> ",root->element);

   preorder(root->left);

   preorder(root->right);

  }

}

 

void inorder(struct node *root)

{

 

  if(root!= NULL)

  {

    inorder(root->left);

    printf("%d --> ",root->element);

    inorder(root->right);

  }

}

 

void postorder(struct node *root)

{

  if(root != NULL)

  {

    postorder(root->left);

    postorder(root->right);

    printf("%d --> ",root->element);

   }

}

 

int main()

{

 

  int ch,val;

  struct node *newnode;

 

 

  do

  {

    printf("\n\n\t\t  Binary Search Tree \n");

    printf("\n\t<1> Insertion");

    printf("\n\t<2> Traversal Techniques");

    printf("\n\t<3> Search");

    printf("\n\t<4> Exit");

    printf("\n\t    Enter Your Choice:  ");

    scanf("%d",&ch);

    switch(ch)

    {

        case 1:

               printf("\n\n\tInserting Element : ");

               scanf("%d",&val);

               newnode = (struct node *)malloc(sizeof(struct node));

               newnode->element = val;

               newnode->left = NULL;

               newnode->right = NULL;

 

               root = insert(root,newnode);

               printf("\n\n\tInorder :   ");

               inorder(root);

               break;

        case 2:

              if(root == NULL)

                       printf("\n\tEmpty tree ");

              else

              {

 

               printf("\n\n\tInorder Traversal : ");

               inorder(root);

 

               printf("\n\n\tPreorder Traversal : ");

               preorder(root);

 

               printf("\n\n\tPostorder Traversal : ");

               postorder(root);

              }

               break;

        case 3:

               printf("\n\n\tSearch Element : ");

               scanf("%d",&val);

               find(val,root);

               break;

        case 4:exit(0);

        default: printf("\n\t Invalid Choice\n\n");

                 break;

       }

      }while(ch!= 4);

    return 0;

}

 

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Introductory Discussions On Complexity Analysis
What is Complexity Analysis of Algorithm? Complexity Analysis, simply put, is a technique through which you can judge about how good one particular algorithm is. Now the term “good” can mean...

Pattern Matching Algorithms
Pattern Matching Algorithms: There are various algorithms used to implement the pattern matching problem. Some of them are: Brute Force. Boyer-Moore. Knuth-Morris-Pratt (KMP). Brute Force: ...

What Is the Difference Between Function And Subroutine?
The difference is indeed very thin and it depends on the context. In general Function is a piece of code with a name, inputs (arguments) and an output (return types). That is function should always return....

Deadlocks In Distributed Systems
Deadlocks in Distributed Systems: Deadlock can occur whenever two or more processes are competing for limited resources and the processes are allowed to acquire and hold a resource (obtain a lock) thus...

Why Indexing Should Start From Zero In Array ?
Why numbering should start at zero? To denote the subsequence of natural numbers 2, 3, ..., 12 without the pernicious three dots, fourconventions are open to usa) 2 ≤ i < 13b) 1 < i ≤ 12c)...
X

Looking for BTech Tuition Classes?

The best tutors for BTech Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BTech Tuition with the Best Tutors

The best Tutors for BTech Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more