******** fig4.25 ********** tree_ptr delete( element_type x, SEARCH_TREE T ) { tree_ptr tmp_cell, child; if( T == NULL ) error("Element not found"); else if( x < T->element ) /* Go left */ T->left = delete( x, T->left ); else if( x > T->element ) /* Go right */ T->right = delete( x, T->right ); else /* Found element to be deleted */ if( T->left && T->right ) /* Two children */ { /* Replace with smallest in right subtree */ tmp_cell = find_min( T->right ); T->element = tmp_cell->element; T->right = delete( T->element, T->right ); } else /* One child */ { tmp_cell = T; if( T->left == NULL ) /* Only a right child */ child = T->right; if( T->right == NULL ) /* Only a left child */ child = T->left; free( tmp_cell ); return child; } return T; }