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

Home » C Home » Data Structures Home » Binary Search Tree (with display option)

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

Search Projects & Source Codes:

Title Binary Search Tree (with display option)
Author Fawad Bin Emad
Author Email fawadbinemad [at] hotmail.com
Description This Binary Search Tree have an extra advantage.You can see inserted numbers in a tree order.
Category C » Data Structures
Hits 391248
Code Select and Copy the Code
Code : #include<stdio.h> #include<conio.h> #include<stdlib.h> struct tree { int data; tree *left,*right,*parent; }; typedef tree *tree_type; tree_type root=NULL; void delete_Node(); void printTree(tree_type ptr,int x,int y,int depth); void predessor(); void successor(); tree_type minValue(tree_type ptr); tree_type maxValue(tree_type ptr); void insertion(); void search(); tree_type SearchNode(tree_type ptr,int target); void Preorder(tree_type ptr); void Postorder(tree_type ptr); void Inorder(tree_type ptr); void insert(); void insert_node(tree_type curr,tree_type ptr); void Destroy(); void main(void) { char choice; do{ clrscr(); printf(" Binary Search Tree "); printf(" Insert Node.......1 "); printf("Search Node.......2 "); printf("Successor Node....3 "); printf("Predessor Node....4 "); printf("Delete Node.......5 "); printf("Indorder..........6 "); printf("Preorder..........7 "); printf("Postorder.........8 "); printf("Destroy Tree......9 "); printf("Exit..............0 "); printf(" Enter Choice Number "); scanf("%c",&choice); switch(choice) { case 49 : insertion();break; case 50 : search();getch();break; case 51 : successor();getch();break; case 52 : predessor();getch();break; case 53 : delete_Node();break; case 54 : clrscr();printf(" INORDER TRAVERSAL ");Inorder(root);gotoxy(30,7);printf("Binary Search Tree");printTree(root,38,10,0);getch();break; case 55 : clrscr();printf(" PREORDER TRAVERSAL ");Preorder(root);gotoxy(30,7);printf("Binary Search Tree");printTree(root,38,10,0);getch();break; case 56 : clrscr();printf(" POSTORDER TRAVERSAL ");Postorder(root);gotoxy(30,7);printf("Binary Search Tree");printTree(root,38,10,0);getch();break; case 57 : clrscr();Destroy();break; case 48 : break; } }while(choice!=48); } void Destroy() { char ch; clrscr(); printf(" Destroy Tree (Y/N) ? "); ch=getche(); if(ch=='y'||ch=='Y') root=NULL; } void insert(int value) { tree_type ptr; tree_type curr =(tree_type)malloc(sizeof(tree)); curr->data=value; curr->left=NULL; curr->right=NULL; curr->parent=NULL; if(root==NULL) root=curr; else { ptr=root; while(ptr->right!=NULL||ptr->left!=NULL) { if(ptr->data>=curr->data) if(ptr->left!=NULL) ptr=ptr->left; else break; if(ptr->data<curr->data) if(ptr->right!=NULL) ptr=ptr->right; else break; } curr->parent=ptr; if(ptr->data<curr->data) ptr->right=curr; if(ptr->data>curr->data) ptr->left=curr; } } void printTree(tree_type ptr,int x,int y,int depth) { if(ptr==NULL) return; gotoxy(x,y); printf("%d",ptr->data); if(ptr->left!=NULL) printTree(ptr->left,x-14/(depth+1),y+4,depth+1); if(ptr->right!=NULL) printTree(ptr->right,x+14/(depth+1),y+4,depth+1); } void insert_node(tree_type curr,tree_type ptr) { while(ptr->right!=NULL||ptr->left!=NULL) { if(ptr->data>=curr->data) if(ptr->left!=NULL) ptr=ptr->left; else break; if(ptr->data<curr->data) if(ptr->right!=NULL) ptr=ptr->right; else break; } curr->parent=ptr; if(ptr->data<curr->data) ptr->right=curr; if(ptr->data>curr->data) ptr->left=curr; } void search() { int target; clrscr(); gotoxy(30,7);printf("Binary Search Tree");printTree(root,38,10,0); gotoxy(1,1); printf(" Enter The Target Value To Search "); scanf("%d",&target); if(SearchNode(root,target)!=NULL) printf("Value %d Was Found ",target); else printf("Value %d Wasn't Found ",target); } tree_type SearchNode(tree_type ptr,int target) { if(ptr==NULL) return NULL; if(ptr->data==target) return ptr; if(target<ptr->data) return SearchNode(ptr->left,target); else return SearchNode(ptr->right,target); } void Inorder(tree_type ptr) { if(ptr==NULL) return; Inorder(ptr->left); printf(" %d ",ptr->data); Inorder(ptr->right); } void Preorder(tree_type ptr) { if(ptr==NULL) return; printf(" %d ",ptr->data); Preorder(ptr->left); Preorder(ptr->right); } void Postorder(tree_type ptr) { if(ptr==NULL) return; Postorder(ptr->left); Postorder(ptr->right); printf(" %d ",ptr->data); } void insertion() { int value; clrscr(); printf("Insert Values Greater Than Zero "); scanf("%d",&value); while(value>0) { insert(value); scanf("%d",&value); } } tree_type maxValue(tree_type ptr) { while(ptr->right!=NULL) { ptr=ptr->right;} return ptr; } tree_type minValue(tree_type ptr) { while(ptr->left!=NULL) ptr=ptr->left; return ptr; } void successor() { int target; tree_type ptr; clrscr(); gotoxy(30,7);printf("Binary Search Tree");printTree(root,38,10,0); gotoxy(1,1); printf(" Enter The Value To Find Its Successor Node "); scanf("%d",&target); ptr=SearchNode(root,target); if(ptr==NULL) {printf(" Value %d Was Not Found ",target);return;} if(ptr==maxValue(root)) printf(" Successor Node of %d Was Not Found ",target); else { if(ptr->right!=NULL) { ptr=minValue(ptr->right); printf(" The Successor Node Was Found To Be %d",ptr->data); } else { while(ptr!=ptr->parent->left) ptr=ptr->parent; if(ptr->parent!=NULL) printf(" The Successor Node Was Found To Be %d",ptr->parent->data); else printf(" Successor Node Was Not Found "); } } } void predessor() { int target; tree_type ptr; clrscr(); gotoxy(30,7);printf("Binary Search Tree");printTree(root,38,10,0); gotoxy(1,1); printf(" Enter The Value To Find Its Predessor Node "); scanf("%d",&target); ptr=SearchNode(root,target); if(ptr==NULL) {printf(" Value %d Was Not Found ",target);return;} if(ptr==minValue(root)) printf(" Predessor Node of %d Was Not Found ",target); else { if(ptr->left!=NULL) { ptr=maxValue(ptr->left); printf(" The Predessor Node Was Found To Be %d",ptr->data); } else { while(ptr!=ptr->parent->right) ptr=ptr->parent; if(ptr->parent!=NULL) printf(" The Predessor Node Was Found To Be %d",ptr->parent->data); else printf(" Predessor Node Was Not Found "); } } } void delete_Node() { int target; clrscr(); gotoxy(30,7);printf("Binary Search Tree");printTree(root,38,10,0); gotoxy(1,2); printf(" Enter The Target Value To Delete "); scanf("%d",&target); tree_type ptr; ptr=SearchNode(root,target); if(ptr==NULL) { printf(" Node %d Was Not Found ",target);getch();} else { if(ptr==root&&ptr->left==NULL&&ptr-> right==NULL) {root=NULL;return;} if(ptr==root&&ptr->right!=NULL) { root=ptr->right; insert_node(ptr->left,root); return; } else if(ptr==root&&ptr->left!=NULL) {root=ptr->left;return;} if(ptr->right==NULL&&ptr->left==NULL) if(ptr->data < ptr->parent->data) ptr->parent->left=NULL; else ptr->parent->right=NULL; else { if(ptr->data<ptr->parent->data) ptr->parent->left=NULL; else ptr->parent->right=NULL; insert_node(ptr->right,ptr->parent); insert_node(ptr->left ,ptr->parent); } } }

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=1047


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