Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.5 kB
1
Indexable
Never
/*Create binary search tree to store srn of students where srns are strings.
1)Implement insert function by comparing strings.
2)a.Implement function to print all sorted srn in ascending order
  b.Implement function to search for srn
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void tree_insert(char tree[100][100],char x[100]);
void disp(char tree[100][100],int index);
int srn_check(char tree[100][100],int,char[]);
//int bin_check(char tree[100][100],int);
int main()
{
        int n,ch,k;
        char x[100],value[100];
        char tree[100][100];
        for(int i=0;i<100;i++)
        {
                strcpy(tree[i],"0");
        }
        while(1)
        {
                printf("\n1.Insert\n2.Display using preorder\n3.Check\n4.Exit\n");
                scanf("%d",&ch);
                switch(ch)
                {
                        case 1:
                        printf("Enter data to insert:");
                        scanf("%s",x);
                        tree_insert(tree,x);
                        break;
                        
                        case 2:
                        disp(tree,0);
                        break;
                        
                        case 3:
                        printf("Enter srn to check:");
                        scanf("%s",value);
                        k=srn_check(tree,0,value);
                        if(k==1) printf("Found\n");
                        else printf("Not found\n");
                        break;
                        
                        
                        case 4:exit(0);
               }
         }
        return 0;
}

void tree_insert(char tree[100][100],char x[100])
{
        int n=0;
        int flag=0;
        while(strcmp(tree[n],"0")!=0)
        {
                if(strcmp(x,tree[n])<0)
                        n=2*n+1;
                else if(strcmp(x,tree[n])>0)
                        n=2*n+2;
                else 
                {
                flag=1;
                printf("Cannot insert\n");
                break;
                }
       }
       if (flag==0)
       strcpy(tree[n],x);
}

void disp(char tree[100][100],int index)
{
        if(strcmp(tree[index],"0")!=0)
        {
                printf("%s-",tree[index]);
                disp(tree,2*index+1);
                disp(tree,2*index+2);
        }
}

int srn_check(char tree[100][100],int index,char value[100])
{
        if(strcmp(tree[index],"0")!=0)
        {
                
                if(strcmp(tree[index],value)!=0)
                {
                srn_check(tree,2*index+1,value);
                srn_check(tree,2*index+2,value);
                }
                else return 1;
        }
        else return 0;
}

/*
int bin_check(char tree[100][100],int parent)
{
        if(strcmp(tree[n],"0")!=0)
        {
                int right=2*parent+2;
                int left=2*parent+1;
                if(t[right]==-1) bin_check(t,parent*2+1);
                        
                else if(t[right]>t[parent] && t[left]<t[parent])
                {
                        bin_check(t,parent*2+1);
                        bin_check(t,parent*2+1);
                }
                else
                {
                        return 0;
                }
        }
        else
                return 1;
                        
                 
}*/