-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
76 lines (65 loc) · 1.38 KB
/
tree.c
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
#include <stdio.h>
typedef struct biTree{
int w;
struct buTree *lchild, *rchild;
}BiTree;
BiTree *T;
BiTree *creatTr(BiTree **pT)
{
*pT = (BiTree *)malloc(sizeof(BiTree));
(*pT)->w = 0;
BiTree *a = (BiTree *)malloc(sizeof(BiTree));
a->w = 1;
BiTree *b = (BiTree *)malloc(sizeof(BiTree));
b->w = 2;
BiTree *c = (BiTree *)malloc(sizeof(BiTree));
c->w = 3;
BiTree *d = (BiTree *)malloc(sizeof(BiTree));
d->w = 4;
BiTree *e = (BiTree *)malloc(sizeof(BiTree));
e->w = 5;
BiTree *f = (BiTree *)malloc(sizeof(BiTree));
f->w = 6;
BiTree *g = (BiTree *)malloc(sizeof(BiTree));
g->w = 7;
BiTree *h = (BiTree *)malloc(sizeof(BiTree));
h->w = 8;
(*pT)->lchild = a;
(*pT)->rchild = b;
a->lchild = c;
a->rchild = d;
b->lchild = e;
b->rchild = f;
c->lchild = NULL;
c->rchild = NULL;
d->lchild = g;
d->rchild = h;
e->lchild = NULL;
e->rchild = NULL;
f->lchild = NULL;
f->rchild = NULL;
g->lchild = NULL;
g->rchild = NULL;
h->lchild = NULL;
h->rchild = NULL;
}
void visit(int data)
{
printf("%d\n", data);
}
void trave(BiTree *pT, int(*visit)(int))
{
BiTree *p = pT;
if(p != NULL)
{
(*visit)(p->w);
trave(p->lchild, visit);
trave(p->rchild, visit);
}
}
int main()
{
creatTr(&T);
trave(T, visit);
return 0;
}