谁能帮我把这个程序改好运行出来,谢谢。
include<stdio.h>#include<malloc.h>typedef int keytype;typedef struct node{ keytype key; struct node* lchild; struct node* rchild;}BSTNode, * Bstree;Bstree root;BSTNode* f;void InsertBST(Bstree root, keytype key){ if(!root) {BSTNode* p; p = (BSTNode*)malloc(sizeof(BSTNode)); p->key = key; p->lchild = p->rchild = NULL; root = p; } else if (key < root->key) InsertBST(root->lchild,key); else if (key > root->key) InsertBST(root->rchild, key);} Bstree CreatBST(Bstree root, keytype key){ Bstree T=NULL; while (key) { InsertBST(T, key); scanf_s("%d", &key); }return T;}void InOrder(Bstree root){ if (root) { InOrder(root->lchild); printf("%d",root->key); InOrder(root->rchild); }}Bstree searchBST(Bstree T, keytype key){ if (T == NULL || key == T->key) return T; if (key < T->key) return searchBST(T->lchild,key); if (key > T->key) return searchBST(T->rchild,key);} void main(){ int x, key; printf("请输入结点创建二叉排序树:\n"); scanf_s("%d", &key); CreatBST(root,key); printf("中序遍历:\n"); InOrder(root); printf("请输入:1 插入 2 查找\n"); scanf_s("%d", &x); if (x == 1) { printf("请输入要插入的数:"); scanf_s("%d", &key); while (key) { InsertBST(root, key); scanf_s("%d", &key); }InOrder(root); } if (x == 2) { printf("请输入要查找的数:"); scanf_s("%d\n", &key); if (searchBST(root, key)) printf("查找成功\n"); else printf("查找失败"); }}
输入以0作为结束
#include<stdio.h>
#include<malloc.h>
#define _CRT_SECURE_NO_WARNINGS
typedef int keytype;
typedef struct node
{
keytype key;
struct node* lchild;
struct node* rchild;
} BSTNode, * Bstree;
Bstree root;
BSTNode* f;
void InsertBST(Bstree root, keytype key)
{
if(!root)
{
BSTNode* p;
p = (BSTNode*)malloc(sizeof(BSTNode));
p->key = key;
p->lchild = p->rchild = NULL;
root = p;
}
else if (key < root->key)
InsertBST(root->lchild,key);
else if (key > root->key)
InsertBST(root->rchild, key);
}
Bstree CreatBST(Bstree root, keytype key)
{
Bstree T=NULL;
while (key)
{
InsertBST(T, key);
scanf("%d", &key);
}
return T;
}
void InOrder(Bstree root)
{
if (root)
{
InOrder(root->lchild);
printf("%d",root->key);
InOrder(root->rchild);
}
}
Bstree searchBST(Bstree T, keytype key)
{
if (T == NULL || key == T->key)
return T;
if (key < T->key)
return searchBST(T->lchild,key);
if (key > T->key)
return searchBST(T->rchild,key);
}
void main()
{
int x, key;
printf("请输入结点创建二叉排序树:\n");
scanf("%d", &key);
CreatBST(root,key);
printf("中序遍历:\n");
InOrder(root);
printf("请输入:1 插入 2 查找\n");
scanf("%d", &x);
if (x == 1)
{
printf("请输入要插入的数:");
scanf("%d", &key);
while (key)
{
InsertBST(root, key);
scanf("%d", &key);
}
InOrder(root);
}
if (x == 2)
{
printf("请输入要查找的数:");
scanf("%d\n", &key);
if (searchBST(root, key))
printf("查找成功\n");
else
printf("查找失败");
}
}