Friday 10 March 2017

Slip 1 :- Binary search tree DS S.Y.Bsc Computer Science Pune University

Download Solved DS Slips for S.Y.BSc Comp Sci 2017 (Pune University) https://goo.gl/S5RaiQ
/*
Write a C program which uses Binary search tree library and implements following Functions:
a) Create Binary Tree [10]
b) Insert a node in a binary tree [5]
c) Search a node in binary tree [5]
d) Display a binary tree [5]
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
            int data;
            struct node *left,*right;
};
struct node * createnode(struct node *newnode,int data)
{
            newnode=malloc(sizeof(struct node));
            newnode->data=data;
            newnode->left= newnode->right = NULL;         
            return newnode;
}
void insert(struct node **root,int data)
{
            struct node *newnode;
            newnode=createnode(newnode,data);
            if(*root==NULL)
                            *root=newnode;
            else
            {
                            struct node *temp=*root;
                            while(1)
                            {
                           
                                            if(data <= temp->data)
                                            {
                                                            if(temp->left==NULL)
                                                            {
                                                                        temp->left=newnode;
                                                                        break;
                                                            }
                                                            temp=temp->left;
                                                           
                                            }
                                            else
                                            {
                                                            if(temp->right==NULL)
                                                            {
                                                                        temp->right=newnode;
                                                                        break;
                                                            }
                                                            temp=temp->right;
                                            }
                            }
            }
}
void createbst(struct node **root)
{
            int n,i;
            int data;
            printf("\n\tENter the how many nodes ");
            scanf("%d",&n);
           
            for(i=0;i<n;i++)
            {
                        printf("\n\tEnter data : ");
                        scanf("%d",&data);
                            insert(root,data);
            }
}
void display(struct node *temp)
{
            if(temp)
            {
                            printf("%d\t",temp->data);
                            display(temp->left);
                            display(temp->right);
            }
}
int search(struct node *temp,int data)
{
            if(temp)
            {
                            if(temp->data == data)
                            {
                                            printf("data found");
                                            return;              
                            }
                            search(temp->left,data);
                            search(temp->right,data);
            }
}
int menu()
{
            int ch;
            system("clear");
            printf("\n\t0.Exit\n\t1.Create BST\n\t2.Insert Node\n\t3.Search a node\n\t4.Display tree\n\tEnter you choice :  ");
            scanf("%d",&ch);
            return ch;
}
main()
{
            struct node *root= NULL;
            int ch;
           
            while((ch=menu())!=0)
            {
                            if(ch==1)
                            {
                                            createbst(&root);
                            }
                            else
                            if(ch==2)
                            {
                                            int node;
                                            printf("\n\tEnter data : ");
                                            scanf("%d",&node);
                                            insert(&root,node);
                            }
                            else
                            if(ch==3)
                            {
                                            int node;
                                            printf("\n\tEnter node to be searched : ");
                                            scanf("%d",&node);
                                            search(root,node);
                                            getchar();getchar();
                            }
                            else
                            if(ch==4)
                            {
                                            display(root);
                                            getchar();getchar();
                            }
            }           
}
Download Solved DS Slips for S.Y.BSc Comp Sci 2017 (Pune University) https://goo.gl/S5RaiQ

No comments:

Post a Comment