源代码连接: http://www.oschina.net/code/snippet_2348884_47615ios
1、题目算法
用二叉树的数据结构实现一个简易实用的族谱。主要有建立,插入,查询,删除,修改信息,计算代数,打印,写入,读取重要人物信息的功能。数据结构
2、数据结构与算法函数
数据结构:测试
利用二叉树的链式存储结构,以及结构体,类,栈,队列完成。this
族谱表示二叉树结构如 图1;spa
族谱二叉树结构中结点所包含的信息如 表 1 所示.net
表 1 族谱二叉树结点信息指针
结构体分为:code
1 结点结构体(我的姓名,兄弟结点指针,儿女结点指针,父亲结点指针,妻子(们)信息指针,我的信息结构体);结构体分为:
2 我的信息结构体(父母姓名,性别,状态(去世 or 健在),处于家谱中第几代,出生日期,死亡日期,选择性重要信息指针);
类分为
1 日期类 (年,月,日,判断大小, 计算享年,修改,打印格式)
须要用到栈和队列:
1 实现遍历功能;
2 实现查找功能
算法:
一、二叉树的层次遍历,利用队列实现。
二、凹入式目录打印族谱,利用递归,非递归两种实现。
三、查找基于遍历功能。
四、删除,修改基于查找功能。
五、文件读写
重要头文件(包含部分重要结构体,所有函数定义)
#include<iostream> #include<string> #include<queue> #include<vector> #include <algorithm> #include<time.h> #include<fstream> #include "Date.h" using namespace std; //妻子信息 struct Wife_Information { string name; bool divorce; Wife_Information() { name = ""; divorce = false; }; Wife_Information(string name_) { name = name_; divorce = false; }; }; //我的信息 struct Member_Information{ string Sex; //性别 bool IsLife; vector<Wife_Information> wifves; string father_name; string mother_name; int generation; Date birth; Date death; string VIP_Information; // 若是这我的是个名人或者是对家族及其重要的人,新增信息缓冲区,存入该人平生事迹 // Member_Information() { Sex = "undefine"; VIP_Information = ""; generation = 0; IsLife = true; }; //先列基本信息,更多的信息能够之后再加进去 }; struct Tree{ string name; Tree *left; //儿子或女儿 Tree *right; //兄弟 Tree *father; //父亲 Tree *brother; vector<Wife_Information> wifves; //妻子(们)的信息 Member_Information self; // 本人信息 Tree() { name = ""; left = right = father = brother = 0; self.generation = 0; }; Tree(string name_,Member_Information s, Tree* left_=0, Tree* right_=0, Tree* father_=0, Tree* brother_=0) { name = name_; left = left_; right = right_; father = father_; brother = brother_; self = s; }; }; // fundamental operation // void Create_Tree(Tree* &tr, string name); Tree* Search(Tree *tr, string name); int Count_Generation_All(Tree* tr); // Insert // string Insert_Wife(Tree* &tr, string name, string husband); string Insert_Children(Tree* &tr, string name, string father); string Insert_Brother_Or_Sister(Tree* &tr, string name,string brother_sister); // add brief member informations or change informations // void Change_Information(Tree* tr); void Change_Name(Tree* &tr, string name, string new_name); void Change_Wives_Information(Tree* &tr, string name, string wife); void Change_Sex(Tree* &tr, string name, string sex ); void Change_IsLife(Tree* &tr, string name, bool islife); void Change_Fathername(Tree* &tr, string name, string father_name); void Change_Mothername(Tree* &tr, string name, string mother_name); void Change_Birth(Tree* &tr, string name, string date); void Change_Festa(Tree* &tr, string name, string date); void Change_VIP(Tree* &tr, string name); // Delete // string Divorce(Tree* &tr, string name, string husband); string Delete_Member(Tree* &tr, string name); void Delete_SubTree(Tree* &tr); // User Search (if user find someone and this person is VIP ,ask if there is need to print the VIP's information) // bool Search_Oneself(Tree* tr, string name); bool Search_One_Parents(Tree* tr, string name); bool Search_One_Children(Tree* tr, string name); bool Search_One_Wife(Tree* tr, string name); bool Search_One_Brother_And_Sister(Tree* tr, string name); int Count_Ones_Age(Tree* tr, string name); // Print // void Print_Information(Tree* tr); void Print_VIP_Information(Tree* tr); void Print_All(Tree* tr); void Print_MainMenu(); void Print_Add_Information_Menu();
一些测试: