二叉树家谱系统

基本内容

实验内容:基于二叉树来存储家谱关系,同时实现1.后序递归遍历算法。2.前序非递归遍历算法。3.输入指定人物代号,查询其所有长辈的功能。

首先需要将要存储的族谱转化为输入的字符串,再通过输入的字符串创建族谱二叉树。

规定输入形式为括号表达式:A(B(C,D(,E)),)

其表示的树为

程序架构

主程序

首先输入括号表达式,然后通过该括号表达式,初始化一个树,随后调用前序遍历非递归函数,后序遍历递归函数,以及输入一个人物代号,找到其所有长辈,验证程序的正确性。

树模板类

  • 二叉树的建立
    • 分析:树的每个节点包含三个域,一个数据域,一个指向左孩子的指针,一个指向右孩子的指针。包含一个节点指针变量,指向根节点。
    • 通过输入的括号表达式,逐个字符判断,若遇到左括号,表示下一个遇到的字符为左孩子,则k=1,同时表示该节点拥有子节点,则将该节点入栈;若遇到逗号,表示下一个遇到的字符为右孩子,则k=2;若遇到右括号表示该块读取完成。若读取为字符,则通过k的值,添加栈顶的节点左右孩子节点。
    • template<typename T>
      void BTree<T>::init(T c[]) {
      	char ch;
      	BNode<T> *stack[MaxSize], *p;
      	int top = -1, k = 0, j = 0;
      	while ((ch = c[j++]) != '\0') {
      		switch (ch) {
      			case '(': {
      				top++;
      				stack[top] = p;
      				k = 1;
      				break;
      			}
      			case ',': {
      				k = 2;
      				break;
      			}
      			case ')': {
      				top--;
      				break;
      			}
      			default: {
      				p = new BNode<T>;
      				p->data = ch;
      				p->lchild = p->rchild = NULL;
      				if (this->b == NULL) {
      					this->b = p;
      				} else {
      					switch (k) {
      						case 1:
      							stack[top]->lchild = p;
      							break;
      						case 2:
      							stack[top]->rchild = p;
      							break;
      					}
      				}
      			}
      		}
      	}
      }
  • 后序遍历递归算法
    • template<typename T>
      void BTree<T>::PostOrderRe(BNode<T> *b) {
      	if (b != NULL) {
      		PostOrderRe(b->lchild);
      		PostOrderRe(b->rchild);
      		cout << b->data << " ";
      	}
      }
  • 前序遍历非递归算法
    • 非递归实现,则使用栈来存储遍历过的父节点。
    • template<typename T>
      void BTree<T>::PreOrderNotRe() {
      	cout << "先序遍历非递归算法输出为:";
      	BNode<T> *stack[MaxSize];
      	BNode<T> *p = this->b;
      	int top = -1;
      	while (p != NULL || top != -1) {
      		if (p != NULL) {
      			cout << p->data << " ";
      			top++;
      			stack[top] = p;
      			p = p->lchild;
      		} else {
      			p = stack[top];
      			top--;
      			p = p->rchild;
      		}
      	}
      	cout << endl;
      }
  • 得到要查询人物的所有祖先

    • 通过定义flag标志,查询是否找到了该被查询人物。通过递归后序遍历查询,若查询到了,则令flag=1,而回溯时,flag==1,则输出该节点,即为被查询人物祖先

    • template<typename T>
      void BTree<T>::FindAllAncestor(BNode<T> *b, char object) {
      	if (b != NULL) {
      		FindAllAncestor(b->lchild, object);
      		FindAllAncestor(b->rchild, object);
      		if (flag == 1) {
      			cout << b->data << " ";
      		}
      		if (b->data == object) flag = 1;
      	}
      }
      

程序验证

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二叉树是一种常用的数据结构,我们可以通过二叉树来实现家谱管理系统。下面是一个简单的C++代码示例: ```c++ #include <iostream> #include <string> using namespace std; // 家谱节点 struct Genealogy { string name; // 姓名 Genealogy *father; // 父亲节点指针 Genealogy *mother; // 母亲节点指针 Genealogy(string n) : name(n), father(NULL), mother(NULL) {} }; // 二叉树节点 struct TreeNode { Genealogy *data; // 数据 TreeNode *left; // 左子节点指针 TreeNode *right; // 右子节点指针 TreeNode(Genealogy *d) : data(d), left(NULL), right(NULL) {} }; // 插入节点 void insert(TreeNode *&root, Genealogy *data) { if (root == NULL) { root = new TreeNode(data); return; } if (data->name < root->data->name) { insert(root->left, data); } else if (data->name > root->data->name) { insert(root->right, data); } } // 查找节点 Genealogy *search(TreeNode *root, string name) { if (root == NULL) { return NULL; } if (root->data->name == name) { return root->data; } else if (name < root->data->name) { return search(root->left, name); } else { return search(root->right, name); } } // 打印家谱 void print(Genealogy *root, int level) { if (root == NULL) { return; } for (int i = 0; i < level; i++) { cout << "--"; } cout << root->name << endl; if (root->father != NULL) { print(root->father, level + 1); } if (root->mother != NULL) { print(root->mother, level + 1); } } int main() { TreeNode *root = NULL; // 插入节点 insert(root, new Genealogy("爷爷")); insert(root, new Genealogy("爸爸")); insert(root, new Genealogy("妈妈")); insert(root, new Genealogy("哥哥")); insert(root, new Genealogy("弟弟")); insert(root, new Genealogy("姐姐")); insert(root, new Genealogy("妹妹")); insert(root, new Genealogy("儿子")); insert(root, new Genealogy("女儿")); // 构建家谱 Genealogy *grandfather = search(root, "爷爷"); Genealogy *father = search(root, "爸爸"); Genealogy *mother = search(root, "妈妈"); Genealogy *son1 = search(root, "儿子"); Genealogy *daughter1 = search(root, "女儿"); Genealogy *son2 = new Genealogy("孙子"); Genealogy *daughter2 = new Genealogy("孙女"); insert(father->father, grandfather); father->mother = new Genealogy("奶奶"); mother->father = new Genealogy("外公"); mother->mother = new Genealogy("外婆"); insert(father, son1); insert(father, daughter1); insert(son1, son2); insert(daughter1, daughter2); // 打印家谱 print(grandfather, 0); return 0; } ``` 上述代码中,`Genealogy`结构体表示家谱节点,包括姓名和父亲母亲节点指针;`TreeNode`结构体表示二叉树节点,包括数据和左右子节点指针。`insert`函数用于插入节点,`search`函数用于查找节点,`print`函数用于打印家谱。在`main`函数中,先插入节点,然后根据关系构建家谱,最后打印家谱

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值