Program for binary search tree in C

In binary search tree,

The first number that we insert is the root element and every other number that is entered in the binary search tree is compared with the root element.

If the new number is > the root element then it is put in the right sub-tree.

And if new number is < the root element then it is put in the left sub-tree.

 

Program:

#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
struct node *r;
struct node* insert(struct node*,int val);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
int main()
{
    r=NULL;
    int i,a,b=1,n;
    printf("Enter the number of values you wanna insert in binary search tree.\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter the value %d:",i+1);
        scanf("%d",&a);
        r=insert(r,a);
    }
    while(b)
    {
    printf("In which format do you want to display it?\n1. Preorder\n2. Inorder\n3. Postorder");
    scanf("%d",&n);
    switch(n)
    {
    case 1:
        {
            preorder(r);
            break;
        }
    case 2:
        {
            inorder(r);
            break;
        }
    case 3:
        {
            postorder(r);
            break;
        }
    default:
        printf("Not a valid option.\n");
    }
    printf("Continue? 1/0\n");
    scanf("%d",&b);
    }
}

struct node* insert(struct node *r,int val)
{
    if(r==NULL)
    {
        r=(struct node*) malloc(sizeof(struct node));
        r->data=val;
        r->left=NULL;
        r->right=NULL;
    }
    else if(val>r->data)
    {
        r->right=insert(r->right,val);
    }
    else
    {
        r->left=insert(r->left,val);
    }
    return r;
};

void preorder(struct node *r)
{
    if(r!=NULL)
    {
    printf("%d\t",r->data);
    preorder(r->left);
    preorder(r->right);
}
}
void inorder(struct node *r)
{
if(r!=NULL)
{
    inorder(r->left);
    printf("%d\t",r->data);
    inorder(r->right);
}
}

void postorder(struct node *r)
{
    if(r!=NULL)
    {
    postorder(r->left);
    postorder(r->right);
    printf("%d\t",r->data);
}
}

 

Library Management System (Using Database)

Hello Everyone,

I am back with another database project which was finalized 2 days back and this time I worked on Library Management using database.

Here’s a brief video on it:

So without much talking, let’s dive into the project.

To begin with, following are the tables which have been used in this project:

  1. BOOKS (Details about books in library):
Column Datatype
Book_ID VARCHAR2(10 CHAR)
Book_Title VARCHAR2(300 CHAR)
Book_Author VARCHAR2(50 CHAR)

2. USERS (Users includes both staff and students):

Column Datatype
USER_ID VARCHAR2(10 CHAR)
NAME VARCHAR2(50 CHAR)
DESIGNATION VARCHAR2(10 CHAR)

3. ISSUES (Records for books issued):

Column Datatype
Issue_ID Number(10,0)
Book_ID VARCHAR2(10 CHAR)
Book_Title Varchar2(100 CHAR)
Borrower_ID VARCHAR2(10 CHAR)
Borrower_Name Varchar2(100 CHAR)
Borrower_Designation Varchar2(50 CHAR)
Issue_Date DATE
Date_Expiry DATE

4. RETURNS (Records for books returned):

Column DataType
Return_ID Number(10,0)
Book_ID VARCHAR2(10 CHAR)
Borrower_ID VARCHAR2(10 CHAR)
Borrower_Name Varchar2(100 CHAR)
Issue_Date DATE
Deadline_Date DATE
Return_Date DATE
Delay Number(10,0)

5. FINE (Records for fine against books):

Column DataType
Fine_ID Number(10,0)
Book_ID VARCHAR2(10 CHAR)
Book_Title Varchar2(100 CHAR)
Borrower_ID VARCHAR2(10 CHAR)
Borrower_Name Varchar2(100 CHAR)
Delay NUMBER(19,9)
Fine_Amount NUMBER(39,19)

 

Please find below the lucid chart for better explaination:

 

 

 

 

 

 

 

 

 

 

 

 

 

Following are two points which were set as standard while using the delay logic:

For staff members, the maximum delay in return can be of 5 days.

For students,  the maximum delay in return can be of 4 days.

You can change this value by changing the value in below trigger (ISSUE_EXPIRY):

 

 

 

I have provided the SQL files for table creations – BOOKS, USERS, ISSUES, RETURNS and FINE which includes the sample data insertion queries as well.

Apart from this, I have also included the triggers file which will automate the value insertion of columns (marked with green arrow).

Zip file containing all the project files: Click here to download
Lucid (ERD): Click here to download

If you liked the stuff, please leave positive comment and in case you have any feedback, feel free to share that in comments!