Dsa Lab12
Dsa Lab12
struct node{
int info;
struct node *left;
struct node *right;
};
//deletion cases
//case a (node to be deleted is a leaf node)
struct node* case_a(struct node* root, struct node* par, struct node* ptr){
if (par == NULL){
root = NULL;
}
else if (ptr == par->left){
par->left = ptr->left;
}
else {
par->right = ptr->right;
}
free(ptr);
return root;
}
if (par == NULL){
root = child;
}
else if (ptr == par->left){
par->left = child;
}
else {
par->right = child;
}
free(ptr);
return root;
}
//node as 2 child
else if (ptr->left != NULL && ptr->right != NULL){
root = case_c(root, par, ptr);
}
//node has 1 children
else if (ptr->left != NULL || ptr->right != NULL){
root = case_b(root, par, ptr);
}
//node has no children
else {
root = case_a(root, par, ptr);
}
return root;
}
int main(){
int data, ch, i, n;
while (1)
printf("\n3.Preorder\n4.Exit");
scanf("%d", &ch);
switch (ch)
case 1:
printf("\nEnter N value: ");
scanf("%d", &n);
scanf("%d", &data);
break;
case 2:
printf("\nEnter the element to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 3:
printf("\nPreorder Traversal: \n");
preorderPrint(root);
break;
case 4:
exit(0);
default:
printf("\nWrong option");
break;
}
}
return 0;
}