Documente Academic
Documente Profesional
Documente Cultură
1. Write a program for implementation of Fabonacii Series. 2. Write a program for displaying following pattern. 1 4 7 8 9 6 16 9
4. Write a program for implementation of stack using push ( ) and pop( ) methods. 5. Write a program to implement. 6. Write a program for implementation of 7. Write a program for implementation of binary search. 8. Write a program to perform Quick sort. 9. Write a program to perform the following operations in 10. Write a program for implementation of singly linked list 11. Write a program to perform preorder, inorder and post order traversal of binary search tree. 12. Write a program to solve postfix expression using stack.
Theory:Infix Expression: The arithmetic is an expression in which opearator is present in between two operands called as Infix Expressions. Ex :( a*b) Postfix Expression: The arithmetic is an expression in which operator is present after operands called as Postfix Expression. Ex: ab* Algorithms:-
Infixtopostfix(P,I ):
Description: Here I is an arithmetic expression written in infix notation and P is the equivalent postfix expression generated by this algorithm.
1. Push ( left parenthesis onto stack. 2. Add ) right parenthesis to the end of expression I. 3. Scan I from left to right and repeat step 4 for each element of I until the stack becomes empty. 4. If the scanned element is: (a) an operand then add it to P. (b) a left parenthesis then push it onto stack. (c) an operator then: (i) Pop from stack and add to P each operator which has the same or higher precedence then the scanned operator. (ii) Add newly scanned operator to stack. (d) a right parenthesis then: (i) Pop from stack and add to P each operator
until a left parenthesis is encountered. (ii) Remove the left parenthesis. [End of Step 4 If][End of step 3 For Loop] 5. Exit. Program code:#include<stdio.h> #define SIZE 50 #include <ctype.h> char s[SIZE]; int top = -1; /* Global declarations */ /* Size of Stack */
int pr(char elem) { /* Function for precedence */ switch (elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } }
main() { /* Main Program */ char infx[50], pofx[50], ch, elem; int i = 0, k = 0; printf("\n\nRead the Infix Expression ? "); scanf("%s", infx); push('#'); while ((ch = infx[i++]) != '\0') { if (ch == '(') push(ch); else if (isalnum(ch)) pofx[k++] = ch; else if (ch == ')') { while (s[top] != '(') pofx[k++] = pop(); elem = pop(); /* Remove ( */ } else { /* Operator */ while (pr(s[top]) >= pr(ch)) pofx[k++] = pop(); push(ch); } } while (s[top] != '#') /* Pop from stack till empty */ pofx[k++] = pop(); pofx[k] = '\0'; /* Make pofx as valid string */ printf("\n\nGiven Infix Expn: %s\n\nPostfix Expn: %s\n", infx, pofx); }
Output:-
Assignments: i)Write a program for infix expression:(a*b)+c/e. Viva Questions: 1)What are the steps to convert infix expression to equivalent postfix expression? 2)Which data structure used for this purpose? 3)How to decide precedence of operators?
PROGRAM:
#include<stdio.h> #include<stdlib.h> #include<conio.h> typedef struct node *pos; typedef struct node *avltree; avltree t; struct node { int element;
avltree left; avltree right; int height; }; avltree insert(int,avltree); int max(int,int); int height(pos p); pos singlerotatewithleft(pos); pos singlerotatewithright(pos); pos doublerotatewithleft(pos); pos doublerotatewithright(pos); void display(avltree); void main() { int ch,ele; clrscr(); t=NULL; do { printf("\nMENU\n"); printf("\n1. Insertion\n"); printf("\n2. Display\n"); printf("\n3. Exit\n"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch)
{ case 1: printf("\nEnter the element to be inserted:"); scanf("%d",&ele); t=insert(ele,t); printf("\nThe element is inserted"); break; case 2: printf("\nThe elements in the AVL TREE are:"); display(t); break; case 3: exit(0); break; } }while(ch<4); getch(); } avltree insert(int x,avltree t) { if(t==NULL) { t=(node *)malloc(sizeof(struct node)); if(t==NULL) exit(0); else { t->element=x;t->height=0; t->left=t->right=NULL;
} }
else if(x<t->element) { t->left=insert(x,t->left); if(height(t->left)-height(t->right)==2) if(x<t->left->element) t=singlerotatewithleft(t); else t=doublerotatewithleft(t); } else if(x>t->element) { t->right=insert(x,t->right); if(height(t->right)-height(t->left)==2) if(x>t->left->element) t=singlerotatewithright(t); else t=doublerotatewithright(t); } t->height=max(height(t->left),height(t->right))+1; return t; }
return p->height; } int max(int p,int q) { if(p>q) return p; else return q; } pos singlerotatewithleft(pos k2) { pos k1; k1=k2->left; k2->left=k1->right; k1->right=k2; k2->height=max(height(k2->left),height(k2->right))+1; k1->height=max(height(k1->left),k2->height)+1; return k1; } pos singlerotatewithright(pos k1)
} pos doublerotatewithleft(pos k3) { k3->left=singlerotatewithright(k3->left); return singlerotatewithleft(k3); } pos doublerotatewithright(pos k1) { k1->right=singlerotatewithleft(k1->right); return singlerotatewithright(k1); } void display(avltree t) { if(t!=NULL) { display(t->left); printf("\n%d",t->element);
display(t->right); } }
OUTPUT:
MENU 1. Insertion 2. Display 3. Exit Enter your choice:1 Enter the element to be inserted:10 The element is inserted MENU 1. Insertion 2. Display 3. Exit Enter your choice:2 The elements in the AVL TREE are:
10
MENU 1. Insertion 2. Display 3. Exit Enter your choice:1 Enter the element to be inserted:20 The element is inserted MENU 1. Insertion 2. Display
RESULT:
Thus the C program to implement AVL tree is executed.
ALGORITHM:
CREATION OF BINARY SEARCH TREE: Step 1: Enter the number of elements in the tree n Step 2: Enter the n elements into the binary search tree Step 3: As each element is put into the tree check whether tree is binary
SEARCH TREE PROPERTY IS MAINTAINED: Step 1: Enter the element to be inserted x Step 2: If x< root, Keep moving towards the left sub tree Step 3: If x>root, Keep moving towards right sub tree Step 4: Perform steps 2 and 3 for the sub trees until the exact location of insertion is Found Step 5: Insert at that location and display the new tree using inorder traversal
DELETION OF A NODE: Step 1: Enter the element to be deleted x Step 2: If X has no children then it can be deleted immediately Step 3: If x has no child then adjust the parents pointer by passing the node Step 4: If the node has two children then replace the data in the node with the minimum Data in the right sub tree of that node and then delete the node recursively Step 5: Display the new tree using inorder traversal
FINDING THE MINIMUM AND MAXIMUM Step 1: More towards the left sub tree to find the minimum Step 2: More towards the right sub tree to find the maximum
PROGRAM :
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct treenode { }; int element; struct treenode *left; struct treenode *right; typedef struct treenode *position,*searchtree; searchtree insert(int x,searchtree t) { if(t==NULL) { t=(struct treenode*)malloc(sizeof(struct treenode)); if(t==NULL) exit(0); else { t->element=x; t->left=t->right=NULL; } } else if(x<t->element) t->left=insert(x,t->left); else if(x>t->element) t->right=insert(x,t->right); return t; } position findmin(searchtree t) { if(t==NULL)
return NULL;
else if(t->left==NULL) return t; else return findmin(t->left); } position findmax(searchtree t) { if(t==NULL) return NULL; else if(t->right==NULL) return t; else return findmax(t->right); }
searchtree rem(int x,searchtree t) { position temp; if(t==NULL) printf("\nElement not found"); else if(x<t->element) t->left=rem(x,t->left); else if(x>t->element) t->right=rem(x,t->right); else if(t->left&&t->right) { temp=findmin(t->right); t->element=temp->element; t->right=rem(t->element,t->right); } else { temp=t; if(t->left==NULL) t=t->right; else if(t->right==NULL) t=t->left; free(temp); }
intrav(head->left); printf("%d\t",head->element); if(head->right!=NULL) intrav(head->right); } void main() { int n,i,dat,ch; searchtree t=NULL; position node; clrscr(); printf("Enter no of elements:\n"); scanf("%d",&n); printf("Enter the elements:\n"); for(i=1;i<=n;i++) { scanf("%d",&dat); t=insert(dat,t); } intrav(t); do { printf("\n\n"); printf("\n ****MENU****\n"); printf("\nEnter 1 -> Insert a node\n"); printf("\n 2 -> Delete a node\n"); printf("\n 3 -> Find Minimum\n"); printf("\n 4 -> Find Maximum\n"); printf("\n 5 -> Display(Inorder Traversal\n"); printf("\n 6 -> Exit\n"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1:printf("\nEnter the element to be inserted:"); scanf("%d",&dat); t=insert(dat,t);
break; case 2:printf("\n Enter the node to be deleted:"); scanf("%d",&dat); t=rem(dat,t); break; case 3:node=findmin(t); printf("\nThe minimum element is %d",node->element); break; case 4:node=findmax(t); printf("\nThe maximum element is %d",node->element); break; case 5:intrav(t); break; case 6:exit(0); } }while(ch!=6); getch(); }
OUTPUT:
Enter no of elements: 3 Enter the elements: 10 6 2
10
3 -> Find Minimum 4 -> Find Maximum 5 -> Display(Inorder Traversal 6 -> Exit Enter your choice:3 The minimum element is 2 ****MENU**** Enter 1 -> Insert a node 2 -> Delete a node 3 -> Find Minimum 4 -> Find Maximum 5 -> Display(Inorder Traversal 6 -> Exit Enter your choice:6
RESULT:
Thus the C program to implement binary search tree is executed.
Practical Number: Aim: - Write a program to perform Quick sort. .Theory:Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists. The steps are: 1. Pick an element, called a pivot, from the list. 2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. 3. Recursively apply the above steps to the sub-list of elements with smaller values and separately the sub-list of elements with greater values. The base case of the recursion are lists of size zero or one, which never need to be sorted.
Complexities:
Worst-case: O(N2) This happens when the pivot is the smallest (or the largest) element. Then one of the partitions is empty, and we repeat recursively the procedure for N-1 elements. Best-case:O(NlogN) The best case is when the pivot is the median of the array, and then the left and the right part will have same size. There are logN partitions, and to obtain each partitions we do N comparisons (and not more than N/2 swaps). Hence the complexity is O(NlogN) Average Case:O(NlogN)
Algorithms:
Quick Sort ( A, BEG, END ): Description: Here A is an unsorted array. BEG is the lower bound and END is the upper bound. 1. If (BEG < END) Then 2. X = Partition (A, BEG, END) 3. Call Quick Sort (A, BEG, X 1) 4. Call Quick Sort (A, X + 1, END) [End of If] 5.Exit
Partition ( A, BEG, END ): Description: Here A is an unsorted array. BEG is the lower bound, END is the upper bound. 1. Set LOC = BEG 2. Repeat While (True) 3. Repeat While (A[LOC] <= A[END]) and (LOC != END) 4. END = END 1 [End of While Loop] 5. If (LOC == END) Then 6. Return LOC [End of If] 7. Interchange A[LOC] and A[END] 8. Set LOC = END 9. Repeat While (A[LOC] >= A[BEG]) and (LOC != BEG) 10. BEG = BEG + 1 [End of While Loop] 11. If (LOC == BEG) Then 12. Return LOC [End of If] 13. Interchange A[LOC] and A[BEG] 14. Set LOC = BEG [End of While Loop] 15.Exit.
Program code:#include<stdio.h> #include<conio.h> void quicksort(int [10],int,int); void main(){ int x[20],size,i; printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size);
for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); getch(); }
} }
} }
Output:-
Assignments:
i)Write a program to sort 15 elements using quick sort.
Viva Questions: 1. What is logic for quick sort? 2. How it is efficient? 3. Which Data Structure is used to perform quick sort?
first=node; } else { temp=first; if(p==1) { node->next=first; first=node; } else { while(temp->next!=NULL && i!=p-1) { temp=temp->next; i++; } node->next=temp->next; temp->next=node; } }
return(first); }