forked from lintingbin/C-language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
118 lines (105 loc) · 2.37 KB
/
Copy pathtree.c
File metadata and controls
118 lines (105 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
//关于树的一些练习
typedef struct btNode
{
int data;
struct btNode *lchild, *rchild;
} btNode, *BiTree;
BiTree CreateBiTree()
{
char ch;
BiTree T;
scanf(" %c",&ch);
if(ch=='#')T=NULL;
else
{
T = (BiTree)malloc(sizeof(btNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;
}
void PreOrderTraverse(BiTree T)
{
if(T)
{
printf("%c",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
void TreeOfMirror(BiTree T)
{
BiTree stack[100], current, tmp;
int top = -1;
if (T != NULL)
stack[++top] = T;
while (top != -1)
{
current = stack[top--];
if (current->lchild != NULL)
stack[++top] = current->lchild;
if (current->rchild != NULL)
stack[++top] = current->rchild;
tmp = current->lchild;
current->lchild = current->rchild;
current->rchild = tmp;
}
}
int TreeDeep(BiTree T)
{
int leftDeep, rightDeep;
if (T == NULL)
return 0;
leftDeep = TreeDeep(T->lchild) + 1;
rightDeep = TreeDeep(T->rchild) + 1;
return leftDeep>rightDeep? leftDeep:rightDeep;
}
int TreeMaxRoad(BiTree T)
{
int leftDeep, rightDeep;
static int Max = 0;
if (T == NULL)
return 0;
leftDeep = TreeDeep(T->lchild) + 1;
rightDeep = TreeDeep(T->rchild) + 1;
if (leftDeep + rightDeep - 1 > Max)
Max = leftDeep + rightDeep - 1;
return Max;
}
int PrintNodeAtLevel(BiTree T, int level)
{
if (!T || level < 0)
return 0;
if (level == 0)
{
printf("%c\n", T->data);
return 1;
}
return (PrintNodeAtLevel(T->lchild, level-1) + PrintNodeAtLevel(T->rchild, level-1));
}
#define N 100
int PrintAllLevel(BiTree root)
{
BiTree sequence[N];
int start, end, last;
if (root == NULL)
return 0;
start = -1;
end = last = 0;
sequence[end++] = root;
while (++start <= end - 1)
{
if (sequence[start]->lchild != NULL)
sequence[end++] = sequence[start]->lchild;
if (sequence[start]->rchild != NULL)
sequence[end++] = sequence[start]->rchild;
printf("%c ", sequence[start]->data);
if (start == last)
{
printf("\n");
last = end - 1;
}
}
}