FamilyTree.c
#ifndef FAMILYTREE_C
#define FAMILYTREE_C
#include "FamilyTree.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Init_Tree(struct BinNode *tree)
{
(tree) = NULL;
}
struct BinNode *Search_Tree(struct BinNode *T, char *name)
{
int i, j, count;
struct BinNode *Q[100];
i = j = 0;
if(T)
{
Q[j++] = T;
while(i<j)
{
while(Q[i]&& strcmp(Q[i]->data.Name, name)!=0)
{
if(Q[i] -> child)
{
Q[j++] = Q[i]->child;
}
Q[i] = Q[i] -> brother;
}
if(Q[i]&& strcmp(Q[i]->data.Name, name) == 0)
{
return Q[i];
}
i++;
}
}
return NULL;
}
struct BinNode *Insert(struct BinNode *root, ifo N)//由个人信息插出入家族树中
{
struct BinNode *head, *q;
if(root==NULL)//当根节点为空
{
root = (struct BinNode *)malloc(sizeof(BinNode));//开辟空间
root->brother = root->child = root->parent = NULL;//初始化指针
root-> data = N;//初始化信息
return root;//返回根节点
}
if(!strcmp(root->data.Father,N.Name))//当插入的是根节点的父亲
{
head = (struct BinNode *)malloc(sizeof(BinNode));//开辟空间
head->data = N;//初始化信息
head -> child = root;//孩子指针指向当前的根节点
head -> brother = NULL;//初始化指针
head -> parent = NULL;
while(root->brother)//将所有的兄弟节点的父亲指针更新
{
root->brother->parent = head;
root = root->brother;
}
root = head;//更新根节点
}
else if(strcmp(root->data.Father, N.Father)==0) //当要插入的节点是根节点的亲兄弟
{
head = (struct BinNode *)malloc(sizeof(BinNode));//开辟空间
head -> data = N;//初始化信息
root -> brother = head;//确定指针指向
head -> child = NULL;
head -> brother = NULL;
head -> parent = NULL;
}
else //当要插入的成员为家谱树中已有成员的孩子
{
head = Search_Tree(root, N.Father);//找到父亲指针的位置
if(head -> child == NULL)//当插入的为长子
{
q = (struct BinNode *)malloc(sizeof(struct BinNode));//开辟空间
q->data = N;//传递信息
head->child = q;//更新指针指向
head->child->brother = NULL;
head->child->parent = head;
head->child->child = NULL;
}
else//当插入的成员不是长子时
{
q = head -> child;
while(q -> brother)//找到要插入的位置
{
q = q -> brother;
}
q->brother = (struct BinNode *)malloc(sizeof(struct BinNode));//开辟空间
q->brother->data = N;//更新信息
q->brother->child = NULL;//更新指针信息
q->brother->parent = Search_Tree(root, N.Father);
q->brother->brother = NULL;
}
}
return root;//返回根节点
}
void Tranverse(struct BinNode *tree)
{
if(tree)
{
sw[pp ++] = tree -> data;
if(tree -> child) Tranverse(tree -> child);
if(tree -> brother) Tranverse(tree -> brother);
}
}
struct BinNode *Read_File(struct BinNode *root)
{
FILE *fp;
int i;
char name[121];
printf("\t\t查找家谱文件~\n");
sleep(1);
int ff=0;
if((fp=fopen("tree.txt","rb"))==NULL)
ff=1;
if(!ff)
{
if((fp=fopen("tree.txt","rb+"))==NULL)
{
printf("\t\tCannot open file !");
return ;
}
printf("\t\t文件打开成功。\n");
sleep(1);
fscanf(fp,"%d",&n);
pr=sr;
fread(pr,sizeof(ifo),n,fp);
fclose(fp);
for(i=0; i<n; i++)
{
root = Insert(root,sr[i]);
}
sleep(1);
printf("\t\t~~~~~~~~~~~~~^_^ 家谱树创建成功!!!~~~~~~~~~~~~\n\n");
sleep(1);
}
else
{
printf("\t\t~~~~~~~~~~~~~T_T 家谱树创建失败!!!~~~~~~~~~~~~\n\n");
sleep(1);
}
return root;
}
void Save_Data(struct BinNode *tree)
{
FILE *fp;
if((fp = fopen("tree.txt", "wb+")) == NULL)
{
printf("\t\tERROR\n");
return ;
}
pp = 0;
Tranverse(tree);
fprintf(fp, "%d", n);
pw = sw;
fwrite(pw, sizeof(ifo), n, fp);
fclose(fp);
sleep(1);
printf("\t\t~~~~~~~~~~~~~^_^ 文件创建成功!!!~~~~~~~~~~~~\n\n");
sleep(1);
}
void LeavelOrderTraverse_Tree(struct BinNode *tree)
{
if(tree)
{
int i;
printf("\t\t");
for(i = 0;i <= tree -> data.Degress;i++)
{
printf(" ");
}
printf("-%s\n",tree -> data.Name);
if(tree -> child)
{
LeavelOrderTraverse_Tree(tree -> child);
}
if(tree -> brother)
{
LeavelOrderTraverse_Tree(tree -> brother);
}
}
else printf("\t\t请重新建树\n");
}
void Print_child(struct BinNode *person)
{
printf("\t\t他的儿女为: ");
(person) = (person) -> child;
while((person) != NULL)
{
printf("%s\n", (person)->data.Name);
(person) = (person) -> brother;
}
}
/*输出父亲*/
void Print_parent(struct BinNode *person)
{
printf("\t\t他的父母为: ");
(person) = (person)-> parent;
printf("%s\n", (person)->data.Name);
}
/*查找个人信息*/
void OutputMessage(struct BinNode *tree, char name[])
{
struct BinNode *t;
t = Search_Tree((tree), name);
if(t != NULL)
{
printf("\t\t姓名:%s\n", (t)->data.Name);
printf("\t\t性别:%s\n", (t)->data.Sex);
printf("\t\t父亲姓名:%s\n", (t)->data.Father);
printf("\t\t辈分:%d\n", (t)->data.Degress);
printf("\t\t孩子个数:%d\n", (t)->data.Num);
printf("\t\t出生地:%s\n", (t)->data.Born);
printf("\t\t职业:%s\n", (t)->data.Job);
printf("\t\t毕业大学:%s\n", (t)->data.College);
}
else
{
printf("\t\t这个人不存在\n");
sleep(1);
}
}
/*查找直系*/
void Search_Di(struct BinNode *tree, char name[])
{
struct BinNode *x, *y;
x = Search_Tree(tree, name);
if(x == NULL)
{
printf("\t\t这个人不存在\n");
sleep(1);
}
else
{
y = x;
if((x) -> parent == NULL&&(x) -> child != NULL)//无父有子
{
printf("\t\t他是整个家族的最长者\n");
Print_child(x);
}
else if((x) -> parent == NULL&&(x) -> parent == NULL)//无父无子
{
printf("\t\t他是个孤儿\n");
}
else if((x) -> parent != NULL&&(x) -> child == NULL)//有父无子
{
printf("\t\t他没有子女\n");
Print_parent(x);
}
else if((x) -> parent != NULL&&(x) -> child != NULL)//有父有子
{
Print_parent(x);
Print_child<