#include<iostream>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
#define max 100
typedef char TElemType;
typedef struct
{
TElemType data;
int parent;
}PTNode;
typedef struct
{
PTNode node[max];
int n;
}PTree;
void InitTree(PTree &tree)
{
char ch;
int j;
cout << "请输入树的节点个数" << endl;
cin >> tree.n;
cout << "请输入结点的值和其双亲" << endl;
for (int i = 0; i < tree.n; i++)
{
fflush(stdin);
cin >> ch >> j;
tree.node[i].data = ch;
tree.node[i].parent = j;
}
tree.node[0].parent = -1;
}
void FindParent(PTree tree)
{
int i;;
cout << "请输入你要查找的结点" << endl;
cin >> i;
cout << i << "的双亲为:" << tree.node[i].parent << endl;
}
void FindChild(PTree tree)
{
int i;
char ch;
//cout << "请输入你想要查找孩子的双亲的序号" << endl;
cout << "请输入你想查找孩子双亲的值" << endl;
cin >> ch;
for (i = 0; i < tree.n; i++)
{
if (ch == tree.node[tree.node[i].parent].data)
{
cout << "该孩子的值为:";
cout << tree.node[i].data << endl;
}
}
return;
}
int main()
{
PTree tree;
InitTree(tree);
//for (int i = 0; i < 10; i++)
//FindParent(tree);
for(int i=0;i<3;i++)
FindChild(tree);
return 0;
}