??????5?????????
//?????????
//????1???????????????0?????????
BiTNode *SearchValue(BiTree T??int x){
if (T == NULL) {
return 0;
}else{
if (x < T->data) {
//????????
SearchValue(T->lChild?? x);
}else if (x > T->data){
//????????
SearchValue(T->rChild?? x);
}else{
//??????
printf("?????????????%p "??T);
return T;
}
}
return NULL;
}
??????6????????
//??????????
BiTree *DeleteValue(BiTree *T??int x){
//???????y??????????y???????
BiTNode *searchNode;
BiTNode *parentNode;
//????????????
searchNode = *T;
parentNode = *T;
while (searchNode->data != x) {
if (searchNode->lChild == NULL && searchNode->rChild == NULL) {
//??????????????????????????????????????????????????
printf("%s??????У????????????????? "??__FUNCTION__);
return T;
}
if (x < searchNode->data) {
parentNode = searchNode;
searchNode = searchNode->lChild;
}else if (x > searchNode->data){
parentNode = searchNode;
searchNode = searchNode->rChild;
}else{
break;
}
}
if (searchNode->lChild == NULL && searchNode->rChild == NULL) {
//???????
if ((*T)->lChild == NULL && (*T)->rChild == NULL) {
//??????
free(*T);
*T = NULL;
}else{
//????????????????????
//????ж????????????????????????????
if (searchNode->data < parentNode->data) {
//??????
parentNode->lChild = NULL;
}else{
//???????
parentNode->rChild = NULL;
}
free(searchNode);
searchNode = NULL;
}
return T;
}
if (searchNode->lChild != NULL && searchNode->rChild == NULL) {
//???????????????????
//???????????????????????????????????;
//????????ж?????????????????????????
if (searchNode->data < parentNode->data) {
//??????
parentNode->lChild = searchNode->lChild;
}else{
//???????
parentNode->rChild = searchNode->lChild;
}
free(searchNode);
searchNode = NULL;
return T;
}
if (searchNode->lChild == NULL && searchNode->rChild != NULL) {
//???????????????????
if (searchNode->data < parentNode->data) {
//??????
parentNode->lChild = searchNode->rChild;
}else{
//???????
parentNode->rChild = searchNode->rChild;
}
free(searchNode);
searchNode = NULL;
return T;
}
if (searchNode->lChild != NULL && searchNode->rChild != NULL) {
//?????????????????????????
/**
*  ??????????p???????????????????????????p????y?????y????????????????????????y??????y????????y??????????????????y???????p?????
???????????????????????ú???????????
*/
BiTNode *nextNode;//???????????????
BiTNode *nextParentNode;//????????????????????
nextParentNode = searchNode;
nextNode = searchNode->rChild;
while (nextNode->lChild != NULL) {
nextParentNode = nextNode;
nextNode = nextNode->lChild;
}
//??????ж?nextNode????nextParentNode???????С?????????ж??????????????????????????
if (nextNode->data < nextParentNode->data) {
//??????
nextParentNode->lChild = nextNode->rChild;
}else{
//???????
nextParentNode->rChild = nextNode->rChild;
}
//?????
searchNode->data = nextNode->data;
//????????
free(nextNode);
nextNode = NULL;
return T;
}
return T;
}
??????7?????????
int main(int argc?? const char * argv[]) {
BiTree tree;
//????????????
CreateBinarySearchTree(&tree);
MiddleOrder(tree);
printf(" ");
printf("????????????????");
int searchValue;
scanf("%d"??&searchValue);
SearchValue(tree??searchValue);
printf("????????????????");
int deleteValue;
scanf("%d"??&deleteValue);
DeleteValue(&tree?? deleteValue);
printf("?????????");
PreOrder(tree);
printf(" ?????????");
MiddleOrder(tree);//???????
printf(" ");
return 0;
}