帮我写一份完整的c语言代码完成二叉树二叉链表结构的定义 ,实现二叉链表创建二叉树(尝试用递归算法实现),并实现先序、中序和后序遍历算法。实现二叉树求结点个数,求树的高度,求叶子结点数、交换二叉树左右子树的算法。
时间: 2023-06-09 13:05:03 浏览: 124
好的,以下是完整的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode{
int data;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;
// 创建二叉树
TreeNode* createTree(){
TreeNode* pNode = NULL;
int data;
scanf("%d", &data);
if(data == -1){
pNode = NULL;
}else{
pNode = (TreeNode*)malloc(sizeof(TreeNode));
pNode->data = data;
pNode->left = createTree();
pNode->right = createTree();
}
return pNode;
}
// 先序遍历
void preOrder(TreeNode* root){
if(root){
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
// 中序遍历
void inOrder(TreeNode* root){
if(root){
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}
// 后序遍历
void postOrder(TreeNode* root){
if(root){
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}
// 求结点个数
int getNodeCount(TreeNode* root){
if(root == NULL){
return 0;
}else{
return getNodeCount(root->left) + getNodeCount(root->right) + 1;
}
}
// 求树的高度
int getHeight(TreeNode* root){
if(root == NULL){
return 0;
}else{
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
}
// 求叶子结点数
int getLeafCount(TreeNode* root){
if(root == NULL){
return 0;
}else if(root->left == NULL && root->right == NULL){
return 1;
}else{
return getLeafCount(root->left) + getLeafCount(root->right);
}
}
// 交换左右子树
void swap(TreeNode* root){
TreeNode* temp;
if(root == NULL){
return;
}else{
swap(root->left);
swap(root->right);
temp = root->left;
root->left = root->right;
root->right = temp;
}
}
int main(){
printf("请输入二叉树结构(用-1表示空结点):\n");
TreeNode* root = createTree();
printf("先序遍历:");
preOrder(root);
printf("\n");
printf("中序遍历:");
inOrder(root);
printf("\n");
printf("后序遍历:");
postOrder(root);
printf("\n");
printf("结点个数:%d\n", getNodeCount(root));
printf("树的高度:%d\n", getHeight(root));
printf("叶子结点数:%d\n", getLeafCount(root));
printf("交换左右子树后的先序遍历:");
swap(root);
preOrder(root);
printf("\n");
return 0;
}
```
希望上面的代码能够满足您的需求,如果您还有其他问题需要解答,请继续提问。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)