Skip to content

Commit ca48162

Browse files
authored
Merge pull request #126 from PWHL/master1
ok
2 parents 402946d + 51b848b commit ca48162

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

2017-1/PWHL/Homework.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include"Homework.h"
2+
status CreateBiTree(BiTree *T, char *c)
3+
{
4+
5+
ElemType ch;
6+
ch = c[number];
7+
number++;
8+
if (ch == '*')
9+
{
10+
*T = NULL;
11+
}
12+
else
13+
{
14+
if (!(*T = (BiTree)malloc(sizeof(BiTNode))))
15+
{
16+
exit(-1);
17+
}
18+
(*T)->data = ch;//生成根结点
19+
CreateBiTree(&(*T)->lchild, c);//构造左子树
20+
CreateBiTree(&(*T)->rchild, c);//构造右子树
21+
}
22+
return 1;
23+
}
24+
//后序遍历二叉树
25+
void postorderTraversal(BiTree t)//后续遍历
26+
{
27+
if (t == NULL)
28+
{
29+
printf("*");
30+
return;
31+
}
32+
postorderTraversal(t->lchild);//访问左子树
33+
postorderTraversal(t->rchild);//访问右子树
34+
printf("%c ", t->data);
35+
}
36+
//二叉树的深度
37+
int TreeDepth(BiTree T)
38+
{
39+
int depth = 0;
40+
if (T)
41+
{
42+
int leftdepth = TreeDepth(T->lchild);//查找左子树的的最深值
43+
int rightdepth = TreeDepth(T->rchild);//查找右子树的最深值
44+
depth = leftdepth >= rightdepth ? leftdepth + 1 : rightdepth + 1;
45+
}
46+
return depth;
47+
}
48+
int widthHouxu(BiTree t)//后续遍历
49+
{
50+
int i;
51+
int max = 0;//用于存储最大值
52+
if (t == NULL)
53+
{
54+
return 0;
55+
}
56+
width[w]++;
57+
w++;
58+
widthHouxu(t->lchild);//访问左子树
59+
widthHouxu(t->rchild);//访问右子树
60+
w--;//当左子树和右子树均遍历了 返回上一层继续统计
61+
for (i = 0; i < nwidth; i++)//找到最大值进行输出
62+
{
63+
64+
if (max < width[i])
65+
{
66+
max = width[i];
67+
}
68+
}
69+
return max;
70+
}
71+
int main()
72+
{
73+
BiTree t;
74+
ElemType n[20];
75+
76+
scanf("%s", n);
77+
printf("*代表空\n");
78+
CreateBiTree(&t, n);//产生二叉树
79+
printf("后序遍历的顺序为\n");
80+
postorderTraversal(t);//后序遍历二叉树
81+
printf("\n树的深度为%d \n", TreeDepth(t));
82+
printf("\n树的宽度为%d \n", widthHouxu(t));
83+
return 0;
84+
}

2017-1/PWHL/Homework.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
typedef int status;
4+
typedef char ElemType;
5+
#define nwidth 10
6+
int w = 1;//计算层数
7+
int width[nwidth] = { 0 };
8+
int number = 0;//用于计算结点个数
9+
int count[100];
10+
11+
typedef struct BiTree
12+
{
13+
struct BiTree *lchild;
14+
struct BiTree *rchild;
15+
ElemType data;
16+
}BiTNode, *BiTree;
17+
status CreateBiTree(BiTree *T, char *c);
18+
void postorderTraversal(BiTree T);
19+
int TreeDepth(BiTree T);
20+
int widthHouxu(BiTree T);
21+
46 KB
Loading
20.8 KB
Loading
28.3 KB
Loading

2017-1/PWHL/hw.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include"hw.h"
2+
status CreateBiTree(BiTree *T, char *c)
3+
{
4+
5+
ElemType ch;
6+
ch = c[number];
7+
number++;
8+
if (ch == '*')
9+
{
10+
*T = NULL;
11+
}
12+
else
13+
{
14+
if (!(*T = (BiTree)malloc(sizeof(BiTNode))))
15+
{
16+
exit(-1);
17+
}
18+
(*T)->data = ch;//生成根结点
19+
CreateBiTree(&(*T)->lchild, c);//构造左子树
20+
CreateBiTree(&(*T)->rchild, c);//构造右子树
21+
}
22+
return 1;
23+
}
24+
//后序遍历二叉树
25+
void postorderTraversal(BiTree t)//后续遍历
26+
{
27+
if (t == NULL)
28+
{
29+
printf("*");
30+
return;
31+
}
32+
postorderTraversal(t->lchild);//访问左子树
33+
postorderTraversal(t->rchild);//访问右子树
34+
printf("%c ", t->data);
35+
}
36+
//二叉树的深度
37+
int TreeDepth(BiTree T)
38+
{
39+
int depth = 0;
40+
if (T)
41+
{
42+
int leftdepth = TreeDepth(T->lchild);//查找左子树的的最深值
43+
int rightdepth = TreeDepth(T->rchild);//查找右子树的最深值
44+
depth = leftdepth >= rightdepth ? leftdepth + 1 : rightdepth + 1;
45+
}
46+
return depth;
47+
}
48+
int widthHouxu(BiTree t)//后续遍历
49+
{
50+
int i;
51+
int max = 0;//用于存储最大值
52+
if (t == NULL)
53+
{
54+
return 0;
55+
}
56+
width[w]++;
57+
w++;
58+
widthHouxu(t->lchild);//访问左子树
59+
widthHouxu(t->rchild);//访问右子树
60+
w--;//当左子树和右子树均遍历了 返回上一层继续统计
61+
for (i = 0; i < nwidth; i++)//找到最大值进行输出
62+
{
63+
64+
if (max < width[i])
65+
{
66+
max = width[i];
67+
}
68+
}
69+
return max;
70+
}
71+
int main()
72+
{
73+
BiTree t;
74+
ElemType n[20];
75+
int k=0;
76+
printf("用例1:输入1为abc**d**ef***,输入2为abcd**ef***g**h*g**\n");
77+
78+
scanf("%d",&k);
79+
if (k == 1)
80+
{
81+
strcpy(n, "abc**d**ef***");
82+
}
83+
else if (k == 2)
84+
{
85+
strcpy(n, "abcd**ef***g**h*g**");
86+
}
87+
printf("*代表空\n");
88+
CreateBiTree(&t, n);//产生二叉树
89+
printf("后序遍历的顺序为\n");
90+
postorderTraversal(t);//后序遍历二叉树
91+
printf("\n树的深度为%d \n", TreeDepth(t));
92+
printf("\n树的宽度为%d \n", widthHouxu(t));
93+
return 0;
94+
}

2017-1/PWHL/hw.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
typedef int status;
4+
typedef char ElemType;
5+
#define nwidth 10
6+
int w = 1;//计算层数
7+
int width[nwidth] = { 0 };
8+
int number = 0;//用于计算结点个数
9+
int count[100];
10+
11+
typedef struct BiTree
12+
{
13+
struct BiTree *lchild;
14+
struct BiTree *rchild;
15+
ElemType data;
16+
}BiTNode, *BiTree;
17+
status CreateBiTree(BiTree *T, char *c);
18+
void postorderTraversal(BiTree T);
19+
int TreeDepth(BiTree T);
20+
int widthHouxu(BiTree T);
21+

0 commit comments

Comments
 (0)