0% found this document useful (0 votes)
14 views5 pages

Aiml14 DS Exp10

This document summarizes a student's experiment implementing a binary search tree. It includes the student's name, roll number, batch, branch, and aim to implement a BST. It provides the source code for inserting nodes into the BST and displaying the tree. The output is a BST with inserted nodes. The conclusion is that the student learned how to implement a BST through this experiment.

Uploaded by

Purva Jage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Aiml14 DS Exp10

This document summarizes a student's experiment implementing a binary search tree. It includes the student's name, roll number, batch, branch, and aim to implement a BST. It provides the source code for inserting nodes into the BST and displaying the tree. The output is a BST with inserted nodes. The conclusion is that the student learned how to implement a BST through this experiment.

Uploaded by

Purva Jage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DATA STRUCTURE

EXPERIMENT : 10
NAME : JAGE TANVI NARENDRA
ROLL NO. : AIML14
BATCH : A1
BRANCH : CSE(AIML)
AIM :
TO IMPLEMENTATION OF BINARY SEARCH TREE

SOURCE CODE :
#include<stdio.h>
#include<stdlib.h>

struct node
{
struct node *lchild;
int info;
struct node *rchild;
};

struct node *insert(struct node *ptr, int ikey);


void display(struct node *ptr,int level,int side);

int main( )
{
struct node *root=NULL,*ptr;
int choice,k,p,i;

while(1)
{
printf("\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:

printf("\nEnter the key to be inserted : ");


scanf("%d",&k);
root = insert(root, k);
break;

case 2:
printf("\n");
display(root,0,1);
printf("\n");
break;

case 3:
exit(1);

default:
printf("\nWrong choice\n");
}
}

return 0;
}

struct node *insert(struct node *ptr, int ikey )


{
if(ptr==NULL)
{
ptr = (struct node *) malloc(sizeof(struct node));
ptr->info = ikey;
ptr->lchild = NULL;
ptr->rchild = NULL;
}
else if(ikey < ptr->info) /*Insertion in left subtree*/
ptr->lchild = insert(ptr->lchild, ikey);
else if(ikey > ptr->info) /*Insertion in right subtree */
ptr->rchild = insert(ptr->rchild, ikey);
else
printf("\nDuplicate key\n");
return ptr;
}

void display(struct node *ptr,int level,int side)


{
int i,p=0;
if(ptr == NULL )
return;
level++;
//else
{
display(ptr->rchild, level,1);
printf("\n");

for (i=0; i<level; i++)


{ printf("\t");}
if(side==1)
printf("/ ");
else
printf("\\ ");

printf("%d", ptr->info);
display(ptr->lchild, level,0);
}
}/*End of display()*/

OUTPUT :
CONCLUSION :
From this experiment we have learnt the implementation of a binary search
tree.

You might also like