Binary Search Tree

378 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
378
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Binary Search Tree

  1. 1. Binary Search Tree#include<stdio.h> do#include<conio.h> {#include<process.h> printf("n***MENU***");#include<alloc.h> printf("n1.CREATEn2.INSERT");struct node printf("n3.DELETEn4.DISPLAYn5.EXIT"){ ; int data; printf("nPLease Enter the Choice..."); struct node *left,*right; scanf("%d",&ch);}*root=NULL,*New,*temp,*p; switch(ch)int item; {void create(); case 1:create();break;void disp(struct node*,int); case 2:insert();break;void del(); case 3:del();break;void insert(); case 4:disp(root,1);break;void main() case 5:exit(0);{ default:printf("nInvalid choice..."); int ch,e; } clrscr(); }while(ch!=5); getch();}
  2. 2. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else printf("nDo you want add Another(y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; } root = NULL
  3. 3. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else New printf("nDo you want add Another(y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; } root = NULL
  4. 4. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); 90 else if(root==NULL) p->right=New; root=New; } else New 90 printf("nDo you want add Another(y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; } root = NULL
  5. 5. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else New 90 printf("nDo you want add Another(y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; } root = NULL
  6. 6. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another(y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; }
  7. 7. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another { (y/n)..."); p=NULL; scanf("%c",&ch); y temp=root; }while(ch==y); }
  8. 8. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another (y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New }
  9. 9. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); 60 else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another (y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 }
  10. 10. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another (y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 }
  11. 11. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another (y/n)..."); { temp scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 } p=NULL
  12. 12. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another (y/n)..."); { temp scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 } p=NULL
  13. 13. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 p printf("nDo you want add Another (y/n)..."); { temp scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 }
  14. 14. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) 60<=90 do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 p printf("nDo you want add Another (y/n)..."); { temp scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 }
  15. 15. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 p printf("nDo you want add Another (y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 } temp = NULL
  16. 16. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 p printf("nDo you want add Another (y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; New 60 } temp = NULL
  17. 17. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 p printf("nDo you want add Another (y/n)..."); { scanf("%c",&ch); p=NULL; }while(ch==y); temp=root; 60 }
  18. 18. void create() while(temp!=NULL){ { char ch; p=temp; root=NULL; if(New->data<=temp->data) do temp=temp->left; { else New=(struct node *)malloc(sizeof(struct temp=temp->right; node)); } New->left=New->right=NULL; if(New->data<=p->data) printf("nEnter the Element..."); p->left=New; scanf("%d",&New->data); else if(root==NULL) p->right=New; root=New; } else root 90 printf("nDo you want add Another { (y/n)..."); p=NULL; scanf("%c",&ch); n temp=root; 60 }while(ch==y); }
  19. 19. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); 70 if(root==NULL) root=New; root 90 else { p=NULL; 60 temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  20. 20. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else { p=NULL; 60 temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  21. 21. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else temp { p=NULL; 60 temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  22. 22. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else temp { p=NULL; 60 temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  23. 23. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 p else temp { p=NULL; 60 temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  24. 24. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 p else { p=NULL; 60 temp temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  25. 25. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 p else { p=NULL; 60 temp temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  26. 26. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else { p=NULL; 60 temp p temp=root; while(temp!=NULL) New 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  27. 27. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else { p=NULL; 60 p temp=root; while(temp!=NULL) New 70 { p=temp; temp = NULL if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  28. 28. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else { p=NULL; 60 p temp=root; while(temp!=NULL) New 70 { p=temp; temp = NULL if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  29. 29. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else { p=NULL; 60 p temp=root; while(temp!=NULL) New 70 { p=temp; temp = NULL if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  30. 30. void insert() if(New->data<=p->data){ p->left=New; New=(struct node *)malloc(sizeof(struct else node)); p->right=New; New->left=New->right=NULL; } printf("nEnter the Element..."); } scanf("%d",&New->data); if(root==NULL) root=New; root 90 else { p=NULL; 60 temp=root; while(temp!=NULL) 70 { p=temp; if(New->data<=temp->data) temp=temp->left; else temp=temp->right; }
  31. 31. void del() if(p==NULL){ { struct node *p,*pp,*s,*ps,*child; printf("n%d is not found in the Tree",item); if(root==NULL) return; { } printf("nTree is Empty..."); return; if(p->left!=NULL&&p->right!=NULL) } { printf("nEnter the data to be deleted..."); ps=p; s=p->right; scanf("%d",&item); while(s->left!=NULL) . pp=NULL; { p=root; ps=s; s=s->left; while(p!=NULL && p->data!=item) } { root 90 pp=p; if(item<p->data) p=p->left; 60 else p=p->right; 70 }
  32. 32. void del() if(p==NULL){ { struct node *p,*pp,*s,*ps,*child; printf("n%d is not found in the Tree",item); if(root==NULL) return; { } printf("nTree is Empty..."); return; if(p->left!=NULL&&p->right!=NULL) } { printf("nEnter the data to be ps=p; s=p->right; deleted..."); while(s->left!=NULL) . scanf("%d",&item); 90 { pp=NULL; ps=s; s=s->left; p=root; } while(p!=NULL && p->data!=item) root p 90 { pp=p; if(item<p->data) 60 p=p->left; else 70 p=p->right; } pp = NULL
  33. 33. void del() if(p==NULL){ { struct node *p,*pp,*s,*ps,*child; printf("n%d is not found in the Tree",item); if(root==NULL) return; { } printf("nTree is Empty..."); return; if(p->left!=NULL&&p->right!=NULL) } { printf("nEnter the data to be deleted..."); ps=p; s=p->right; scanf("%d",&item); while(s->left!=NULL) . pp=NULL; { p=root; ps=s; s=s->left; while(p!=NULL && p->data!=item) } { root 90 p pp=p; if(item<p->data) p=p->left; 60 else p=p->right; 70 } pp = NULL
  34. 34. void del() if(p==NULL){ { struct node *p,*pp,*s,*ps,*child; printf("n%d is not found in the Tree",item); if(root==NULL) return; { } printf("nTree is Empty..."); return; if(p->left!=NULL&&p->right!=NULL) } { printf("nEnter the data to be deleted..."); ps=p; s=p->right; scanf("%d",&item); while(s->left!=NULL) . pp=NULL; { p=root; ps=s; s=s->left; while(p!=NULL && p->data!=item) } { root 90 p pp=p; if(item<p->data) p=p->left; 60 else p=p->right; 70 } pp = NULL
  35. 35. void del() if(p==NULL){ { struct node *p,*pp,*s,*ps,*child; printf("n%d is not found in the Tree",item); if(root==NULL) return; { } printf("nTree is Empty..."); return; if(p->left!=NULL&&p->right!=NULL) } { printf("nEnter the data to be deleted..."); ps=p; s=p->right; scanf("%d",&item); while(s->left!=NULL) . pp=NULL; { p=root; ps=s; s=s->left; while(p!=NULL && p->data!=item) } { root 90 p pp=p; if(item<p->data) p=p->left; 60 else p=p->right; 70 } pp = NULL
  36. 36. p->data=s->data; free(p); p=s; printf("n%d is deleted from Tree...",item); pp=ps; }}if(p->left!=NULL)child=p->left;else if(p->right!=NULL)child=p->right;elsechild=NULL;if(p==root)root=child;else root 90 p{ if(p==pp->left) pp->left=child; 60 else pp->right=child; 70} pp = NULL
  37. 37. p->data=s->data; free(p); p=s; printf("n%d is deleted from Tree...",item); pp=ps; }}if(p->left!=NULL)child=p->left;else if(p->right!=NULL)child=p->right;elsechild=NULL;if(p==root)root=child;else root 90 p{ if(p==pp->left) pp->left=child; child 60 else pp->right=child; 70} pp = NULL
  38. 38. p->data=s->data; free(p); p=s; printf("n%d is deleted from Tree...",item); pp=ps; }}if(p->left!=NULL)child=p->left;else if(p->right!=NULL)child=p->right;elsechild=NULL;if(p==root)root=child;else root 90 p{ if(p==pp->left) pp->left=child; child 60 else pp->right=child; 70} pp = NULL
  39. 39. p->data=s->data; free(p); p=s; printf("n%d is deleted from Tree...",item); pp=ps; }}if(p->left!=NULL)child=p->left;else if(p->right!=NULL)child=p->right;elsechild=NULL;if(p==root)root=child;else 90 p{ if(p==pp->left) pp->left=child; child 60 root else pp->right=child; 70} pp = NULL
  40. 40. p->data=s->data; free(p); p=s; printf("n%d is deleted from Tree...",item); pp=ps; }}if(p->left!=NULL)child=p->left;else if(p->right!=NULL)child=p->right;elsechild=NULL;if(p==root)root=child;else{ if(p==pp->left) pp->left=child; 60 root else pp->right=child; 70}

×