上午
看关于《大话数据结构》中的关于二叉树的章节,已经了解了四种遍历序列。分别为前序遍历序列、中序遍历序列、后序遍历序列和层次遍历序列。
嗯,看了比较久,下午也有在看书。果然,看了书和会写题目是不能一概而论的……
遍历方式了解到了,遍历的代码也会写了,也试了一下写创建二叉树的模板,嗯,还行,还行。
把模板贴这里吧。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char a;
struct node *lchild;
struct node *rchild;
}Bitree;
char c;
Bitree *h;
Bitree *creat()
{
Bitree *t;
scanf("%c",&c);
if(c=='*')
{t=NULL;return t;}
t=(Bitree *)malloc(sizeof(Bitree));
t->a=c;
t->lchild=creat();
t->rchild=creat();
return t;
}
void print(Bitree *t)
{
if(t==NULL)
return;
printf("%c",t->a);
print(t->lchild);
print(t->rchild);
}
int main()
{
h=creat();
print(h);
}
这个是关于创建二叉树和以前序遍历序列输出的模板,其他的输出方式只需要改一下print就好了。
下午
看书,写了两个给出前序遍历序列和中序遍历序列、给出后序遍历序列和中序遍历序列然后求出另一种遍历序列的题目。嗯,还行。手写能写入出来,让后发现有一个题目是要编写个程序来求出……还没整出来。
写出个题目:
过的代码是:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int y;
char b[1000];
typedef struct node{
char a;
struct node *lchild;
struct node *rchild;
}Bitree;
Bitree *h;
Bitree *creat()
{
Bitree *t;
if(b[y++]=='*')
{t=NULL;return t;}
t=(Bitree *)malloc(sizeof(Bitree));
t->a=b[y-1];
t->lchild=creat();
t->rchild=creat();
return t;
}
void print(Bitree *t)
{
if(t==NULL)
return;
printf("%c",t->a);
print(t->lchild);
print(t->rchild);
}
int main()
{
int n,c=0;
char a[30][5];
scanf("%d\n",&n);
for(int i=0;i<n;i++)
gets(a[i]);
for(int j=0;j<3;j++)
b[c++]=a[0][j];
for(int i=1;i<n;i++)
for(int k=0;k<strlen(b);k++)
if(b[k]==a[i][0])
{
char l[100];
int p=0;
for(int i=k+1;i<strlen(b);i++)
l[p++]=b[i];
b[k+1]=a[i][1];b[k+2]=a[i][2];
int u=k+3;
for(int i=0;i<p;i++)
b[u++]=l[i];
}
h=creat();
print(h);
}
嗯,没写注释了,以后统一写到题解组里去。
这题就很麻,我本来就写的是对的,它好像偏要使用二叉树方式输出才算正确。我之前用普通的字符串处理写出来的答案明明是对的,死活不让我对,后来我灵机一动,把得出的答案做成个二叉树,然后使用前序遍历序列输出。欸!就对了。nice!
看书看书,持续看书。
晚上
嗯,写那道我说是编程算出遍历序列的题,还未成功……
我又仔细看了看书上的求出遍历序列的描述,嗯,离成功不远了。
写做过的题目题解。
明日计划
写出2道+的题目,再看看并查集的内容。