按凹入表形式横向打印二叉树结构c语言代码。各个功能分开实现

时间: 2023-12-03 21:45:51 浏览: 57
这里给出横向打印二叉树结构的 C 语言代码,实现了以下功能: 1.创建二叉树节点 2.插入二叉树节点 3.横向打印二叉树结构 ```c #include <stdio.h> #include <stdlib.h> // 二叉树节点 typedef struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 创建二叉树节点 TreeNode *createNode(int data) { TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); node->data = data; node->left = NULL; node->right = NULL; return node; } // 插入二叉树节点 TreeNode *insertNode(TreeNode *root, int data) { if (root == NULL) { return createNode(data); } else if (data < root->data) { root->left = insertNode(root->left, data); } else { root->right = insertNode(root->right, data); } return root; } // 计算树的高度 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); } } // 打印空格 void printSpaces(int n) { for (int i = 0; i < n; i++) { printf(" "); } } // 横向打印二叉树结构 void printTree(TreeNode *root, int level) { if (root == NULL) { return; } int height = getHeight(root); int indent = (1 << (height - level - 1)) - 1; printSpaces(indent); printf("%d", root->data); printSpaces(indent); if (root->left || root->right) { printf("\n"); printSpaces(indent); printf(root->left ? "/" : " "); printSpaces(indent * 2 - 1); printf(root->right ? "\\" : " "); printf("\n"); } printTree(root->left, level + 1); printTree(root->right, level + 1); } int main() { TreeNode *root = createNode(5); insertNode(root, 3); insertNode(root, 7); insertNode(root, 1); insertNode(root, 4); insertNode(root, 6); insertNode(root, 9); printTree(root, 0); return 0; } ```

相关推荐

最新推荐

recommend-type

按凹入表形式横向打印任意二叉树结构,即二叉树的根在屏幕的最左边,二叉树的左子树在屏幕的下边,二叉树的右子树在屏幕的上边。

按凹入表形式横向打印任意二叉树结构,即二叉树的根在屏幕的最左边,二叉树的左子树在屏幕的下边,二叉树的右子树在屏幕的上边。
recommend-type

打印二叉树结构课程设计

按凹入表形式横向打印任意二叉树结构,即二叉树的根在屏幕的最左边,二叉树的左子树在屏幕的下边,二叉树的右子树在屏幕的上边。
recommend-type

课设 - 平衡二叉树的演示 .docx

(2) 平衡二叉树的显示采用凹入表现形式。 (3)输入的形式时数字,无论对功能的选择还是对数据的录入,都是以数字的形式进行输入,无需使用文件保存数据。  (4) 输出的形式时在dos界面进行输出,一旦检测到...
recommend-type

数据结构综合课设设计一个哈夫曼的编/译码系统.docx

1.问题描述 利用哈夫曼编码进行通信可以提高信道利用率,缩短信息传输时间,降低传输成本。...将已在 中的哈夫曼树以直观的方式(树或凹入表形式)显示在终端上,同时将此字符形式的哈夫曼树写入文件TreePrint中。
recommend-type

ansys maxwell

ansys maxwell
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。