(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-2960223314593660", enable_page_level_ads: true }); Binary Tree using Linked List - TecGlance

Header Ads

Binary Tree using Linked List

#include <stdlib.h>
#include <stdio.h>
struct node
{
int data;
struct node *lc;
struct node *rc;
};
void build_tree (struct node * ptr)
{
char op1,op2;
if(ptr!=NULL)
{
printf("\nEnter the element: ");
scanf("%d",&ptr->data);
printf("\nCreate left node? (Y/N)");
fflush(stdin);
scanf("%c",&op1);
if ((op1=='Y')||(op1=='y'))
{
struct node *lcptr;
lcptr=(struct node *)malloc(sizeof(struct node));
ptr->lc=lcptr;
build_tree(lcptr);
}
else
{
struct node *lcptr;
lcptr=(struct node *)malloc(sizeof(struct node));
ptr->lc=NULL;
lcptr=NULL;
build_tree(lcptr);
}
printf("\nCreate right node? (Y/N)");
fflush(stdin);
scanf("%c",&op2);
if ((op2=='Y')||(op2=='y'))
{
struct node *rcptr;
rcptr=(struct node *)malloc(sizeof(struct node));
ptr->rc=rcptr;
build_tree(rcptr);
}
else
{
struct node *rcptr;
rcptr=(struct node *)malloc(sizeof(struct node));
ptr->rc=NULL;
rcptr=NULL;
build_tree(rcptr);
}
}
}
void traversal(struct node * ptr)
{
if(ptr!=NULL)
{
printf("%d ",ptr->data);
traversal(ptr->lc);
traversal(ptr->rc);
}
};
struct node * search_parent(struct node *ptr,int item,struct node *check)
{
if(ptr!=NULL)
    {
        if((ptr->lc!=NULL)||(ptr->rc!=NULL))
        {
             if(ptr->lc!=NULL)
             {
              if(ptr->lc->data==item)
                {
                check=ptr;
                return check;
        }
             }
             if(ptr->rc!=NULL)
             {
              if(ptr->rc->data==item)
                {
                check=ptr;
                return check;
        }
             }
}
    }
    if(check!=NULL)
    {
        return check;
    }
    if(ptr==NULL)
    {
          return NULL;
    }
    check=search_parent(ptr->lc,item,check);
    check=search_parent(ptr->rc,item,check);
    return check;
}
struct node *search_tree(struct node *ptr,int key,struct node *check)
{
    if(ptr!=NULL)
    {
        if(ptr->data==key)
        {
            check=ptr;
            return check;
        }
    }
    if(check!=NULL)
    {
        return check;
    }
    if(ptr==NULL)
    {
          return NULL;
    }
    check=search_tree(ptr->lc,key,check);
    check=search_tree(ptr->rc,key,check);
    return check;
}
void insert_tree(struct node * root, int item)
{
struct node * ptr;
ptr=search_tree(root,item,NULL);
char opt;
if(ptr==NULL)
{
printf("\nElement not found! ");
exit (0);
}
if((ptr->lc==NULL)||(ptr->rc==NULL))
{
printf("\nInsert as Left/Right child? (L/R)");
fflush(stdin);
scanf("%c",&opt);
if ((opt=='L')||(opt=='l'))
{
if(ptr->lc==NULL)
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
ptr->lc=newnode;
printf("\nEnter the element: ");
scanf("%d",&newnode->data);
newnode->lc=NULL;
newnode->rc=NULL;
}
else
{
printf("\nInsertion not possible at left child!");
}
}
else
{
if(ptr->rc==NULL)
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
ptr->rc=newnode;
printf("\nEnter the element: ");
scanf("%d",&newnode->data);
newnode->lc=NULL;
newnode->rc=NULL;
}
else
{
printf("\nInsertion not possible at right child!");
}
}
}
else
{
printf("\nInsertion not possible");
}
}
void delete_leaf(struct node *root, int item)
{
struct node *parent,*ptr1,*ptr2;
if(root==NULL)
{
printf("\nTree is empty, Deletion not possible");
}
parent=search_parent(root,item,NULL);
if (parent==NULL)
{
printf("\nElement not found");
}
if(parent!=NULL)
{
ptr1=parent->lc;
ptr2=parent->rc;
if(ptr1!=NULL)
{
    if(ptr1->data==item)
    {
    if((ptr1->lc==NULL)&&(ptr1->rc==NULL))
    {
    parent->lc=NULL;
    }
    else
    {
    printf("\nNode is not a leaf node!");
    }
    }
        }
if(ptr2!=NULL)
{
             if(ptr2->data==item)
    {
    if((ptr2->lc==NULL)&&(ptr2->rc==NULL))
    {
    parent->rc=NULL;
    }
    else
    {
    printf("\nNode is not a leaf node!");
    }
    }
        }
}
else
{
printf("\nNo such node exist in tree!");
}
}
int main()
{
struct node *root,*search=NULL;
root=(struct node *)malloc(sizeof(struct node));
root->lc=NULL;
root->rc=NULL;
int item,ch,parent,leaf;
build_tree(root);
traversal(root);
do
{
printf("\n\n..MENU..\n1. Insert an element.\n2. Delete a leaf.\n3. Traversal\n4. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter a parent node: ");
fflush(stdin);
scanf("%d",&parent);
insert_tree(root,parent);
break;
case 2: printf("\nEnter a leaf node: ");
fflush(stdin);
scanf("%d",&leaf);
delete_leaf(root,leaf);
break;
case 3: traversal(root);
break;
case 4: exit(0);
default: printf("\nWrong choice");
}
}
while(1);
}

No comments

Powered by Blogger.