之前说记录自己大学学习代码的历程的,结果疫情以来就没更过,从现在起要勤快了!加油!
#include<stdlib.h>
#include<stdio.h>
typedef char elemtype;
struct BitNode{
elemtype data;
BitNode* lchild;
BitNode* rchild;
};
void CreateTree(BitNode** T)//创建二叉链表
{
elemtype ch;
scanf("%c",&ch);
getchar();
if(ch=='#'){
(*T)=NULL;
}else{
(*T)=(BitNode*)malloc(sizeof(BitNode));
if(!(*T)){
printf("创建错误!");
exit(1);
}
(*T)->data=ch;
printf("输入%c的左子树:",(*T)->data);
CreateTree(&((*T)->lchild));
printf("输入%c的右子树:",(*T)->data);
CreateTree(&((*T)->rchild));
}
}
BitNode* id_elem(BitNode* T,elemtype x)//查找
{
BitNode* temp;
if(T==NULL){
return NULL;
}
if(T->data==x){
return T;
}else{
temp=id_elem(T->lchild,x);
if(temp){
return temp;
}
temp=id_elem(T->rchild,x);
if(temp){
return temp;
}
}
}
int main()
{
BitNode* T,*temp;
elemtype ch;
printf("输入根树:");
CreateTree(&T);
printf("输入要查找的值:");
scanf("%c",&ch);
temp=id_elem(T,ch);
if(temp){
printf("%c",temp->data);
}else{
printf("为空!");
}
return 0;
}