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

Home » C Home » Data Structures Home » Binary Tree

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

Search Projects & Source Codes:

Title Binary Tree
Author Praveen P
Author Email praviis [at] rediffmail.com
Description Implementation of Binary Tree using Linked list
Category C » Data Structures
Hits 389104
Code Select and Copy the Code
Code : #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct btree { int n; struct btree *left; struct btree *right; }typedef btree,node; btree *root,*ptr,*prev,*bfor,*x,*y; int num; char ch; void main() { int c; void create(node *); void print(node *); void search(node *); void insert(node *); void modify(node *); void delet(node *); while(c!=7) { clrscr(); printf("1. CREATE "); printf("2. PRINT "); printf("3. SEARCH "); printf("4. INSERT "); printf("5. MODIFY "); printf("6. DELETE "); printf("7. EXIT "); printf("Enter your choice : "); scanf("%d",&c); switch(c) { case 1 : { root=(node *)malloc(sizeof(node)); printf(" Enter number : "); scanf("%d",&root->n); ptr=root; root->left=root->right=NULL; printf("Enter more(y/n)? "); ch=getch(); create(root); break; } case 2 : { print(root); getch(); break; } case 3 : { printf(" "); search(root); getch(); break; } case 4 : { printf(" "); insert(root); getch(); break; } case 5 : { printf(" "); modify(root); getch(); break; } case 6 : { printf(" "); delet(root); getch(); break; } case 7 : exit(0); default: { printf(" Invalid choice"); getch(); break; } } } getch(); } void create(node *ptr) { while(ch == 'y') { printf(" "); ptr=prev=root; printf("Enter number : "); scanf("%d",&num); do { if(num < ptr->n) { prev=ptr; ptr=ptr->left; } else if(num > ptr->n) { prev=ptr; ptr=ptr->right; } else { prev=NULL; break; } }while(ptr); if(prev) { ptr=(node *)malloc(sizeof(node)); ptr->n=num; ptr->left=ptr->right=NULL; if(ptr->n < prev->n) { prev->left=ptr; if(prev == root) root=prev; } if(ptr->n > prev->n) { prev->right=ptr; ptr->left=ptr->right=NULL; if(prev == root) root=prev; } } else printf("'%d' is already present.. ",num); printf("Enter more(y/n)? "); ch=getch(); } } void print(node *ptr) { void inprint(node *); void preprint(node *); void postprint(node *); if(!ptr) { printf("Tree is empty..."); return; } printf(" Root is '%d' ",root->n); printf(" INORDER : "); inprint(root); printf(" PREORDER : "); preprint(root); printf(" POSTORDER : "); postprint(root); } void inprint(node *ptr) { if(!ptr) return; inprint(ptr->left); printf("%2d ",ptr->n); inprint(ptr->right); return; } void preprint(node *ptr) { if(!ptr) return; printf("%2d ",ptr->n); preprint(ptr->left); preprint(ptr->right); return; } void postprint(node *ptr) { if(!ptr) return; postprint(ptr->left); postprint(ptr->right); printf("%2d ",ptr->n); return; } void search(node *ptr) { if(!ptr) { if(ptr == root) { printf("Tree is empty... You can't search anything..."); return; } } printf("Enter the number to search : "); scanf("%d",&num); while(ptr) { if(ptr->n == num) { printf("Success... You found the number..."); return; } else if(ptr->n < num) ptr=ptr->right; else ptr=ptr->left; } printf("Tree don't contain '%d'...",num); } void insert(node *ptr) { if(!ptr) { printf("Tree is empty...First create & then insert... "); return; } printf(" "); ptr=prev=root; printf("Enter number to be inserted : "); scanf("%d",&num); do{ if(num < ptr->n) { prev=ptr; ptr=ptr->left; } else if(num > ptr->n) { prev=ptr; ptr=ptr->right; } else { prev=NULL; break; } }while(ptr); if(prev) { ptr=(node *)malloc(sizeof(node)); ptr->n=num; ptr->left=ptr->right=NULL; if(ptr->n < prev->n) { prev->left=ptr; if(prev == root) root=prev; } if(ptr->n > prev->n) { prev->right=ptr; ptr->left=ptr->right=NULL; if(prev == root) root=prev; } printf(" '%d' is inserted...",num); } else printf("'%d' is already present...",num); return; } void modify(node *ptr) { int mod; if(!ptr) { if(ptr == root) { printf("Tree is empty... You can't modify anything..."); return; } } printf("Modification of particular number can't create a binary tree..."); getch(); printf(" Enter the number to get modified : "); scanf("%d",&num); prev=ptr; while(ptr) { if(ptr->n == num) { x=ptr; mod=x->n; printf("Then enter new number : "); scanf("%d",&num); bfor=ptr=root; while(ptr) { if(ptr->n == num) { printf("'%d' already present...Modification denied...,",num); return; } else if(ptr->n < num) { bfor=ptr; ptr=ptr->right; } else { bfor=ptr; ptr=ptr->left; } } if(x==root) { y=x->right; root=y; while(y->left) y=y->left; y->left=x->left; free(x); } else if(!(x->left) && !(x->right)) { if(prev->left == x) { prev->left=NULL; free(x); } else if(prev->right == x) { prev->right=NULL; free(x); } } else if(!(x->left)) { if(prev->left == x) { prev->left=x->right; free(x); } else if(prev->right == x) { prev->right=x->right; free(x); } } else if(!(x->right)) { if(prev->left == x) { prev->left=x->left; free(x); } else if(prev->right == x) { prev->right=x->left; free(x); } } else { y=x->right; while(y->left) y=y->left; y->left=x->left; if(prev->left == x) prev->left=y; else if(prev->right == x) prev->right=y; free(x); } ptr=(node *)malloc(sizeof(node)); ptr->n=num; ptr->left=ptr->right=NULL; if(ptr->n < bfor->n) { bfor->left=ptr; if(bfor == root) root=bfor; } if(ptr->n > bfor->n) { bfor->right=ptr; ptr->left=ptr->right=NULL; if(bfor == root) root=bfor; } printf("'%d' is modified by '%d'",mod,ptr->n); return; } else if(ptr->n < num) { prev=ptr; ptr=ptr->right; } else { prev=ptr; ptr=ptr->left; } } printf("Tree don't contain '%d'...",num); } void delet(node *ptr) { if(!ptr) { if(ptr == root) { printf("Tree is empty... You can't delete anything..."); return; } } printf("Enter the number to get deleted : "); scanf("%d",&num); prev=ptr; while(ptr) { if(ptr->n == num) { if(ptr==root) { x=ptr->right; root=x; while(x->left) x=x->left; x->left=ptr->left; free(ptr); printf("'%d' is deleted...",num); return; } else if(!(ptr->left) && !(ptr->right)) { if(prev->left == ptr) prev->left=NULL; else prev->right=NULL; free(ptr); printf("'%d' is deleted...",num); return; } else if(!(ptr->left)) { if(prev->left == ptr) { prev->left=ptr->right; free(ptr); } else if(prev->right == ptr) { prev->right=ptr->right; free(ptr); } printf("'%d' is deleted...",num); return; } else if(!(ptr->right)) { if(prev->left == ptr) { prev->left=ptr->left; free(ptr); } else if(prev->right == ptr) { prev->right=ptr->left; free(ptr); } printf("'%d' is deleted...",num); return; } else { x=ptr->right; while(x->left) x=x->left; x->left=ptr->left; if(prev->left == ptr) prev->left=ptr->right; else if(prev->right == ptr) prev->right=ptr->right; free(ptr); printf("'%d' is deleted...",num); return; } } else if(ptr->n < num) { prev=ptr; ptr=ptr->right; } else { prev=ptr; ptr=ptr->left; } } printf("Tree don't contain '%d'...",num); }

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 | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=971


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