用c语言编写简历二叉树,输出二叉树,输出二叉树形状,先序遍历并打印,层次遍历并打印,计算节点数,计算叶子节点数,计算数的深度的相关代码
时间: 2023-08-10 14:04:33 浏览: 97
以下是用C语言编写的简历二叉树的相关代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构体
typedef struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode, *TreeNodePtr;
// 创建一个新的二叉树节点
TreeNodePtr createNode(int data) {
TreeNodePtr node = (TreeNodePtr) malloc(sizeof(TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
// 插入节点
TreeNodePtr insertNode(TreeNodePtr root, int data) {
if (root == NULL) {
root = createNode(data);
} else if (data < root->data) {
root->left = insertNode(root->left, data);
} else if (data > root->data) {
root->right = insertNode(root->right, data);
}
return root;
}
// 输出二叉树
void printTree(TreeNodePtr root, int level) {
if (root != NULL) {
printTree(root->right, level + 1);
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%d\n", root->data);
printTree(root->left, level + 1);
}
}
// 输出二叉树形状
void printShape(TreeNodePtr root, int level) {
if (root != NULL) {
printShape(root->right, level + 1);
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%d\n", root->data);
printShape(root->left, level + 1);
} else {
printf("\n");
}
}
// 先序遍历并打印
void preorderTraversal(TreeNodePtr root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// 层次遍历并打印
void levelTraversal(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
TreeNodePtr node = queue[front++];
printf("%d ", node->data);
if (node->left != NULL) {
queue[rear++] = node->left;
}
if (node->right != NULL) {
queue[rear++] = node->right;
}
}
}
// 计算节点数
int countNodes(TreeNodePtr root) {
if (root == NULL) {
return 0;
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
// 计算叶子节点数
int countLeaves(TreeNodePtr root) {
if (root == NULL) {
return 0;
} else if (root->left == NULL && root->right == NULL) {
return 1;
} else {
return countLeaves(root->left) + countLeaves(root->right);
}
}
// 计算数的深度
int countDepth(TreeNodePtr root) {
if (root == NULL) {
return 0;
}
int leftDepth = countDepth(root->left);
int rightDepth = countDepth(root->right);
return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
}
int main() {
// 创建二叉树
TreeNodePtr root = NULL;
root = insertNode(root, 10);
root = insertNode(root, 5);
root = insertNode(root, 15);
root = insertNode(root, 3);
root = insertNode(root, 7);
root = insertNode(root, 12);
root = insertNode(root, 17);
// 输出二叉树
printf("Binary Tree:\n");
printTree(root, 0);
// 输出二叉树形状
printf("Binary Tree Shape:\n");
printShape(root, 0);
// 先序遍历并打印
printf("Preorder Traversal:\n");
preorderTraversal(root);
printf("\n");
// 层次遍历并打印
printf("Level Traversal:\n");
levelTraversal(root);
printf("\n");
// 计算节点数
printf("Node Count: %d\n", countNodes(root));
// 计算叶子节点数
printf("Leaf Count: %d\n", countLeaves(root));
// 计算数的深度
printf("Tree Depth: %d\n", countDepth(root));
return 0;
}
```
阅读全文