使用C语言,(1)用三种遍历算法遍历该二叉树,给出对应的输出结果。 (2) 写一个函数查找二叉树中的节点,根据其是否属于该树,输出true或者false。 (3) 统计该树的满节点、叶子节点、总节点数。
时间: 2024-12-20 13:21:41 浏览: 7
您的请求涉及到二叉树的基本操作,以下是针对每个任务的具体实现:
### 1. 使用三种遍历算法遍历二叉树
假设我们有一个简单的二叉树结构如下:
```c
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
```
#### 前序遍历 (Preorder Traversal)
```c
void preorderTraversal(TreeNode* root) {
if (root == NULL) return;
printf("%d ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
```
#### 中序遍历 (Inorder Traversal)
```c
void inorderTraversal(TreeNode* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->val);
inorderTraversal(root->right);
}
```
#### 后序遍历 (Postorder Traversal)
```c
void postorderTraversal(TreeNode* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->val);
}
```
### 2. 查找二叉树中的节点
```c
bool searchNode(TreeNode* root, int target) {
if (root == NULL) return false;
if (root->val == target) return true;
return searchNode(root->left, target) || searchNode(root->right, target);
}
```
### 3. 统计树的满节点、叶子节点、总节点数
```c
int countFullNodes(TreeNode* root) {
if (root == NULL) return 0;
int count = 0;
if (root->left != NULL && root->right != NULL) count++;
count += countFullNodes(root->left);
count += countFullNodes(root->right);
return count;
}
int countLeaves(TreeNode* root) {
if (root == NULL) return 0;
if (root->left == NULL && root->right == NULL) return 1;
return countLeaves(root->left) + countLeaves(root->right);
}
int countTotalNodes(TreeNode* root) {
if (root == NULL) return 0;
return 1 + countTotalNodes(root->left) + countTotalNodes(root->right);
}
```
### 示例代码
假设我们有一棵如下所示的二叉树:
```
1
/ \
2 3
/ \ \
4 5 6
```
可以创建这棵树并调用上述函数进行测试:
```c
#include <stdio.h>
// 定义二叉树节点结构
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建新节点
TreeNode* createNode(int val) {
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int main() {
// 创建示例二叉树
TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->right = createNode(6);
// 测试遍历
printf("Preorder traversal: ");
preorderTraversal(root);
printf("\n");
printf("Inorder traversal: ");
inorderTraversal(root);
printf("\n");
printf("Postorder traversal: ");
postorderTraversal(root);
printf("\n");
// 测试查找节点
int target = 5;
if (searchNode(root, target)) {
printf("Node %d found.\n", target);
} else {
printf("Node %d not found.\n", target);
}
// 统计节点数量
printf("Number of full nodes: %d\n", countFullNodes(root));
printf("Number of leaves: %d\n", countLeaves(root));
printf("Total number of nodes: %d\n", countTotalNodes(root));
return 0;
}
```
运行上述代码将输出:
```
Preorder traversal: 1 2 4 5 3 6
Inorder traversal: 4 2 5 1 3 6
Postorder traversal: 4 5 2 6 3 1
Node 5 found.
Number of full nodes: 2
Number of leaves: 3
Total number of nodes: 6
```
这些函数实现了二叉树的前序、中序和后序遍历,查找特定节点,并统计满节点、叶子节点和总节点数。希望这对您有所帮助!
阅读全文