数据结构小作业---家族族谱

家族族谱树


#include 
#include
#include
#include
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){
            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<<"输入一个姓名"<>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、事迹)"<>option;
    switch (option) {
        case 1:
            cout<<"请输入你的姓名"<>Q->name;
            break;
        case 2:
            cout<<"请输入你要修改性别"<>Q->sex;
            break;
        case 3:
            cout<<"请输入所要修改的生日"<>Q->birthday;
            break;
        case 4:
            cout<<"请输入你所要修改的配偶姓名"<>Q->spouse;
            break;
        case 5:
            cout<<"请输入你要修改的事迹"<>Q->introdeuce;
    }
}

void TreeNodeChange(TreeNode *&Q){
    int change;
    cout<<"选择修改的内容(1、姓名     2、性别      3、生日     4、事迹)"<>change;
    switch(change){
        case 1:{
            cout<<"输入修改的姓名"<>Q->name;
            cout<<"修改成功"<>Q->sex;
            cout<<"修改成功"<>Q->birthday;
            cout<<"修改成功"<>Q->introdeuce;
            cout<<"修改成功"<pre->Root){
        cout<<"他的父亲的姓名为"<pre->name<pre->sex<pre->birthday<pre->spouse<pre->introdeuce;
    }
}
void FindTreeNodeChild(TreeNode *&Q){
    TreeNode *p;
    int k=1;
    if(Q->childnum>1){
        p=Q->child;
        cout<<"第"<name<next!=NULL){
            p=p->next;
            cout<<"第"<name<name<childnum==1)
        cout<<"他的孩子为"<child->name<child;
    if(Q->childnum==1){
        delete Q->child;
        Q->childnum=0;
        Q->child=NULL;
        cout<<"删除成功"<childnum>1){
        if(p->name==ch){
            Q->child=p->next;
            Q->childnum--;
            cout<<"删除成功"<next!=NULL){
            t=p;
            p=p->next;
            if(p->name==ch){
               t->next=p->next;
                Q->childnum--;
                delete p;
                cout<<"删除成功"<>option;
        switch (option) {
            case 1:{
                if(EmptyTree(root))
                    AddTreeNode(root, root);
                else{
                    cout<<"输入插入人的父亲姓名"<>ch;
                    SearchTreeNode(root, Q, ch);
                    if(found)
                        AddTreeNode(root, Q);
                    else
                        cout<<"暂无改父亲信息"<>ch;
                SearchTreeNode(root, Q, ch);
                if(found)
                    cout<name<>ch;
                SearchTreeNodeIntrodeuce(root, Q, ch);
                if(found)
                    cout<name<>ch;
                SearchTreeNode(root, Q, ch);
                if(found)
                    TreeNodeChange(Q);
                else
                    cout<<"未能查到"<>ch;
                SearchTreeNode(root, Q, ch);
                if(found){
                    TreeNode *p;
                    p=Q->pre;
                    DeleteTreeNode(p, ch);
                  
                }
                else cout<<"未能查到此人"<>ch;
                SearchTreeNode(root, Q, ch);
                if(found){
                    if(!Q->pre->Root){
                        cout<<"他的父亲是"<pre->name<>ch;
                SearchTreeNode(root, Q, ch);
                if(found)
                    FindTreeNodeChild(Q);
                else
                    cout<<"未能查到此人"<

 

你可能感兴趣的:(树)