求编一个c代码,输入满二叉树的先序遍历,输出中序遍历和后序遍历
答案:2 悬赏:70
解决时间 2021-02-16 19:33
- 提问者网友:沉默的哀伤
- 2021-02-16 10:48
注意是满二叉树。
最佳答案
- 二级知识专家网友:温柔刺客
- 2021-02-16 11:22
在淘宝搜快客利,认准店主sinohzxu,官方自助话费充值
#include <stdio.h>
#include <string.h>
#include <math.h>
#define NULL 0
typedef struct btree
{
char data;
struct btree *pLeft;
struct btree *pRight;
}quickli;
quickli* constuctTree(char input[], int beginIndex, int endIndex)
{
if (beginIndex > endIndex) return NULL;
quickli* root=(quickli *)malloc(sizeof(quickli));
root->data=input[beginIndex];
root->pLeft=constuctTree(input , beginIndex+1,(endIndex+beginIndex)/2);
root->pRight=constuctTree(input , (endIndex+beginIndex)/2+1,endIndex);
return root;
}
void inorderTree(quickli* tree)
{
if (tree == NULL) return ;
inorderTree(tree->pLeft);
printf("%c",tree->data);
inorderTree(tree->pRight);
}
void postorderTree(quickli* tree)
{
if (tree == NULL) return ;
postorderTree(tree->pLeft);
postorderTree(tree->pRight);
printf("%c",tree->data);
}
int main()
{
printf("请输入一个满二叉树先序遍历(如ABCDEFG):\n");
char input[100];
scanf("%s",input);
double check = log(strlen(input)+1)/log(2);
if(check != (int)check)
{
printf("错误:满二叉树的序列长度必须等于2^n-1(^是幂的意思)\n");
return -1;
}
quickli* tree=constuctTree(input,0,strlen(input)-1);
printf("中序遍历如下:\n");
inorderTree(tree);
printf("\n");
printf("后序遍历如下:\n");
postorderTree(tree);
printf("\n");
return 0;
}
在淘宝搜快客利,认准店主sinohzxu,官方自助话费充值
#include <stdio.h>
#include <string.h>
#include <math.h>
#define NULL 0
typedef struct btree
{
char data;
struct btree *pLeft;
struct btree *pRight;
}quickli;
quickli* constuctTree(char input[], int beginIndex, int endIndex)
{
if (beginIndex > endIndex) return NULL;
quickli* root=(quickli *)malloc(sizeof(quickli));
root->data=input[beginIndex];
root->pLeft=constuctTree(input , beginIndex+1,(endIndex+beginIndex)/2);
root->pRight=constuctTree(input , (endIndex+beginIndex)/2+1,endIndex);
return root;
}
void inorderTree(quickli* tree)
{
if (tree == NULL) return ;
inorderTree(tree->pLeft);
printf("%c",tree->data);
inorderTree(tree->pRight);
}
void postorderTree(quickli* tree)
{
if (tree == NULL) return ;
postorderTree(tree->pLeft);
postorderTree(tree->pRight);
printf("%c",tree->data);
}
int main()
{
printf("请输入一个满二叉树先序遍历(如ABCDEFG):\n");
char input[100];
scanf("%s",input);
double check = log(strlen(input)+1)/log(2);
if(check != (int)check)
{
printf("错误:满二叉树的序列长度必须等于2^n-1(^是幂的意思)\n");
return -1;
}
quickli* tree=constuctTree(input,0,strlen(input)-1);
printf("中序遍历如下:\n");
inorderTree(tree);
printf("\n");
printf("后序遍历如下:\n");
postorderTree(tree);
printf("\n");
return 0;
}
在淘宝搜快客利,认准店主sinohzxu,官方自助话费充值
全部回答
- 1楼网友:萌萌哒小可爱
- 2021-02-16 12:35
#define el 10
#define tel 2*el+1
#define len sizeof(struct node)
#include "stdio.h"
#include "stdlib.h"
char pre[tel]="abcdefghij";
char pin[tel]="cbedaghfji";
typedef struct node
{ char data;
struct node * lch,*rch;
}btnode,*btree;
btnode root;
btree rt=&root;
int pos(char c,char s[],int st)
{char *p;
p=s+st;
while(*p!=c && *p!='\0') p++;
return p-s;
}
void create(btree *t,int i1,int i2,int len)
{int r,llen,rlen;
if(len<=0) *t=null;
else
{*t=(btree)malloc(len);
(*t)->data=pre[i1];
r=pos(pre[i1],pin,i2);
llen=r-i2;
rlen=len-(llen+1);
create(&(*t)->lch,i1+1,i2,llen);
create(&(*t)->rch,i1+llen+1,r+1,rlen);
}
}
void travel(btree t)
{if(t)
{travel(t->lch);
travel(t->rch);
putchar(t->data);
}
}
int main()
{create(&rt,0,0,el);
if(rt) travel(rt);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯