Add to Favorites    Make Home Page 10008 Online  
 Language Categories  
 Our Services  

Home » C Home » Data Structures Home » Binary tree Implementaion Insertion & Deletion(Using

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Binary tree Implementaion Insertion & Deletion(Using
Author K.E.Joseph
Author Email kejoseph77 [at] yahoo.co.in
Description Implementation Of BINARY TREE OPERATION(insertion &
Deletion)
Using LINLED LIST
Category C » Data Structures
Hits 43989
Code Select and Copy the Code
// Implementation Of BINARY TREE OPERATION (insertion & Deletion) #include<conio.h> #include<stdio.h> #include<malloc.h> #include<process.h> struct node { struct node *llink; struct node *rlink; int data; }; void main() { struct node *head,*t; int s,d; struct node* finsert(); struct node* delenode(struct node*,int); void insert(struct node *); void inorder(struct node *); clrscr(); head=NULL; do { printf(" 1-Insertion"); printf(" 2-Deletion"); printf(" 3-Inorder"); printf(" 4-Exit"); printf(" Enter Choice:"); scanf("%d",&s); switch(s) { case 1://insertion if(head==NULL) { head=finsert(); } else insert(head); break; case 2://Deletion if(head==NULL) printf(" Binary Tree Empty......."); else { printf(" Enter Data to delete:"); scanf("%d",&d); if(head->llink==NULL && head->rlink==NULL && head->data==d) { t=head; head=NULL; free(t); } else head = delenode(head,d); } break; case 3://to display printf(" IN-ORDER:"); if(head==NULL) printf(" Binary Tree Empty...."); else inorder(head); break; case 4://exit exit(0); } }while(s<5 ||s>0); getch(); } struct node* finsert() { struct node * head; head=(struct node*)malloc(sizeof(struct node)); printf(" Enter Data:"); scanf("%d",&head->data); head->llink=NULL; head->rlink=NULL; return head; } void insert(struct node *head) { struct node *t,*n; t=head; n=(struct node *)malloc(sizeof(struct node)); printf(" Enter Data:"); scanf("%d",&n->data); n->llink=NULL; n->rlink=NULL; while(t->llink!=NULL || t->rlink!=NULL) { if(t->llink!=NULL) if(n->data < t->data) t=t->llink; if(t->rlink!=NULL) if(n->data>=t->data) t=t->rlink; if((t->llink==NULL) && (n->data < t->data) && (n->data < t->rlink->data)) break; if((t->rlink==NULL) && (n->data >= t->data) && (n->data > t->llink->data)) break; } if((n->data < t->data) && (t->llink==NULL)) t->llink=n; if((n->data > t->data) && (t->rlink==NULL)) t->rlink=n; } void inorder(struct node * head) { if(head!=NULL) { inorder(head->llink); printf("%d ",head->data); inorder(head->rlink); } } struct node * delenode(struct node *head,int d) { int f=0,f1=0; struct node *p,*t,*t1,*x; t=head; //to search found or not while(t!=NULL) { if(t->data==d) { f=1; x=t; break; } if(t->data > d) { p=t; t=t->llink; } else if(t->data <= d) { p=t; t=t->rlink; } } if(f==0) { printf(" Given element not found......."); return head; } //Deleted node has no child if(x->llink==NULL && x->rlink==NULL) { if(p->rlink==x) p->rlink=NULL; else p->llink=NULL; free(x); return head; } //deleted node has 2 children if(x->llink!=NULL && x->rlink!=NULL) { p=x; t1=x->rlink; while(t1->llink!=NULL) { p=t1; f1=1; t1=t1->llink; } if(t1->llink==NULL && t1->rlink==NULL) { x->data=t1->data; if(f1==1) p->llink=t1->llink; if(f1==0) x->rlink=t1->rlink; free(t1); return head; } if(t1->rlink!=NULL) { x->data=t1->data; if(f1==1) p->llink=t1->rlink; if(f1==0) p->rlink=t1->rlink; free(t1); return head; } } //Deleted node has oniy right child if(x->llink==NULL && x->rlink!=NULL && x->data!=head->data) { if(p->llink==x) p->llink=x->rlink; else p->rlink=x->rlink; free(x); return head; } //Deleted node has oniy left child if(x->llink!=NULL && x->rlink==NULL && x->data!=head->data) { if(p->llink==x) p->llink=x->llink; else p->rlink=x->llink; free(x); return head; } if(x->llink!=NULL && x->rlink==NULL && x->data==head->data) { head=x->llink; free(p); return head; } if(x->llink==NULL && x->rlink!=NULL && x->data==head->data) { head=x->rlink; free(p); return head; } }

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi դ ? ??
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2017 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1170


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review