Can anyone see why the delete function is returning a segmentation fault in this code??

the values "d", "b", "f", "a", "c", "e", "g" are being insterted and printed correctly from a seperate source file. as soon as i try to delete a node i cant print or do anything.

thanks

Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include "bst.h" #include "mylib.h" struct bstnode { char *key; bst left; bst right; }; bst bst_new(){ return NULL; } bst bst_insert(bst b, char *key){ if (b == NULL) { b = emalloc(sizeof *b); b -> key = key; b -> left = NULL; b -> right = NULL; } else if (strcmp(key, b -> key) < 1) { b -> left = bst_insert(b -> left, key); } else { b -> right = bst_insert(b -> right, key); } return b; } char *bst_search(bst b, char *key){ if (b == NULL) { return NULL; } else if (b -> key == key) { return b -> key; } else if (strcmp(key, b -> key) > 0) { return bst_search(b -> left, key); } else { return bst_search(b -> right, key); } } void bst_inorder(bst b, void f(char *s)){ if (b == NULL) { return; } else { bst_inorder(b -> left, f); f(b -> key); bst_inorder(b -> right, f); } } void bst_preorder(bst b, void f(char *s)){ if (b == NULL) { return; } else { f(b -> key); bst_preorder(b -> left, f); bst_preorder(b -> right, f); } } bst bst_delete(bst b, char *key){ int x = strcmp(key, b->key); printf("%d\n", x); if (x<0) { return bst_delete(b->left, key); } else if (x > 0) { return bst_delete(b->right, key); } else { if (b->left == NULL && b->right == NULL) { b->key = NULL; b->left = NULL; b->right = NULL; return b; } else if (b->left == NULL) { b->key = b->right->key; b->right = b->right->right; b->left = b->right->left; return b; } else if (b->right == NULL) { b->key = b->left->key; b->right = b->left->right; b->left = b->left->left; return b; } else { bst b2 = b->right; while (b2->left != NULL) { b2=b2->left; } b->key = b2->key; return bst_delete(b2, b2->key); } } }