Question
#include #include struct tree { int data; struct tree *left; struct tree *right; }*temp, *succ; struct tree *newnode(int x) { struct tree *newnode = malloc(sizeof(struct
#include
struct tree *newnode(int x) { struct tree *newnode = malloc(sizeof(struct tree)); newnode -> data = x; newnode -> left = NULL; newnode -> right = NULL; return newnode; } void BST_search(struct tree *root, int x) { if(root == NULL) { printf("Element not present "); } else if(x < root -> data ) { BST_search(root -> left, x); } else if(x > root -> data ) { BST_search(root -> right, x); } else if(x == root -> data) { printf("Element found "); } } void inorder(struct tree* root) { if(root != NULL) { inorder(root -> left); printf("%d ", root -> data); inorder(root -> right); } } struct tree *insert_iteration(struct tree *root , int x) { struct tree *temp = newnode(x), *parent = NULL, *curr = root;
while(curr != NULL) { parent = curr; if(curr -> data > x) curr = curr -> left; else if(curr -> data < x) curr = curr -> right; else return root; } if(parent == NULL) return temp; if(parent -> data > x) parent -> left = temp;
else parent -> right = temp; return root; } struct tree *getsuccessor(struct tree *curr) { curr = curr -> right; while(curr != NULL && curr -> left != NULL) curr = curr -> left; return curr;
}
struct tree *delnode(struct tree *root, int x) { if(root == NULL) return root; if(x > root -> data) root -> right = delnode(root -> right, x); else if(x < root -> data) root -> left = delnode(root -> left, x); else { if(root -> left == NULL) { temp = root -> right; free(root); return temp; } else if(root -> right == NULL) { temp = root -> left; free(root); return temp; } else { succ = getsuccessor(root); root -> data = succ -> data; root -> right = delnode(root -> right, succ -> data); } }
return root; } void predeccessor(struct tree *root, struct tree *pred, int x) { if(root == NULL) { pred = NULL; return; }
if(root -> data == x) { if(root -> left != NULL) { struct tree* tmp = root->left; while (tmp->right) { tmp = tmp->right; } pred = tmp ; } } else if(root -> data > x) { predeccessor(root -> left,pred, x); } else { pred = root; predeccessor(root -> right,pred, x); } printf("%d ", pred -> data); } struct tree *Min(struct tree *curr) { while(curr -> left != NULL) { curr = curr -> left; } return curr; }
struct tree *Max(struct tree *curr) { while(curr -> right != NULL) { curr = curr -> right; } return curr; } int main () { struct tree *root, *t; root = newnode(18); root -> left = newnode(9); root -> right = newnode(23); root -> left -> left = newnode(7); root -> left -> right = newnode(15); root -> right -> left = newnode(19); root -> right -> right = newnode(20); root = insert_iteration(root ,35); printf("The inorder is :"); inorder(root); printf(" "); struct tree *pred = NULL; printf("the predeccesoor of root :"); predeccessor(root, pred,18); printf(" "); printf("the max element is :"); printf("%d ",Max(root)->data);
printf("the min element is :"); printf("%d",Min(root)->data); return 0; } EXPLAIN THE CODE PROPERLY AND ALSO LOGIC BEHIND IT
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started