0% found this document useful (0 votes)
23 views7 pages

Ex 4

DC characters
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)
23 views7 pages

Ex 4

DC characters
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/ 7

Binary search tree Implementation

Date: name:

Rollno

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct node {

char name[20];

int rollno;

int salary;

char month[20];

float attendance;

struct node *left;

struct node *right;

} *root = NULL;

struct node *createnode(char a[], int b, int c, char d[], float e) {

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

strcpy(temp->name, a);

temp->rollno = b;

temp->salary = c;

strcpy(temp->month, d);

temp->attendance = e;

temp->left = NULL;

temp->right = NULL;

return temp;

}
struct node *insert(struct node *T, char a[], int b, int c, char d[], float e) {

if (T == NULL)

T = createnode(a, b, c, d, e);

else if (e > T->attendance)

T->right = insert(T->right, a, b, c, d, e);

else if (e < T->attendance)

T->left = insert(T->left, a, b, c, d, e);

return T;

void preorder(struct node *T) {

if (T != NULL) {

printf("\n%s --> %d --> %d --> %s --> %f", T->name, T->rollno, T->salary, T->month, T->attendance);

preorder(T->left);

preorder(T->right);

void postorder(struct node *T) {

if (T != NULL) {

postorder(T->left);

postorder(T->right);

printf("\n%s --> %d --> %d --> %s --> %f", T->name, T->rollno, T->salary, T->month, T->attendance);

void inorder(struct node *T) {

if (T != NULL) {

inorder(T->left);

printf("\n%s --> %d --> %d --> %s --> %f", T->name, T->rollno, T->salary, T->month, T->attendance);
inorder(T->right);

void descendingorder(struct node *T) {

if (T != NULL) {

descendingorder(T->right);

printf("\n%s --> %d --> %d --> %s --> %f", T->name, T->rollno, T->salary, T->month, T->attendance);

descendingorder(T->left);

struct node *minimum(struct node *T) {

while (T->left != NULL)

T = T->left;

return T;

struct node *maximum(struct node *T) {

while (T->right != NULL)

T = T->right;

return T;

struct node *Search(struct node *T, int d) {

if (T == NULL || T->rollno == d)

return T;

if (d > T->rollno)

return Search(T->right, d);

else

return Search(T->left, d);


}

struct node *Delete(struct node *T, int d) {

if (T == NULL)

return T;

else if (d > T->rollno)

T->right = Delete(T->right, d);

else if (d < T->rollno)

T->left = Delete(T->left, d);

else {

if (T->left == NULL && T->right == NULL) {

free(T);

T = NULL;

} else if (T->left == NULL) {

struct node *temp = T;

T = T->right;

free(temp);

} else if (T->right == NULL) {

struct node *temp = T;

T = T->left;

free(temp);

} else {

struct node *temp = minimum(T->right);

T->rollno = temp->rollno;

T->right = Delete(T->right, temp->rollno);

return T;

int main() {
int n;

char name[20];

int rollno, salary;

char month[20];

float attendance;

char confirm;

while (1) {

printf("\n1.Insert employee details\n2.Delete employee details by rollno\n3.Inorder\n4.Postorder\


n5.Preorder\n6.Maximum attendance\n7.Minimum attendance\n8.Search with rollno\n9.Display in
descending order of attendance\n10.Exit");

printf("\nPlease enter the choice:");

scanf("%d", &n);

switch (n) {

case 1:

do {

printf("Enter the name: ");

scanf("%s", name);

printf("Enter the rollno: ");

scanf("%d", &rollno);

printf("Enter the salary: ");

scanf("%d", &salary);

printf("Enter the month: ");

scanf("%s", month);

printf("Enter the attendance: ");

scanf("%f", &attendance);

root = insert(root, name, rollno, salary, month, attendance);

printf("\nAdd another employee? (y/n): ");

scanf(" %c", &confirm);

} while (confirm == 'y' || confirm == 'Y');

printf("\nRegistration successfully saved.\n");

break;
case 2:

printf("\nEnter the rollno number:");

scanf("%d", &rollno);

root = Delete(root, rollno);

break;

case 3:

inorder(root);

break;

case 4:

postorder(root);

break;

case 5:

preorder(root);

break;

case 6:

printf("\nMaximum attendance student:\n%s --> %d --> %d --> %s --> %f", maximum(root)->name,


maximum(root)->rollno, maximum(root)->salary, maximum(root)->month, maximum(root)->attendance);

break;

case 7:

printf("\nMinimum attendance student:\n%s --> %d --> %d --> %s --> %f", minimum(root)->name,


minimum(root)->rollno, minimum(root)->salary, minimum(root)->month, minimum(root)->attendance);

break;

case 8:

printf("\nEnter the rollno:");

scanf("%d", &rollno);

struct node *x = Search(root, rollno);

if (x != NULL && x->rollno == rollno)

printf("\n%s --> %d --> %d --> %s --> %f", x->name, x->rollno, x->salary, x->month, x->attendance);

else

printf("\nElement %d not found", rollno);

break;
case 9:

descendingorder(root);

break;

case 10:

return 0;

default:

printf("\nPlease enter the correct option");

Result:

Thus the implementation of binary search was compile and run successfully.

You might also like