Вы находитесь на странице: 1из 13

Assignment

CAP-619

ADS
Submitted to: Respected Preeti Maam Submitted by: Ankur Singh Roll no: - B31

Course Code:CAP619 HOMEWORK NO:1 1. Explain with example that the complexity of BST is less than the complexity of Binary tree. Ans 1 DEF: A binary tree in which the nodes are labeled with elements of an ordered dynamic set and the following BST property is satisfied: all elements stored in the left subtree of any node x are less than the element stored at x and all elements stored in the right subtree of x are greater than the element at x. A prominent data structure used in many systems programming applications for representing and managing dynamic sets. Average case complexity of Search, Insert, and Delete Operations is O(log n), where n is the number of nodes in the tree. Binary search trees are one of the most fundamental data structures. While the height of such a tree may be linear in the worst case, the average height with respect to the uniform distribution is only logarithmic. The exact value is one of the best studied problems in average-case complexity. We investigate what happens in between by analysing the smoothed height of binary search trees: Randomly perturb a given (adversarial) sequence and then take the expected height of the binary search tree generated by the resulting sequence. As perturbation models, we consider partial permutations, partial alterations, and partial deletions. On the one hand, we prove tight lower and upper bounds of roughly @Q((1-p)@?n/p) for the expected height of binary search trees under partial permutations and partial alterations, where n is the number of elements and p is the smoothing parameter. This means that worst-case instances are rare and disappear under slight perturbations

2. Create a C program to implement Selection sort, Bubble sort and insertion sort in one program using menu by asking choice of type of sorting from the user. Take the numbers to sort from the user. Ans 2 #include<stdio.h> #include<conio.h> void accept(int Arr[], int s); void display(int Arr[], int s); void isort(int Arr[], int s); void ssort(int Arr[], int s); void bsort(int Arr[],int s); int main() { int Arr[100],n,choice; printf("Enter Size of Array "); scanf("%d",&n); do { printf("\nMENU"); printf("\n1. Accept elements of array"); printf("\n2. Display elements of array"); printf("\n3. Sort the array using insertion sort method"); printf("\n4. Sort the array using selection sort method"); printf("\n5. Sort the array using bubble sort method"); printf("\n6. Exit"); printf("\n\nEnter your choice 1-6 :"); scanf("%d",&choice); switch(choice) { case 1: accept(Arr,n); break; case 2: display(Arr,n); break; case 3: isort(Arr,n); break; case 4: ssort(Arr,n); break;

case 5: bsort(Arr,n); break; case 6: break; default:cout<<"\nInvalid choice"; } }while(choice!=6); return 0; } void accept(int Arr[], int s) { for(int I=0;I<s;I++) { printf("Enter element %d : "I+1); scanf("%d",&Arr[I]); } } void display(int Arr[], int s) { printf("The elements of the array are:\n"); for(int I=0;I<s;I++) Printf("%d",Arr[I]); } void isort(int Arr[], int s) { int I,J,Temp; for(I=1;I<s;I++) { Temp=Arr[I]; J=I-1; while((Temp<Arr[J]) && (J>=0)) { Arr[J+1]=Arr[J]; J--; }

Arr[J+1]=Temp; } } void ssort(int Arr[], int s) { int I,J,Temp,Small; for(I=0;I<s-1;I++) { Small=I; for(J=I+1;J<s;J++) //finding the smallest element if(Arr[J]<Arr[Small]) Small=J; if(Small!=I) { Temp=Arr[I]; //Swapping Arr[I]=Arr[Small]; Arr[Small]=Temp; } } } void bsort(int Arr[],int s) { int I,J,Temp; for(I=0;I<s-1;I++) //sorting { for(J=0;J<(s-1-I);J++) if(Arr[J]>Arr[J+1]) { Temp=Arr[J]; Arr[J]=Arr[J+1]; Arr[J+1]=Temp; } } }

//swapping

3. Convert A to S alphabets in Red Black Tree. Ans 3

4. Give the algorithm to explain all cases of deletion in BST. Ans 4 TREE-MINIMUM (x) while left[x] NIL do x left [x] return x TREE-MAXIMUM (x) while right[x] NIL do x right [x] return x TREE-SUCCESSOR (x) if right [x] NIL then return TREE-MINIMUM (right[x]) else y p[x] while y NIL .AND. x = right[y] do xy y p[y] return y TREE-DELETE (T, z) if left [z] = NIL .OR. right[z] = NIL then y z else y TREE-SUCCESSOR (z) if left [y] NIL then x left[y] else x right [y] if x NIL then p[x] p[y] if p[y] = NIL then root [T] x else if y = left [p[y]] then left [p[y]] x else right [p[y]] x

if y z then key [z] key [y] if y has other field, copy them, too return y 5. Compare Binary search tree, AVL tree and Red Black Trees correlating their applications, advantages, disadvantages, complexities etc. Ans 5 BINARY SEARCH TREE Advantages: 1. BST is fast in insertion and deletion etc when balanced. 2. Very efficient and its code is easier than link lists. Disadvantages: 1. Shape of the tree depends upon order of insertion and it can be degenerated. 2. Searching takes long time. AVL 1. The advantage of an AVL tree is that it is always balanced, guaranteeing the O (log n) speed of the Binary Search algorithm. 1. The disadvantages the complex rotations used by the insertion and removal algorithms needed to maintain the tree's balance. Red Black Tree Advantages: 1. Fast under both low and high CPU load. Disadvantages: 1. Nodes are smaller but more prolific since there is one node for every key in the tree. 2. Rebalancing the tree requires touching more adjacent nodes to rearrange and recolor nodes, which lowers performance.

6. Explain the concept of deletion in AVL tree. Ans 6 1. Reduce the problem to the case when the node x to be deleted has at most one child. 2. Delete x. We use a Boolean variable shorter to show if the height of a subtree has been shortened. 3. While shorter is TRUE do the following steps for each node p on the path from the parent of x to the root of the tree. When shorter becomes FALSE, the algorithm terminates. Case 1: Node p has balance factor equal. Case 2: The balance factor of p is not equal, and the taller subtree was shortened. Case 3: The balance factor of p is not equal, and the shorter subtree was shortened. Apply a rotation as follows to restore balance. Let q be the root of the taller subtree of p. Case 3a: The balance factor of q is

int avl_delete_NOT(node *treep, value_t target) { node tree = *treep; while (tree && tree->value != target) { direction dir = (target > value);

treep = &tree->next[dir]; tree = *treep; } if (!tree) return 0; if (tree->next[LEFT] == NULL) *treep = tree->next[RIGHT]; else if (tree->next[RIGHT] == NULL) *treep = tree->next[LEFT]; else MAGIC(); free(tree); return 1; }

Вам также может понравиться