题目:
编写一个程序,采用一个二叉树表示一个家谱关系(由若干个家谱记录构成,每个家谱记录由丈夫、妻子的姓名组成),每家最多两个孩子,要求程序具有以下功能。
(1) 查找某人的孩子们。
(2) 查找某人的所有祖先(包括直系和旁系)。
(3) 查找某人同一辈分的所有兄弟姊妹。
![](https://i-blog.csdnimg.cn/blog_migrate/dc6d4d4979682a0fd0b4de325b856614.png)
实现代码:
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
class ListNode
{
public:
string male;
string female;
ListNode* left;
ListNode* right;
ListNode* parents;
int num;
ListNode(int t, string s1, string s2) {
num = t;
male = s1;
female = s2;
left = nullptr;
right = nullptr;
parents = nullptr;
}
};
ListNode* root1;
ListNode* root2;
ListNode* root3;
ListNode* root4;
ListNode* root5;
ListNode* root6;
ListNode* root7;
string name[7] = { "苏序", "苏涣", "苏洵","苏不欺", "苏不疑" , "苏轼" , "苏辙" };
void child()
{
string s;
int i = 0;
cout << "请输入姓名" << endl;
cin >> s;
for (i = 0; i < 7; i++)
{
if (s == name[i])
break;
}
i = i + 1;
if (i == 1)
{
cout << root2->male << "+" << root2->female << endl;
cout << root3->male << "+" << root3->female << endl;
}
if (i == 2)
{
cout << root4->male << "+" << root4->female << endl;
cout << root5->male << "+" << root5->female << endl;
}
if (i == 3)
{
cout << root6->male << "+" << root6->female << endl;
cout << root7->male << "+" << root7->female << endl;
}
if (i == 4 || i == 5 || i == 6 || i == 7)
{
cout << "空" << endl;
}
cout << "输入任意字符返回" << endl;
_getch();
}
void ancestors()
{
string s;
int i = 0;
cout << "请输入姓名" << endl;
cin >> s;
for (i = 0; i < 7; i++)
{
if (s == name[i])
break;
}
i = i + 1;
if (i == 1)
{
cout << "空" << endl;
}
if (i == 2 || i== 3)
{
cout << root1->male << "+" << root1->female << endl;
}
if (i == 4|| i == 5 || i == 6 || i == 7)
{
cout << root1->male << "+" << root1->female << endl;
cout << root2->male << "+" << root2->female << endl;
cout << root3->male << "+" << root3->female << endl;
}
cout << "输入任意字符返回" << endl;
_getch();
}
void peer()
{
string s;
int i = 0;
cout << "请输入姓名" << endl;
cin >> s;
for (i = 0; i < 7; i++)
{
if (s == name[i])
break;
}
i = i + 1;
if (i == 1)
{
cout << "空" << endl;
}
if (i == 2 || i == 3)
{
cout << root2->male << "+" << root2->female << endl;
cout << root3->male << "+" << root3->female << endl;
}
if (i == 4 || i == 5|| i == 6 || i == 7)
{
cout << root4->male << "+" << root4->female << endl;
cout << root5->male << "+" << root5->female << endl;
cout << root6->male << "+" << root6->female << endl;
cout << root7->male << "+" << root7->female << endl;
}
cout << "输入任意字符返回"<< endl;
_getch();
}
int main()
{
while (1)
{
root1 = new ListNode(1, "苏序", "史氏");
root1->left = new ListNode(2, "苏涣", "杨氏");
root1->left->parents = root1;
root2 = root1->left;
root1->right = new ListNode(3, "苏洵", "程氏");
root1->right->parents = root1;
root3 = root1->right;
root1->left->left = new ListNode(4, "苏不欺", "蒲氏");
root1->left->left->parents = root1->left;
root4 = root1->left->left;
root1->left->right = new ListNode(5, "苏不疑", "");
root1->left->right->parents = root1->left;
root5 = root1->left->right;
root1->right->left = new ListNode(6, "苏轼", "王弗");
root1->right->left->parents = root1->right;
root6 = root1->right->left;
root1->right->right = new ListNode(7, "苏辙", "史氏");
root1->right->right->parents = root1->right;
root7 = root1->right->right;
cout << "请选择功能:" << endl;
cout << "1.查找孩子们" << endl;
cout << "2.查找祖先" << endl;
cout << "3.查找兄弟姐妹" << endl;
int i = 0;
cin >> i;
if (i == 1) child();
if (i == 2) ancestors();
if (i == 3) peer();
}
}