void findFamilyTree(pTreeNode root) { pTreeNode target = NULL; char name[MAX] = { 0 }; printf("-------------------------------------------------\n"); printf(" \n 查找家谱成员 \n"); printf(" 输入姓名:"); scanf("%s", name); target = recursiveFamilyTreeNodeFind(root, name); if (target) { int* brother_line = (int*)calloc(1024, sizeof(int)); printf("---------------\n"); showFamilyMemberTitle(); showFamilyMember(&target->member, 1); printf("---------------\n"); printf("【所有孩子】\n"); recursiveFamilyTreeNodeShow(target, brother_line, 0, 0); free(brother_line); printf("【所有祖先】\n"); showFamilyMemberTitle(); while (target->parent) { target = target->parent; showFamilyMember(&target->member, 1); } } else { printf(" 没有找到相关信息!\n"); } printf("--------------------------------------------------\n");
时间: 2023-06-26 15:04:22 浏览: 33
这是一个基于树形结构的家谱查找程序。程序首先接收用户输入的要查找的姓名,然后通过递归查找函数 `recursiveFamilyTreeNodeFind()` 在家谱树中查找该成员。如果查找到了该成员,程序会输出该成员的信息,并递归遍历该成员的所有子孙节点,输出其所有孩子的信息。之后程序还会递归遍历该成员的所有祖先节点,输出其所有祖先的信息。如果没有找到相关信息,程序会输出提示信息。
相关问题
用C语言实现二叉树的链式存储和基本操作
二叉树是一种非常重要的数据结构,它可以用链式存储和顺序存储两种方式实现。链式存储是指用指针来表示二叉树的结构,而顺序存储则是用数组来表示二叉树的结构。在C语言中,我们通常使用链式存储来实现二叉树。
二叉树的基本操作包括建立、遍历、计算深度、结点数、叶子数等。其中,建立二叉树可以使用前序遍历或中序遍历或后序遍历的方式来实现。下面是用前序遍历的方式来创建二叉树的代码:
```
typedef struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode, *pTreeNode;
pTreeNode createTree() {
char ch;
scanf("%c", &ch);
if (ch == '#') {
return NULL;
}
pTreeNode root = (pTreeNode)malloc(sizeof(TreeNode));
root->data = ch;
root->left = createTree();
root->right = createTree();
return root;
}
```
遍历二叉树有三种方式:前序遍历、中序遍历和后序遍历。下面是三种遍历方式的代码:
```
void preOrder(pTreeNode root) {
if (root == NULL) {
return;
}
printf("%c ", root->data);
preOrder(root->left);
preOrder(root->right);
}
void inOrder(pTreeNode root) {
if (root == NULL) {
return;
}
inOrder(root->left);
printf("%c ", root->data);
inOrder(root->right);
}
void postOrder(pTreeNode root) {
if (root == NULL) {
return;
}
postOrder(root->left);
postOrder(root->right);
printf("%c ", root->data);
}
```
计算二叉树的深度可以使用递归的方式来实现。下面是计算二叉树深度的代码:
```
int depth(pTreeNode root) {
if (root == NULL) {
return 0;
}
int leftDepth = depth(root->left);
int rightDepth = depth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
```
计算二叉树的结点数和叶子数也可以使用递归的方式来实现。下面是计算二叉树结点数和叶子数的代码:
```
int countNodes(pTreeNode root) {
if (root == NULL) {
return 0;
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
int countLeaves(pTreeNode root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaves(root->left) + countLeaves(root->right);
}
```
c语言 创建二叉树。求二叉树的叶子节点。求二叉树的深度
以下是C语言实现二叉树叶子节点和深度的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结构体
typedef struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode, *pTreeNode;
// 创建二叉树
pTreeNode createTree() {
int data;
scanf("%d", &data);
if (data == -1) { // 输入-1表示该节点为空
return NULL;
}
pTreeNode node = (pTreeNode)malloc(sizeof(TreeNode));
node->data = data;
node->left = createTree();
node->right = createTree();
return node;
}
// 求二叉树叶子节点
int getLeafNode(pTreeNode root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return getLeafNode(root->left) + getLeafNode(root->right);
}
// 求二叉树深度
int getDepth(pTreeNode root) {
if (root == NULL) {
return 0;
}
int leftDepth = getDepth(root->left);
int rightDepth = getDepth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
int main() {
printf("请输入二叉树的节点,-1表示该节点为空:\n");
pTreeNode root = createTree();
printf("二叉树叶子节点数为:%d\n", getLeafNode(root));
printf("二叉树深度为:%d\n", getDepth(root));
return 0;
}
```