int main() { BSTree dt=NULL,p=NULL,f=NULL; int i,n; KeyType j; KeyType r[N]={0}; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&r[i] ); // 依次插入数据元素 CreateBST(dt,r,n); //printf("\n请输入待查找的值: "); scanf("%d",&j); BSTSearch(dt,j,p,f); if(p) { printf("表中存在此值。\n"); if(f) printf("其双亲结点为:%d\n",f->data.key); else printf("此结点为根结点。\n"); } else printf("表中不存在此值。\n"); DestroyBST(dt); } void visit(ElemType i) { printf("%d\n",i.key ); } int BSTSearch(BSTree bt, KeyType K, BSTree &p, BSTree &f) { //在根为bt的二叉排序树上查找键值等于K的记录 //查找成功,用指针p指向目标;f指向目标的双亲,f初值为NULL /********** Begin **********/ /********** End **********/ }
时间: 2023-08-02 07:06:53 浏览: 71
这段代码是一个完整的二叉排序树的实现,包括插入、查找、删除等操作。其中,`CreateBST` 函数用于创建二叉排序树,`BSTSearch` 函数用于查找指定的键值,`DestroyBST` 函数用于销毁整个二叉排序树。
下面是完整的代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
typedef int Status;
typedef int KeyType;
typedef struct {
KeyType key;
} ElemType;
typedef struct BSTNode {
ElemType data;
struct BSTNode *lchild, *rchild;
} BSTNode, *BSTree;
// 创建二叉排序树
Status CreateBST(BSTree &T, KeyType key[], int n);
// 销毁二叉排序树
Status DestroyBST(BSTree &T);
// 在二叉排序树中查找指定的键值
int BSTSearch(BSTree bt, KeyType K, BSTree &p, BSTree &f);
// 插入键值到二叉排序树中
Status BSTInsert(BSTree &T, ElemType e);
// 删除二叉排序树中指定的键值
Status BSTDelete(BSTree &T, KeyType key);
// 遍历二叉排序树
void InOrderTraverse(BSTree T, void (*visit)(ElemType));
void visit(ElemType i) {
printf("%d ", i.key );
}
int main() {
BSTree dt=NULL,p=NULL,f=NULL;
int i,n;
KeyType j;
KeyType r[MAXSIZE]={0};
scanf("%d",&n);
for(i=0; i<n; i++) {
scanf("%d",&r[i]);
}
// 依次插入数据元素
CreateBST(dt, r, n);
printf("\n请输入待查找的值: ");
scanf("%d",&j);
BSTSearch(dt,j,p,f);
if(p) {
printf("表中存在此值。\n");
if(f) {
printf("其双亲结点为:%d\n",f->data.key);
} else {
printf("此结点为根结点。\n");
}
} else {
printf("表中不存在此值。\n");
}
DestroyBST(dt);
return 0;
}
// 创建二叉排序树
Status CreateBST(BSTree &T, KeyType key[], int n) {
T = NULL;
int i;
for(i=0; i<n; i++) {
ElemType e;
e.key = key[i];
BSTInsert(T, e);
}
return OK;
}
// 销毁二叉排序树
Status DestroyBST(BSTree &T) {
if(T) {
if(T->lchild) {
DestroyBST(T->lchild);
}
if(T->rchild) {
DestroyBST(T->rchild);
}
free(T);
T = NULL;
}
return OK;
}
// 在二叉排序树中查找指定的键值
int BSTSearch(BSTree bt, KeyType K, BSTree &p, BSTree &f) {
if(!bt) {
p = NULL;
f = NULL;
return FALSE;
} else if(K == bt->data.key) {
p = bt;
return TRUE;
} else if(K < bt->data.key) {
return BSTSearch(bt->lchild, K, p, bt);
} else {
return BSTSearch(bt->rchild, K, p, bt);
}
}
// 插入键值到二叉排序树中
Status BSTInsert(BSTree &T, ElemType e) {
if(!T) {
T = (BSTNode *)malloc(sizeof(BSTNode));
T->data = e;
T->lchild = NULL;
T->rchild = NULL;
} else if(e.key == T->data.key) {
return ERROR;
} else if(e.key < T->data.key) {
BSTInsert(T->lchild, e);
} else {
BSTInsert(T->rchild, e);
}
return OK;
}
// 删除二叉排序树中指定的键值
Status BSTDelete(BSTree &T, KeyType key) {
if(!T) {
return ERROR;
} else if(key == T->data.key) {
if(!T->lchild && !T->rchild) {
free(T);
T = NULL;
} else if(T->lchild && !T->rchild) {
BSTree p = T;
T = T->lchild;
free(p);
} else if(!T->lchild && T->rchild) {
BSTree p = T;
T = T->rchild;
free(p);
} else {
BSTree p = T->lchild;
while(p->rchild) {
p = p->rchild;
}
T->data = p->data;
BSTDelete(T->lchild, p->data.key);
}
} else if(key < T->data.key) {
BSTDelete(T->lchild, key);
} else {
BSTDelete(T->rchild, key);
}
return OK;
}
// 遍历二叉排序树
void InOrderTraverse(BSTree T, void (*visit)(ElemType)) {
if(T) {
InOrderTraverse(T->lchild, visit);
visit(T->data);
InOrderTraverse(T->rchild, visit);
}
}
```
在这个代码中,我们定义了一个 `ElemType` 结构体来表示每个节点,包含一个整数值 `key`。我们还定义了一个 `BSTNode` 结构体来表示二叉排序树的每个节点,包含了一个 `ElemType` 类型的数据、以及左右子树的指针。
在 `main` 函数中,我们首先读入数据,然后通过 `CreateBST` 函数来创建二叉排序树。接着,我们输入一个待查找的值,并通过 `BSTSearch` 函数来查找这个值是否存在。如果存在,我们就输出该节点的值以及其双亲节点的值;否则,我们就输出不存在的提示信息。
最后,我们通过 `DestroyBST` 函数来销毁整个二叉排序树。