void InsertBST(BSTNode bt,KeyType k) { if(bt==NULL) { bt->key=k; printf("元素的数据为:"); scanf("%s",bt->data); printf("增加成功!\n"); } else if(k<bt->key) { bt->lchild=(BSTNode)malloc(sizeof(BSTNode)); InsertBST(bt->lchild,k); } else if(k>bt->key) { bt->rchild=(BSTNode)malloc(sizeof(BSTNode)); InsertBST(bt->rchild,k); } } void CreateBST(BSTNode bt) { int i,n; KeyType k; printf("二叉树的元素个数为:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("请输入第%d个元素的信息:\n",i); printf("元素的关键字为:"); scanf("%d",&k); InsertBST(bt,k); } }修改代码
时间: 2023-06-26 20:03:26 浏览: 41
void InsertBST(BSTNode* bt,KeyType k) {
if (*bt == NULL) {
*bt = (BSTNode)malloc(sizeof(BSTNode));
(*bt)->key = k;
printf("元素的数据为:");
scanf("%s",(*bt)->data);
printf("增加成功!\n");
} else if (k < (*bt)->key) {
InsertBST(&((*bt)->lchild),k);
} else if (k > (*bt)->key) {
InsertBST(&((*bt)->rchild),k);
}
}
void CreateBST(BSTNode* bt) {
int i,n;
KeyType k;
printf("二叉树的元素个数为:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
printf("请输入第%d个元素的信息:\n",i);
printf("元素的关键字为:");
scanf("%d",&k);
InsertBST(bt,k);
}
}
注意要传入指向指针的指针,因为要修改指针本身的值,以便在递归时正确地链接新节点。
相关问题
优化这段代码:#include<stdio.h> #include<assert.h> #include<string.h> #include<errno.h> #include<stdlib.h> typedef int KeyType; typedef char InfoType; typedef struct node { KeyType key; InfoType data; struct node*lchild,*rchild; }BSTNode; BSTNode *insertbst(BSTNode*&bt,KeyType k) { if(bt==NULL) { bt=(BSTNode*)malloc(sizeof(BSTNode)); bt->key=k;bt->lchild=bt->rchild=NULL; } else if(k<bt->key) bt->lchild=insertbst(bt->lchild,k); else if(k>bt->key) bt->rchild=insertbst(bt->rchild,k); return bt; } BSTNode*createbst(KeyType a[],int n) { BSTNode*bt=NULL; int i=0; while(i<n) { bt=insertbst(bt,a[i]); i++; } return bt; } BSTNode *searchbst(BSTNode*bt,KeyType k) { BSTNode *p=bt; while(p!=NULL) { if(p->key==k)break; else if(p->key<k) p=p->lchild; else p=p->rchild; } return p; } void dispbst(BSTNode*bt) { if(bt!=NULL) { printf("%d",bt->key); if(bt->lchild!=NULL||bt->rchild!=NULL) { printf("("); dispbst(bt->lchild); if(bt->rchild!=NULL) printf(","); dispbst(bt->rchild); printf(")"); } } } int main() { BSTNode *bt,q; KeyType k=6; int a[]={4,9,0,1,8,6,3,5,2,7}; int n=10; bt=createbst(a,n); printf("输出这棵树"); dispbst(bt); printf("\n要查找的关键字为:%d",k); q=searchbst(bt,k); if((q!=0) printf("输出关键字6所在的位置:%d\n",q); else printf("\n关键字6的元素不在表中\n"); return 1; }
以下是对代码的优化:
1. 将代码中的Keytype和InfoType定义提前到头文件中,以便在多个文件中使用。
2. 在insertbst函数中,使用了递归的方式实现插入,但是可以使用迭代的方式来实现,代码更加简洁。
3. 在createbst函数中,可以使用for循环替代while循环,代码更加简洁。
4. 在searchbst函数中,如果找到了目标节点,应该返回指向该节点的指针,否则应该返回NULL。
5. 在输出函数dispbst中,应该先输出左子树,再输出右子树。
优化后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef int KeyType;
typedef char InfoType;
typedef struct node {
KeyType key;
InfoType data;
struct node *lchild, *rchild;
} BSTNode;
BSTNode *insertbst(BSTNode *&bt, KeyType k) {
BSTNode *p = bt, *prev = NULL;
while (p != NULL) {
prev = p;
if (k < p->key) {
p = p->lchild;
} else if (k > p->key) {
p = p->rchild;
} else {
return bt;
}
}
p = (BSTNode *) malloc(sizeof(BSTNode));
p->key = k;
p->lchild = p->rchild = NULL;
if (prev == NULL) {
bt = p;
} else if (k < prev->key) {
prev->lchild = p;
} else {
prev->rchild = p;
}
return bt;
}
BSTNode *createbst(KeyType a[], int n) {
BSTNode *bt = NULL;
for (int i = 0; i < n; i++) {
insertbst(bt, a[i]);
}
return bt;
}
BSTNode *searchbst(BSTNode *bt, KeyType k) {
BSTNode *p = bt;
while (p != NULL && p->key != k) {
if (k < p->key) {
p = p->lchild;
} else {
p = p->rchild;
}
}
return p;
}
void dispbst(BSTNode *bt) {
if (bt != NULL) {
printf("%d", bt->key);
if (bt->lchild != NULL || bt->rchild != NULL) {
printf("(");
dispbst(bt->lchild);
if (bt->rchild != NULL) {
printf(",");
}
dispbst(bt->rchild);
printf(")");
}
}
}
int main() {
BSTNode *bt, *q;
KeyType k = 6;
int a[] = {4, 9, 0, 1, 8, 6, 3, 5, 2, 7};
int n = 10;
bt = createbst(a, n);
printf("输出这棵树:");
dispbst(bt);
printf("\n要查找的关键字为:%d", k);
q = searchbst(bt, k);
if (q != NULL) {
printf("\n输出关键字6所在的位置:%d\n", q->key);
} else {
printf("\n关键字6的元素不在表中\n");
}
return 1;
}
```
经过优化后,代码更加简洁易读,并且运行效率也有所提高。
#include<stdio.h> #include<stdlib.h> typedef int KeyType; typedef struct node{ KeyType key; struct node*lchild,*rchild; }BSTNode,*BSTree; void InsertBST(BSTree*bst,KeyType key){ BSTree s;//?????????????????怎么不一样 if(*bst==NULL){ s=(BSTree)malloc(sizeof(BSTNode)); s->key=key; s->lchild=NULL; s->rchild=NULL; *bst=s; return; } else if(key<(*bst)->key) InsertBST(&((*bst)->lchild),key); else if(key>(*bst)->key) InsertBST(&((*bst)->rchild),key); } void CreateBST(BSTree*bst){ KeyType key; *bst=NULL; scanf("%d",&key); while(key!=0){ InsertBST(bst,key); scanf("%d",&key); } } BSTree DelBST(BSTree t,KeyType k){ BSTNode *p,*f,*s,*q; p=t;f=NULL; while(p){ if(p->key==k)break; f=p; if(p->key>k)p=p->lchild; else p=p->rchild; } if(p==NULL)return t; if(p->lchild==NULL){ if(f==NULL)t=p->rchild; else if(f->lchild==p)f->lchild=p->rchild; else f->rchild=p->rchild; free(p); } else{ q=p;s=p->lchild; while(s->rchild) {q=s;s=s->rchild; }if(q==p)q->lchild=s->lchild; else q->rchild=s->lchild; p->key=s->key; free(s); } return t; } int layer(BSTree bst,int k,int lay){ if(bst){ if(bst->key==k)return lay; if(bst->key>k){ lay++; return layer(bst->rchild,k,lay); } if(bst->key<k){ lay++; return layer(bst->lchild,k,lay); } } } void preOrder(BSTree bst){ if(bst!=NULL){ printf("%d ",bst->key); preOrder(bst->lchild); preOrder(bst->rchild); } if(bst==NULL)printf("# "); } void InOrder(BSTree bst){ if(bst!=NULL){ InOrder(bst->lchild); printf("%d ",bst->key); InOrder(bst->rchild); } } int main(){ BSTree bst; CreateBST(&bst); preOrder(bst); int key; scanf("%d",&key); bst=DelBST(bst,key); InOrder(bst); int n,lay=1; scanf("%d",&n); printf("%d",layer(bst,n,lay)); return 0; }为什么层数始终是0?怎么改
在函数`layer`中,当`bst->key>k`时,应该调用`layer(bst->lchild,k,lay)`,而不是`layer(bst->rchild,k,lay)`,因为左子树的值比根节点的值小,而右子树的值比根节点的值大。
因此,修改后的`layer`函数应该是这样的:
```c++
int layer(BSTree bst, int k, int lay) {
if (bst) {
if (bst->key == k) return lay;
if (bst->key > k) {
lay++;
return layer(bst->lchild, k, lay); // 这里应该是左子树
}
if (bst->key < k) {
lay++;
return layer(bst->rchild, k, lay); // 这里应该是右子树
}
}
return -1;
}
```
修改后的代码应该就可以正确地输出层数了。