家族族谱树
#include <iostream>
#include<string>
#include<iomanip>
#include<cstring>
using namespace std;
bool found=false;
typedef long long int ll;
int next1[1000];
int number=0;
string ch;
typedef struct node{
string name;
char sex;
int birthday;
string introdeuce;
string spouse;
node *child;
node *pre;
node *next;
int childnum;
bool Root;
bool adjective;
}TreeNode;
TreeNode *Q;
void KMP_Pre(string ch,ll m,int next[1000]){ //ch为匹配串
int i,j;
i=j=next[0]=-1;
i=0;
while(i<m){
while(-1!=j&&ch[i]!=ch[j])
j=next[j];
next[++i]=++j;
}
return ;
}
bool KMP(string ch,ll m,string sh,ll n){ //匹配事迹
int i,j;
KMP_Pre(ch, m, next1);
i=j=0;
while(i<n){
while(j!=-1&&sh[i]!=ch[j])
j=next1[j];
i++;
j++;
if(j>=m){
return true;
}
}
return false;
}
bool SameTreeNode(string sh,string ch){
KMP_Pre(ch,ch.length(),next1);
if(KMP(ch, ch.length(), sh, sh.length()))
return true;
return false;
}
void InsertTreeNode (TreeNode *&root){
root=new TreeNode;
root->childnum=0;
root->Root=true;
root->next=NULL;
root->pre=NULL;
root->child=NULL;
root->name="";
}
bool EmptyTree(TreeNode *&root){
if(root->child==NULL)
return true;
return false;
}
void AddTreeNode (TreeNode *&root ,TreeNode *&Q){
TreeNode *p,*t=NULL;
p=NULL;
t=new TreeNode;
cout<<"输入一个姓名"<<endl;
cin>>t->name;
t->childnum=0;
// cin>>t->sex;
// cin>>t->birthday;
// cin>>t->spouse;
// cin>>t->adjective;
t->child=NULL;
t->next=NULL;
t->pre=Q;
if(Q->child!=NULL){
p=Q->child;
if(Q->childnum>1)
while(p->next!=NULL)
p=p->next;
p->next=t;
}
else
Q->child=t;
Q->childnum++;
}
void SearchTreeNode (TreeNode *&root,TreeNode *&Q,string ch){
if(root->name==ch){
Q=root;
found=true;
return;
}
if(root->next!=NULL){
TreeNode *p;
p=root->next;
while(p){
if(p->name==ch){
found=1;
Q=p;
return;
}
p=p->next;
}
}
if(root->child!=NULL){
SearchTreeNode(root->child, Q, ch);
}
if(root->next!=NULL){
TreeNode *p;
p=root->next;
while(p){
SearchTreeNode(p, Q, ch);
p=p->next;
}
}
}
void SearchTreeNodeIntrodeuce(TreeNode *&root,TreeNode *&Q,string ch){
if(SameTreeNode(root->introdeuce, ch)){
Q=root;
found=true;
return;
}
if(root->next!=NULL){
TreeNode *p;
p=root->next;
while(p){
if(SameTreeNode(p->name, ch)){
found=1;
Q=p;
return;
}
p=p->next;
}
}
if(root->child!=NULL){
SearchTreeNodeIntrodeuce(root->child, Q, ch);
}
if(root->next!=NULL){
TreeNode *p;
p=root->next;
while(p){
SearchTreeNodeIntrodeuce(p, Q, ch);
p=p->next;
}
}
}
void ChangeTreeNode (TreeNode *&Q){
cout<<"选择你要修改的信息(1、姓名 2、性别 3、生日、4、配偶 5、事迹)"<<endl;
int option;
cin>>option;
switch (option) {
case 1:
cout<<"请输入你的姓名"<<endl;
cin>>Q->name;
break;
case 2:
cout<<"请输入你要修改性别"<<endl;
cin>>Q->sex;
break;
case 3:
cout<<"请输入所要修改的生日"<<endl;
cin>>Q->birthday;
break;
case 4:
cout<<"请输入你所要修改的配偶姓名"<<endl;
cin>>Q->spouse;
break;
case 5:
cout<<"请输入你要修改的事迹"<<endl;
cin>>Q->introdeuce;
}
}
void TreeNodeChange(TreeNode *&Q){
int change;
cout<<"选择修改的内容(1、姓名 2、性别 3、生日 4、事迹)"<<endl;
cin>>change;
switch(change){
case 1:{
cout<<"输入修改的姓名"<<endl;
cin>>Q->name;
cout<<"修改成功"<<endl;
break;
}
case 2:{
cout<<"请输入修改的性别(男 :M 、女 W)"<<endl;
cin>>Q->sex;
cout<<"修改成功"<<endl;
break;
}
case 3:{
cout<<"请输入修改的生日"<<endl;
cin>>Q->birthday;
cout<<"修改成功"<<endl;
break;
}
case 4:{
cout<<"请输入修改的事迹"<<endl;
cin>>Q->introdeuce;
cout<<"修改成功"<<endl;
}
}
}
void FindTreeNodePre(TreeNode *&Q){
if(!Q->pre->Root){
cout<<"他的父亲的姓名为"<<Q->pre->name<<endl;
cout<<Q->pre->sex<<endl;
cout<<Q->pre->birthday<<endl;
cout<<Q->pre->spouse<<endl;
cout<<Q->pre->introdeuce;
}
}
void FindTreeNodeChild(TreeNode *&Q){
TreeNode *p;
int k=1;
if(Q->childnum>1){
p=Q->child;
cout<<"第"<<k++<<"个孩子"<<endl;
cout<<p->name<<endl;
while(p->next!=NULL){
p=p->next;
cout<<"第"<<k++<<"个孩子"<<endl;
cout<<p->name<<endl;
cout<<p->name<<endl;
}
}
else if(Q->childnum==1)
cout<<"他的孩子为"<<Q->child->name<<endl;
else
cout<<"他没有孩子"<<endl;
}
void DeleteTreeNode(TreeNode *&Q,string ch){
TreeNode *p;
p=Q->child;
if(Q->childnum==1){
delete Q->child;
Q->childnum=0;
Q->child=NULL;
cout<<"删除成功"<<endl;
return ;
}
else if(Q->childnum>1){
if(p->name==ch){
Q->child=p->next;
Q->childnum--;
cout<<"删除成功"<<endl;
return ;
}
TreeNode *t;
t=p;
while(p->next!=NULL){
t=p;
p=p->next;
if(p->name==ch){
t->next=p->next;
Q->childnum--;
delete p;
cout<<"删除成功"<<endl;
return ;
}
}
}
}
int main()
{
TreeNode *root;
InsertTreeNode(root);
InsertTreeNode(Q);
while(1){
found=false;
int option;
cout<<"选择操作(1.添加 2.查找(姓名) 3.查找(事迹) 4.修改 5.删除 6.查找父亲 7.查找子女 8.退出)"<<endl;
cin>>option;
switch (option) {
case 1:{
if(EmptyTree(root))
AddTreeNode(root, root);
else{
cout<<"输入插入人的父亲姓名"<<endl;
cin>>ch;
SearchTreeNode(root, Q, ch);
if(found)
AddTreeNode(root, Q);
else
cout<<"暂无改父亲信息"<<endl;
}
break;
}
case 2:{
cout<<"输入要查找的人的姓名"<<endl;
cin>>ch;
SearchTreeNode(root, Q, ch);
if(found)
cout<<Q->name<<endl;
else
cout<<"未能查到该人信息"<<endl;
break;
}
case 3:{
cout<<"输入你所查找人事迹的关键字"<<endl;
cin>>ch;
SearchTreeNodeIntrodeuce(root, Q, ch);
if(found)
cout<<Q->name<<endl;
else
cout<<"未能找到"<<endl;
}
case 4:{
cout<<"输入修改人的姓名"<<endl;
cin>>ch;
SearchTreeNode(root, Q, ch);
if(found)
TreeNodeChange(Q);
else
cout<<"未能查到"<<endl;
break;
}
case 5:{
cout<<"输入你需要删除的人的姓名"<<endl;
cin>>ch;
SearchTreeNode(root, Q, ch);
if(found){
TreeNode *p;
p=Q->pre;
DeleteTreeNode(p, ch);
}
else cout<<"未能查到此人"<<endl;
break;
}
case 6:{
cout<<"请输入你需要查找的人"<<endl;
cin>>ch;
SearchTreeNode(root, Q, ch);
if(found){
if(!Q->pre->Root){
cout<<"他的父亲是"<<Q->pre->name<<endl;
}
}
break;
}
case 7:{
cout<<"输入你要查找的人的姓名"<<endl;
cin>>ch;
SearchTreeNode(root, Q, ch);
if(found)
FindTreeNodeChild(Q);
else
cout<<"未能查到此人"<<endl;
break;
}
case 8:{
cout<<"欢迎再次使用"<<endl;
exit(1);
}
}
}
}