问题:设计一算法,查找元素 x 所在结点,若找到,返回该结点的指针,否则返回一个空指针
假设二叉树的结点类型定义如下:
typedef struct node {
int data;
struct node *lchild,*rchild;
}Bstree;
算法如下:
BiTree Search(Bstree* bt, datatype x) {
Bstree * p=NULL;
if ( bt ) {
if ( bt->data == x) return bt;
if ( bt->Lchild != NULL) {
p=Search(bt->lchild,x);
if(p) return p;
}
if ( bt->rchild != NULL) {
p=Search(bt->rchild,x);
if(p) return p;
}
}
return NULL;
}