Sei sulla pagina 1di 15

BINARY SEARCH TREE

AIM: To implement binary search tree using linked list and possible operations on binary search trees.

ALGORITHM:

1. Create the memory space for the root node and initialize the value to zero. 2. Read the value. 3. If the value is less than the root value, it is assigned as the left child of the root. 4. Else if new value is greater than the root value, it is assigned as the right child of the root. 5. Else if there is no value in the root, the new value is assigned as the root. 6. The step (2) and (3) is repeated to insert the n number of values.

SEARCH OPERATION 1. Read the value to be searched. 2. Check whether the root is not null. 3. If the value to be searched is less than the root, consider the left sub-tree for searching the particular element else if the value is greater than the root particular element else if the value is equal then return the value that is the value which was searched. INSEARTION 1. Read the value to be inserted. 2. First perform the search operation to check whether the key values is different from those existing element. 3. If the search is unsuccessful, then the key is inserted at the point the search is terminated. DELETION 1. Read the key value to be deleted. 2. First perform search operation to get that particular key element. 3. If it is, check whether a. it is leaf node. b. or it has only one sub-tree

c. or it has exactly 2 sub-trees 4. If the key value is the leaf-node, assign null value to that node. 5. Else if the key contains only one sub tree either left (or) right sub-tree. 6. If the key is root, it is discarded and the root its single sub-tree becomes the new search tree root. 7. Else if the key is the child node, then we change the pointer from the root of key to the child of the key. 8. If the key contain both left and right sub-tree replace the key with either largest element is the left sub-tree or smallest element is the right sub-tree. PROGRAM FOR BINARY SEARCH TREE #include<stdio.h> struct BT { int data; struct BT *right,*left; }; void insert(struct BT ** ptr,int d) { if((*ptr)==NULL) { (*ptr)=(struct BT*)malloc(sizeof(struct BT)); (*ptr)->data=d; (*ptr)->left=(*ptr)->right=NULL; } else { if((*ptr)->data>d) insert(&((*ptr)->left),d); else insert(&((*ptr)->right),d); } return; } int search(struct BT *ptr,int no) { if(ptr==NULL) return(0); if(ptr->data==no) return(1); if(ptr->data>no) return(search(ptr->left,no)); else return(search(ptr->right,no)); }

void inorder(struct BT *ptr) { if(ptr==NULL) return; else { inorder(ptr->left); printf("\t%d",ptr->data); inorder(ptr->right); } } void preorder(struct BT*ptr) { if(ptr==NULL) return; else { printf("\t%d",ptr->data); preorder(ptr->left); preorder(ptr->right); } } void postorder(struct BT*ptr) { if(ptr==NULL) return; else { postorder(ptr->left); postorder(ptr->right); printf("\t%d",ptr->data); } } main() { struct BT*root; int ch,d,no,f; clrscr(); root=NULL; while(ch!=6) { printf("\n 1.Insert\n 2.Search\n 3.Inorder\n 4.Preorder\n 5.Postorder\n 6.Exit\n"); printf("\n Enter the choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the data:"); scanf("%d",&d); insert(&root,d);

break; case 2: printf("Enter the node:"); scanf("%d",&no); f=search(root,no); if(f==0) printf("Node is not present"); else printf("Node is present"); break; case 3: inorder(root); break; case 4: preorder(root); break; case 5: postorder(root); break; case 6: break; } } getch(); } OUTPUT: 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:1 Enter the data:11 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:1 Enter the data:10 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:1 Enter the data:14

1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:1 Enter the data:15 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:1 Enter the data:9 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:2 Enter the node:10 Node is present 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:3 9 10 11 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:4 11 10 9 1.Insert

14

15

14

15

2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:5 9 10 15 1.Insert 2.Search 3.Inorder 4.Preorder 5.Postorder 6.Exit Enter the choice:6 RESULT: Thus the Binary search tree is implemented and possible operations are performed successfully. PRIORITY QUEUE USING BINARY HEAPS

14

11

AIM: To write a C program to implement priority Queue using Binary Heaps.

ALGORITHM:

1. Initialize all necessary variables and functions. 2. Read the choices. 3. For insertion, read the element to be inserted. 4. If root is NULL, assign the given element as root. 5. If the element is equal to the root, print Duplicate Value. 6. Else if element value is less than the root value, insert element at the left of the root. 7. Else insert right side of the root. 8. For deletion, get the priority for maximum or minimum. 9. If maximum, it deletes the root and rearranges the tree. 10. If minimum, it deletes the leaf. 11. End of the program. PROGRAM FOR PRIORITY QUEUE USING BINARY HEAP

#include<stdio.h> #include<conio.h> #include <stdlib.h> enum {FALSE=0,TRUE=-1}; struct Node { struct Node *Previous; int Data; struct Node *Next; }Current; struct Node *head; struct Node *ptr; static int NumOfNodes; int PriorityQueue(void); int Maximum(void); int Minimum(void); void Insert(int); int Delete(int); void Display(void); int Search (int); void main() { int choice; int DT; clrscr(); PriorityQueue(); while(1) { printf("\nEnter ur Choice:"); printf("\n1.Insert\n2.Display\n3.Delete\n4.Search\n5.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter a data to enter Queue"); scanf("%d",&DT); Insert(DT); break; case 2: Display(); break; case 3: { int choice,DataDel; printf("\nEnter ur choice:"); printf("\n1.Maximum Priority queue\n2.Minimum priority Queue\n"); scanf("%d",&choice);

switch(choice) { case 1: DataDel=Maximum(); Delete(DataDel); printf("\n%d is deleted\n",DataDel); break; case 2: DataDel=Minimum(); Delete(DataDel); printf("\n%d is deleted\n",DataDel); break; default: printf("\nSorry Not a correct Choice\n"); } } break; case 4: printf("\nEnter a data to Search in Queue:"); scanf("%d",&DT); if(Search(DT)!=FALSE) printf("\n %d is present in queue",DT); else printf("\n%d is not present in queue",DT); break; case 5: exit(0); default: printf("\nCannot process ur choice\n"); } } } int PriorityQueue(void) { Current.Previous=NULL; printf("\nEnter first element of Queue:"); scanf("%d",&Current.Data); Current.Next=NULL; head=&Current; ptr=head; NumOfNodes++; return; } int Maximum(void) { int Temp; ptr=head; Temp=ptr->Data; while(ptr->Next!=NULL)

{ if(ptr->Data>Temp) Temp=ptr->Data; ptr=ptr->Next; } if(ptr->Next==NULL && ptr->Data>Temp) Temp=ptr->Data; return(Temp); } int Minimum(void) { int Temp; ptr=head; Temp=ptr->Data; while(ptr->Next!=NULL) { if(ptr->Data<Temp) Temp=ptr->Data; ptr=ptr->Next; } if(ptr->Next==NULL && ptr->Data<Temp) Temp=ptr->Data; return(Temp); } void Insert(int DT) { struct Node *newnode; newnode=(struct Node *)malloc(sizeof(struct Node)); newnode->Next=NULL; newnode->Data=DT; while(ptr->Next!=NULL) ptr=ptr->Next; if(ptr->Next==NULL) { newnode->Next=ptr->Next; ptr->Next=newnode; } NumOfNodes++; } int Delete(int DataDel) { struct Node *mynode,*temp; ptr=head; if(ptr->Data==DataDel) { temp=ptr; ptr=ptr->Next; ptr->Previous=NULL; head=ptr;

NumOfNodes--; return(TRUE); } else { while(ptr->Next->Next!=NULL) { if(ptr->Next->Data==DataDel) { mynode=ptr; temp=ptr->Next; mynode->Next=mynode->Next->Next; mynode->Next->Previous=ptr; free(temp); NumOfNodes--; return(TRUE); } ptr=ptr->Next; } if(ptr->Next->Next==NULL && ptr->Next->Data==DataDel) { temp=ptr->Next; free(temp); ptr->Next=NULL; NumOfNodes--; return(TRUE); } } return(FALSE); } int Search(int DataSearch) { ptr=head; while(ptr->Next!=NULL) { if(ptr->Data==DataSearch) return ptr->Data; ptr=ptr->Next; } if(ptr->Next==NULL && ptr->Data==DataSearch) return ptr->Data; return(FALSE); } void Display(void) { ptr=head; printf("\nPriority Queue is as Follows:-\n"); while(ptr!=NULL) { printf("\t\t%d",ptr->Data);

ptr=ptr->Next; } } OUTPUT:

Enter first element of queue: 3 Enter ur Choice: 1.Insert 2.Display 3.Delete 4.Search 5.Exit 1 Enter a data to enter Queue11 Enter ur Choice: 1.Insert 2.Display 3.Delete 4.Search 5.Exit 2 Priority Queue is as Follows:3 11 Enter ur Choice: 1.Insert 2.Display 3.Delete 4.Search 5.Exit 3 Enter ur choice: 1.Maximum Priority queue 2.Minimum priority Queue 1 11 is deleted Enter ur Choice: 1.Insert 2.Display 3.Delete 4.Search 5.Exit 4 Enter a data to Search in Queue:3

3 is present in queue Enter ur Choice: 1.Insert 2.Display 3.Delete 4.Search 5.Exit 5 RESULT: Thus the priority queue using binary heap is implemented and the result is verified successfully. HASHING WITH OPEN ADDRESSING (LINEAR PROBING)

AIM: To write a C program to implement hashing with open addressing (Linear probing).

ALGORITHM:

1. Initialize all necessary variables and functions. 2. Read the number to be inserted in the hash table. 3. Find the location to insert using num % size. 4. If any value is present at that position then find the next unoccupied location. 5. Else if key value is greater than the maximum table size then print Hash Table is full. 6. Repeat the steps 1 to 5 to insert more values. 7. End of the program. PROGRAM FOR HASH TABLE WITH OPEN ADDRESSING #include<stdio.h> #include<conio.h> #include<stdlib.h> #define max 10 void main() { int a[max],num,key,i; char ans; int create(int); void hash(int [],int,int); void display(int[]);

clrscr(); printf("\nhash table\n"); for(i=0;i<max;i++) a[i]=-1; do { printf("\n enter the number:"); scanf("%d",&num); key=create(num); hash(a,key,num); printf("\ndo u want to continue"); ans=getche(); }while(ans=='y'); display(a); getch(); } int create(int num) { int key; key=num % 10; return key; } void hash(int a[max],int key,int num) { int flag,i,count=0; void display(int a[]); flag=0; if(a[key]==-1) a[key]=num; else { i=0; while(i<max) { if(a[i]!= -1) count++; i++; } if(count==max) { printf("\n hashtable is full"); display(a); getch(); exit(1); } for(i=key+1;i<max;i++) if(a[i]==-1) { a[i]=num; flag=1;

break; } for(i=0;i<key && flag==0;i++) if(a[i]==-1) { a[i]=num; flag=1; break; } } } void display(int a[max]) { int i; printf("\nThe hash table is..........\n"); for(i=0;i<max;i++) printf("\n%d %d",i,a[i]); } OUTPUT: hash table enter the number:131 do u want to continue y enter the number:21 do u want to continue y enter the number:3 do u want to continue y enter the number:4 do u want to continue y enter the number:5 do u want to continue n The hash table is.......... 0 1 2 3 4 5 6 7 8 9 -1 131 21 3 4 5 -1 -1 -1 -1

RESULT: Thus the program for Hashing using open addressing (Linear probing) is implemented successfully.

Potrebbero piacerti anche