蔡子经数据结构5.11

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


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct node
{
int ltag;
char data;
int rtag;
} node;
#define MAXN 8
node a[MAXN] =
{
{0,'A',0},
{1,'B',0},
{0,'D',1},
{1,'F',1},
{0,'C',1},
{0,'E',1},
{1,'G',0},
{1,'H',1}
};

int idx = 0;
int findPar(int par, char aim)
{
int cur = idx;
idx++;
if(a[cur].data == aim) return par;
int ret = -1;
if(a[cur].ltag == 0)
ret = findPar(cur, aim);
if(ret != -1) return ret;
if(a[cur].rtag == 0)
ret = findPar(cur, aim);
return ret;
}


int main()
{
int res = findPar(-1, 'D');
if(res == -1) puts("not found");
else printf("%c\n",a[res].data);
return 0;
}