Question
Use the following list of integers in the order given to answer questions 1 and 2 75,99,34,12,56,34,10,100,83,79,90,2,25 Start with an empty binary search tree and insert the integers in the order given. Start with an empty heap and then insert the list of integers in the order given. The root is the minimum. Show the heap that you created for Question 2 after the following actions: Insert 50 Delete min Insert 3 Delete min
Explanation / Answer
#include #include #include struct searchtree{ int element; struct searchtree *left,*right; }*root; typedef struct searchtree *node; typedef int ElementType; node insert(ElementType, node); node delet(ElementType, node); void main(){ int ch; ElementType a; node temp; makeempty(); while(1){ printf(" 1. Insert 2. Delete 3. Exit Enter Your Choice: "); scanf("%d",&ch); switch(ch){ case 1: printf("Enter an element : "); scanf("%d", &a); root = insert(a, root); break; case 2: printf(" Enter the element to delete : "); scanf("%d",&a); root = delet(a, root); break; case 3:exit(0); default: printf("Invalid Choice"); } } } node insert(ElementType x,node t){ if(t==NULL){ t = (node)malloc(sizeof(node)); t->element = x; t->left = t->right = NULL; } else{ if(x element)t->left = insert(x, t->left); else if(x > t->element)t->right = insert(x, t->right); }return t; node delet(ElementType x,node t){ node temp; if(t == NULL) printf(" Element not found"); else{ if(x element)t->left = delet(x, t->left); else if(x > t->element)t->right = delet(x, t->right); else{ if(t->left && t->right){ temp = findmin(t->right); t->element = temp->element; t->right = delet(t->element,t->right); } else if(t->left == NULL)t=t->right;elset=t->left; } }return t;}