typedef int ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode* lchild, * rchild;
}BSTNode, * BSTree;
void BSTInsert(BSTree& T, BSTree s)
{
if (T == NULL)
T = s;
else if (s->data < T->data)
BSTInsert((T->lchild), s);
else
BSTInsert((T->rchild), s);
}
void BSTCreate(BSTree& T)
{
T = NULL;
BSTree cur = NULL;
ElemType x;
cin >> x;
while (x != -1)
{
cur = new BSTNode;
cur->data = x;
cur->lchild = cur->rchild = NULL;
BSTInsert(T, cur);
cin >> x;
}
}
BSTree BSTSearch(BSTree T, ElemType k)
{
if (!T || T->data == k)
return T;
if (k < T->data)
return BSTSearch(T->lchild, k);
else
return BSTSearch(T->rchild, k);
}
int main()
{
BSTree T, p;
BSTCreate(T);
int x;
cin >> x;
p = BSTSearch(T, x);
if (p != NULL)
{
cout << "have found!";
cout << " lchild:";
if (p->lchild)
cout << p->lchild->data;
else
cout << "NULL";
cout << " rchild:";
if (p->rchild)
cout << p->rchild->data;
else
cout << "NULL";
}
else
cout << "NOT FOUND!";
return 0;
}
6-5 数据结构考题 - 二叉排序树 分数 20
于 2023-12-07 22:23:44 首次发布