1 #include<stdio.h>
2 #include<stdlib.h>
3 #define MAX_SIZE 50
4 typedef
char ElemType;
5
6 typedef
struct BiTNode
//结点的结构
7 {
8 ElemType data;
9 int parent;
10 }BiTNode;
11 /*存储在静态的链表中*/
12 typedef
struct
13 {
14 BiTNode node[MAX_SIZE];
15 int n;
//结点的个数
16 }Bitree;
17
18 void InitBiTree(Bitree *
T);
19
20 int Findparent(Bitree T,
int e);
21
22 void Root(Bitree T);
23
24 int main()
25 {
26 Bitree T;
27 T.n=
0;
28 int e;
29
30 printf(
"\n函数InitBiTree测试\n");
31 {
32 printf(
"初始化树T\n");
33 InitBiTree(&
T);
34 printf(
"\n");
35
36 }
37 printf(
"\n函数Findparent测试\n");
38 {
39 int Parent;
40 printf(
"请输入需要查询的结点\n");
41 scanf(
"%d",&
e);
42 Parent=
Findparent(T,e);
43 printf(
"序号为%d的%c双亲是:",e,T.node[e].data);
44
45 printf(
"序号为%d的%c",Parent,T.node[Parent].data);
46 printf(
"\n");
47
48 }
49
50 }
51
52 void InitBiTree(Bitree *
T){
53
54 int i,j,m;
55 char ch;
56 printf(
"请输入结点个数:\n");
57 scanf(
"%d",&
m);
58 T->n=m;
//不是二重指针
59 printf(
"请输入结点的值和其双亲的序号:\n");
60
61 for(i=
0;i<T->n;i++
)
62 {
63 getchar();
//!!!会把回车直接读进去
64 scanf(
"%c,%d",&ch,&
j);
65 T->node[i].data=
ch;
66 T->node[i].parent=
j;
67
68 }
69
70 }
71
72 int Findparent(Bitree T,
int e){
73 if(e==
0){
74 printf(
"根节点没有parent");
75 exit(-
1);
76 }
77 else
78 return T.node[e].parent;
79 }
转载于:https://www.cnblogs.com/lovecodepql/p/7692019.html
转载请注明原文地址: https://win8.8miu.com/read-1489743.html