二叉排序树
#include<bits/stdc++.h>
using namespace std;
typedef struct BiTNode{
char a;
struct BiTNode* left;
struct BiTNode* right;
}BiTNode,*BiTree;
int Search(BiTree &bt,char key,BiTree &p);//给定一个字母,在二叉树上查找对应位置
void Insert(BiTree &bt,char a);//插入节点
void Creat(BiTree &bt);//创建树
void zhongxu(BiTree &bt);//中序遍历
int Search(BiTree &bt,char key,BiTree &p){
BiTree f=NULL;
BiTree s=bt;
if(s==NULL){
p=f;
return 0;
}
while(s!=NULL){
if(s->a==key){
p=s;
return 1;
}
else if(key<s->a){
f=s;
s=s->left;
}
else{
f=s;
s=s->right;
}
}
p=f;//返回空节点的父节点
return 0;
}
void Insert(BiTree &bt,char a){
if(bt==NULL){//如果bt二叉树是空树,我们给bt新建一下
bt=(BiTree)malloc(sizeof(BiTNode));
bt->a=a;
bt->left=bt->right=NULL;
}
else{
BiTree b=NULL;//如果bt不为空,那么寻找插入节点位置,进行插入
if(Search(bt,a,b)==0){
BiTree c=(BiTree)malloc(sizeof(BiTNode));
c->a=a;
c->left=c->right=NULL;
if(b->a>a){
b->left=c;
}
else{
b->right=c;
}
}
}
}
void Creat(BiTree &bt){
bt=NULL;
FILE* fp;
fp=fopen("Letter.txt","w");//打开文件
int a;
int c3[26];
for(int em=0;em<26;em++){
c3[em]=0;
}
srand(time(0));
int b;
for(b=0;b<10;b++){
char c;
while(true){
int a2=rand()%26;//建立字母
if(c3[a2]==0){
c3[a2]=1;
c=(char)('A'+a2);
break;
}
}
fprintf(fp,"%c,",c);
BiTree d=NULL;
if(Search(bt,c,d)==0){
Insert(bt,c);
}
}
fprintf(fp,"\n");
fclose(fp);
}
void dayin(BiTree &bt){
BiTree a=bt;
//cout<<a<<endl;
while(a!=NULL){
cout<<a->a<<endl;
a=a->right;
}
}
void zhongxu(BiTree &bt){
BiTree a=bt;
if(a!=NULL){
zhongxu(a->left);
FILE* fp=fopen("Tree.txt","a");
fprintf(fp,"%c, ",a->a);
fclose(fp);
cout<<a->a<<" "<<endl;
zhongxu(a->right);
}
}
void feidiguizhongxu(BiTree &bt){
FILE* fp=fopen("Tree.txt","w");
BiTree a=bt;
if(bt==NULL){
printf("二叉树为空\n");
return;
}
BiTree b[1000];
int tot=0;
while(a!=NULL||tot>0){
while(a!=NULL){
tot++;
b[tot]=a;
a=a->left;
}
a=b[tot];
tot--;
fprintf(fp,"%c, ",a->a);
cout<<a->a<<endl;//打印到屏幕上
a=a->right;
}
}
int main(){
BiTree bt;
Creat(bt);
feidiguizhongxu(bt);
return 0;
}
二叉排序树测试
#include<bits/stdc++.h>
using namespace std;
typedef struct BiTNode{
char a;
struct BiTNode* left;
struct BiTNode* right;
}BiTNode,*BiTree;
int Search(BiTree &bt,char key,BiTree &p);//给定一个字母,在二叉树上查找对应位置
void Insert(BiTree &bt,char a);//插入节点
void Creat(BiTree &bt);//创建树
void zhongxu(BiTree &bt);//中序遍历
int Search(BiTree &bt,char key,BiTree &p){
BiTree f=NULL;
BiTree s=bt;
if(s==NULL){
p=f;
return 0;
}
while(s!=NULL){
if(s->a==key){
p=s;
return 1;
}
else if(key<s->a){
f=s;
s=s->left;
}
else{
f=s;
s=s->right;
}
}
p=f;//返回空节点的父节点
return 0;
}
void Insert(BiTree &bt,char a){
if(bt==NULL){//如果bt二叉树是空树,我们给bt新建一下
bt=(BiTree)malloc(sizeof(BiTNode));
bt->a=a;
bt->left=bt->right=NULL;
}
else{
BiTree b=NULL;//如果bt不为空,那么寻找插入节点位置,进行插入
if(Search(bt,a,b)==0){
BiTree c=(BiTree)malloc(sizeof(BiTNode));
c->a=a;
c->left=c->right=NULL;
if(b->a>a){
b->left=c;
}
else{
b->right=c;
}
}
}
}
void Creat(BiTree &bt){
bt=NULL;
FILE* fp;
fp=fopen("Letter.txt","w");//打开文件
int a;
int c3[26];
for(int em=0;em<26;em++){
c3[em]=0;
}
srand(time(0));
int b;
for(b=0;b<10;b++){
char c;
while(true){
int a2=rand()%26;//建立字母
if(c3[a2]==0){
c3[a2]=1;
c=(char)('A'+a2);
break;
}
}
fprintf(fp,"%c,",c);
BiTree d=NULL;
if(Search(bt,c,d)==0){
Insert(bt,c);
}
}
fprintf(fp,"\n");
fclose(fp);
}
/*void dayin(BiTree &bt){
BiTree a=bt;
//cout<<a<<endl;
while(a!=NULL){
cout<<a->a<<endl;
a=a->right;
}
}*/
void zhongxu(BiTree &bt){
BiTree a=bt;
if(a!=NULL){
zhongxu(a->left);
FILE* fp=fopen("Tree.txt","a");
fprintf(fp,"%c, ",a->a);
fclose(fp);
cout<<a->a<<" "<<endl;
zhongxu(a->right);
}
}
void feidiguizhongxu(BiTree &bt){
FILE* fp=fopen("Tree.txt","w");
BiTree a=bt;
if(bt==NULL){
printf("二叉树为空\n");
return;
}
BiTree b[1000];
int tot=0;
while(a!=NULL||tot>0){
while(a!=NULL){
tot++;
b[tot]=a;
a=a->left;
}
a=b[tot];
tot--;
fprintf(fp,"%c, ",a->a);
cout<<a->a<<endl;//打印到屏幕上
a=a->right;
}
}
int main(){
BiTree bt;
Creat(bt);
feidiguizhongxu(bt);
return 0;
}
图
#include<bits/stdc++.h>
using namespace std;
typedef struct biao{//表结点
int bianhao;
int weight;
struct biao* next;
}biao;
typedef struct tou{//头节点
char a;
biao* next;
}tou;
typedef struct Graph{//图
tou b[20];
int vex,edge;
}Graph;
int Locate(Graph &p,char a);//根据给定的字母a来定位a的编号
void Creat(Graph &p);//创建一个图
int Locate(Graph &p,char a){
int b=p.vex;
int c;
for(c=0;c<b;c++){
if(p.b[c].a==a){
return c;
}
}
return -1;
}
void Creat(Graph &p){
FILE* fp;
fp=fopen("Graph.txt","w");//写入文件
scanf("%d %d ",&p.vex,&p.edge);//输入图的点的数量和边的数量
int a;
srand(time(0));//随机生成数字
int c3[27];
for(int em=0;em<27;em++){
c3[em]=0;
}
fprintf(fp,"顶点信息:\n");
for(a=0;a<p.vex;a++){
while(true){
int a2=rand()%26;
if(c3[a2]==0){
c3[a2]=1;
p.b[a].a=(char)('a'+a2);
fprintf(fp,"%d:%c; ",a,p.b[a].a);//将字符的编号和字符的内容写入文件
break;
}
}
}
fprintf(fp,"\n");
for(a=0;a<p.vex;a++){
p.b[a].next=NULL;//预处理
}
fprintf(fp,"边的信息\n");
biao* m3=(biao*)malloc(sizeof(biao));//由于我们预先指定一条无向边为0到1
m3->next=NULL;//所以预先设定表节点
m3->bianhao=0;
biao* m4=(biao*)malloc(sizeof(biao));
m4->next=NULL;
m4->bianhao=1;
p.b[0].next=m4;
p.b[1].next=m3;
fprintf(fp,"(%c,%c), ",p.b[0].a,p.b[1].a);//写入文档
for(a=1;a<p.edge;a++){//剩下n-1条变,进行输入
int u1,v1;
scanf(" %d %d",&u1,&v1);//键盘输入
fprintf(fp,"(%c,%c), ",p.b[u1].a,p.b[v1].a);//打印到文件里
biao* m=(biao*)malloc(sizeof(biao));
m->next=NULL;
m->bianhao=v1;
if(p.b[u1].next==NULL){
p.b[u1].next=m;
}
else{
biao* n=p.b[u1].next;
while(n->next!=NULL){
n=n->next;
}
n->next=m;
}
biao* m1=(biao*)malloc(sizeof(biao));//无向边处理两次
m1->next=NULL;
m1->bianhao=u1;
if(p.b[v1].next==NULL){
p.b[v1].next=m1;
}
else{
biao* n=p.b[v1].next;
while(n->next!=NULL){
n=n->next;
}
n->next=m1;
}
}
}
void dayin(Graph &p){//验证
int a=p.vex;
int b;
for(b=0;b<a;b++){
printf("%c: ",p.b[b].a);
biao* m=p.b[b].next;
while(m!=NULL){
printf("%d->",m->bianhao);
m=m->next;
}
printf("\n");
}
}
int main(){
Graph p;
Creat(p);
dayin(p);
return 0;
}