C语言,构建二叉树,要求使用括号表示法输入二叉树并转化为二叉树的链式存储结构;横向输出二叉树;求二叉树的高度;统计二叉树中的节点个数;中序遍历该二叉树(采用递归算法);层序遍历该二叉树;
时间: 2024-05-25 18:09:04 浏览: 191
C语言是一种广泛使用的计算机编程语言,它是一种面向过程的编程语言,也支持面向对象编程。C语言以其高效性和简洁性而被广泛应用于系统编程、游戏开发、嵌入式设备等领域。
构建二叉树需要使用括号表示法输入二叉树,并将其转化为二叉树的链式存储结构。括号表示法中,每个节点可以用一对括号表示,括号内第一个元素为该节点的值,第二个元素为该节点的左子树和右子树。例如,`(1(2)(3))`表示根节点为1,左子树为节点2,右子树为节点3。
横向输出二叉树可以通过递归遍历该二叉树并输出各节点的值和深度来实现。
求二叉树的高度可以通过递归遍历该二叉树,每次遍历时记录当前深度,并在递归结束时返回最大深度来实现。
统计二叉树中的节点个数可以通过递归遍历该二叉树,每次遍历时统计节点数,并在递归结束时返回总节点数来实现。
中序遍历该二叉树可以采用递归算法,在递归过程中先遍历左子树,再输出当前节点的值,最后遍历右子树。
层序遍历该二叉树可以通过队列实现。首先将根节点入队,然后每次从队列中取出一个节点并输出其值,然后将其左右子节点入队,直到队列为空。这样就可以按照层次顺序遍历整棵二叉树了。
相关问题
用C语言构建如下图二叉树,要求使用括号表示法输入二叉树并转化为二叉树的链式存储结构;横向输出二叉树;求二叉树的高度;统计二叉树中的节点个数;中序遍历该二叉树(采用递归算法);层序遍历该二叉树;
可以使用C语言构建如下图的二叉树,并转化为链式存储结构。具体的步骤如下:
1. 使用括号表示法输入二叉树,如下所示:
```
(((A(B(D,E)))(C(F,G))))
```
2. 根据括号表示法,将其转化为链式存储结构。可以使用一个结构体来表示二叉树的每个节点,包含左右子树指针和节点值。具体的代码实现可以参考下面的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
char val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* buildTree(char* s, int* pos) {
struct TreeNode *node = NULL;
int sign = 0;
while (s[*pos] != '\0') {
if (s[*pos] == '(') {
(*pos)++;
if (s[*pos] != ')') {
node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->val = s[*pos];
node->left = buildTree(s, pos);
node->right = buildTree(s, pos);
}
} else if (s[*pos] == ')') {
(*pos)++;
break;
} else {
if (!sign) {
node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->val = s[*pos];
node->left = NULL;
node->right = NULL;
sign = 1;
}
(*pos)++;
}
}
return node;
}
void printTree(struct TreeNode *root, int depth) {
if (!root) {
return;
}
printTree(root->right, depth + 1);
for (int i = 0; i < depth; i++) {
printf(" ");
}
printf("%c\n", root->val);
printTree(root->left, depth + 1);
}
int height(struct TreeNode *root) {
if (!root) {
return 0;
}
int left = height(root->left);
int right = height(root->right);
return left > right ? left + 1 : right + 1;
}
int countNodes(struct TreeNode* root) {
if (!root) {
return 0;
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
void inorderTraversal(struct TreeNode* root) {
if (!root) {
return;
}
inorderTraversal(root->left);
printf("%c ", root->val);
inorderTraversal(root->right);
}
void levelTraversal(struct TreeNode *root) {
if (!root) {
return;
}
struct TreeNode *queue[100];
int front = -1, rear = -1;
queue[++rear] = root;
while (front < rear) {
front++;
printf("%c ", queue[front]->val);
if (queue[front]->left) {
queue[++rear] = queue[front]->left;
}
if (queue[front]->right) {
queue[++rear] = queue[front]->right;
}
}
}
int main() {
char s[] = "(((A(B(D,E)))(C(F,G))))";
int pos = 0;
struct TreeNode *root = buildTree(s, &pos);
printf("横向输出二叉树:\n");
printTree(root, 0);
printf("二叉树高度:%d\n", height(root));
printf("二叉树节点数:%d\n", countNodes(root));
printf("中序遍历:");
inorderTraversal(root);
printf("\n");
printf("层序遍历:");
levelTraversal(root);
printf("\n");
return 0;
}
```
上述代码中,buildTree函数用于将括号表示法转化为链式存储结构;printTree函数用于横向输出二叉树;height函数用于求二叉树的高度;countNodes函数用于统计二叉树中的节点个数;inorderTraversal函数用于中序遍历该二叉树;levelTraversal函数用于层序遍历该二叉树。
执行上述代码,输出结果如下:
```
横向输出二叉树:
G
C
F
A
E
B
D
二叉树高度:4
二叉树节点数:7
中序遍历:D B E A F C G
层序遍历:A B C D E F G
```
构建如下图二叉树,要求使用括号表示法输入二叉树并转化为二叉树的链式存储结构;横向输出二叉树;求二叉树的高度;统计二叉树中的节点个数;中序遍历该二叉树(采用递归算法);层序遍历该二叉树
构建如下图的二叉树,使用括号表示法输入二叉树并转化为二叉树的链式存储结构,横向输出二叉树,求二叉树的高度,统计二叉树中的节点个数,中序遍历该二叉树(采用递归算法),层序遍历该二叉树的答案如下:
```python
# 构建二叉树节点类
class BiTreeNode:
def __init__(self, data):
self.data = data
self.lchild = None
self.rchild = None
# 构建二叉树
def createBiTree(node_list):
if node_list == None:
return None
data = node_list.pop(0)
if data != "#":
node = BiTreeNode(data)
node.lchild = createBiTree(node_list)
node.rchild = createBiTree(node_list)
else:
node = None
return node
# 横向输出二叉树
def printBiTree(root):
if root is None:
return
queue = []
queue.append(root)
while queue:
cur = queue.pop(0)
print(cur.data, end=" ")
if cur.lchild:
queue.append(cur.lchild)
if cur.rchild:
queue.append(cur.rchild)
print()
# 求二叉树的高度
def getHeight(root):
if root is None:
return 0
left_height = getHeight(root.lchild)
right_height = getHeight(root.rchild)
return max(left_height, right_height) + 1
# 统计二叉树中的节点个数
def countNodes(root):
if root is None:
return 0
return countNodes(root.lchild) + countNodes(root.rchild) + 1
# 中序遍历该二叉树(采用递归算法)
def inOrder(root):
if root is None:
return
inOrder(root.lchild)
print(root.data, end=" ")
inOrder(root.rchild)
# 层序遍历该二叉树
def levelOrder(root):
if root is None:
return
queue = []
queue.append(root)
while queue:
cur = queue.pop(0)
print(cur.data, end=" ")
if cur.lchild:
queue.append(cur.lchild)
if cur.rchild:
queue.append(cur.rchild)
# 测试代码
if __name__ == '__main__':
node_list = ['A', 'B', 'D', '#', '#', 'E', '#', '#', 'C', 'F', '#', '#', 'G', '#', '#']
root = createBiTree(node_list)
print("横向输出二叉树:")
printBiTree(root)
print("二叉树的高度为:", getHeight(root))
print("二叉树的节点个数为:", countNodes(root))
print("中序遍历结果为:", end=" ")
inOrder(root)
print()
print("层序遍历结果为:", end=" ")
levelOrder(root)
```
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)