//BinaryTree_ne
#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstring>
using namespace std;
const int maxn = 110;
const int maxm = 710;
struct node {
int left, right;
int left_s, right_s;
int v, id, ak; //v便是value,存储结点的值, ak存储字符数组a的长度
char a[10]; //将value的值转化为字符串
}Nodes[maxn];
char map[maxn][maxn];
char a = '.', b = '-', c = '|', d = '\0';
void BinaryTree_set(int rt, int k) { //set,即为构造二叉排序树 ,rt表示根节点
if(Nodes[k].v > Nodes[rt].v) {
Nodes[rt].right_s++; //s,sum简写,表示此结点的右子树结点个数
if(Nodes[rt].right != -1) { //如果这个结点有右子树
BinaryTree_set(Nodes[rt].right, k); //递归进入右子树进行比较
}
else {
Nodes[rt].right = k; //如果这个结点没有右子树,则记录这个结点的下标
}
}
else {
Nodes[rt].left_s++; //若大于,则进入左子树,左子树结点数量+1
if(Nodes[rt].left != -1) { //若有记录左子树的根节点的位置
BinaryTree_set(Nodes[rt].left, k); //递归进入下一层进行比较
}
else {
Nodes[rt].left = k; //若没有,记录该节点的下标
}
}
}
void Id_set(int rid, int k) { //id表示这个应该在第几行,因为图的构造为右中左的中序遍历,为此要用改结点的右子树的结点个数进行计算
Nodes[k].id = rid + Nodes[k].right_s + 1; //标记数量是根节点的id加上右子树的结点个数+1,+1是为了不用map[0]
if(Nodes[k].right != -1) { //如果这个结点有右子树,递归进入下一层进行计算
Id_set(rid, Nodes[k].right);
}
if(Nodes[k].left != -1) { //如果这个结点有左子树,递归进入下一层进行计算
Id_set(Nodes[k].id, Nodes[k].left);
}
}
void Map_set(int k, int index) { //图的构造 ,index定位字符串结尾'\0'的位置
for(int i = 0; i < Nodes[k].ak; i++) {
map[ Nodes[k].id ][index + i] = Nodes[k].a[Nodes[k].ak - 1 - i]; //进行结点的值的填充
}
index += Nodes[k].ak; //字符串的结束下标要在值的后面,所以要加上字符数组a的长度
if(Nodes[k].left != -1 || Nodes[k].right != -1) { //如果这个结点有左子树或者右子树
map[Nodes[k].id][index++] = b; //就在应该填充'\0'的位置填充'-',并将index的数值+1
int max, min;
max = min = Nodes[k].id; //最大值最小值都为这个数应该打印的行数
if(Nodes[k].left != -1) { //如果这个结点有左子树
max = Nodes[Nodes[k].left].id; //最大值为这个左子树构图时所在行号,因为左子树在根节点的下面,为此左子树所在行号的数会大
map[Nodes[Nodes[k].left].id][index + 1] = b; //该结点左子树的行数在index+1的地方填充'-'
Map_set(Nodes[k].left, index + 2); //进行左子树的递归,在填充完'-'之后填充数组,即在index+2处
}
if(Nodes[k].right != -1) { //如果右子树不为空,原理同上
min = Nodes[Nodes[k].right].id;
map[Nodes[Nodes[k].right].id][index + 1] = b;
Map_set(Nodes[k].right, index + 2);
}
for(int i = min; i <= max; i++) { //在右子树所在行到左子树所在行的index的位置都填充'|'字符
map[i][index] = c;
}
map[Nodes[k].id][index + 1] = d; //填充字符串结束标志'\0'
}
else {
map[Nodes[k].id][index] = d; //填充字符串结束标志'\0'
return;
}
}
int main() {
int n = 0, e, A[maxn];
string s;
getline(cin, s);
istringstream ss(s);
while(ss >> e) {
A[n++] = e;
}
for(int i = 0; i < n; i++) {
e = A[i];
Nodes[i].left = Nodes[i].right = -1;
Nodes[i].left_s = Nodes[i].right_s = 0;
Nodes[i].ak = 0;
Nodes[i].v = e; //v即时value,表示这个结点的值
while(e) {
Nodes[i].a[Nodes[i].ak++] = e % 10 + '0';
e /= 10;
}
}
for(int i = 1; i < n; i++) {
BinaryTree_set(0, i);
}
Id_set(0, 0);
for(int i = 1; i <= n; i++) {
for(int j = 0; j < maxm; j++) {
map[i][j] = a; //全都填充为 '.'
}
}
Map_set(0, 0);
for(int i = 1; i <=n; i++) {
printf("%s\n", map[i]);
}
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的竞赛项目学习资料,作为参考学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 蓝桥杯实战源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
蓝桥杯实战源码.zip (91个子文件)
code_20105
L1_010.exe 1.83MB
huiwen.exe 1.83MB
L1_012.exe 1.85MB
10_to_16.exe 128KB
qsort_3.cpp 472B
test_istringstream.cpp 220B
L1_003.exe 1.83MB
L1_010.cpp 589B
qsort.exe 129KB
L1_014.cpp 97B
L1_011.exe 1.83MB
L1_004_2.exe 128KB
matrix_multi.cpp 662B
queue_net.cpp 1KB
L1_009.exe 1.9MB
lcm_tri.cpp 2KB
queue_saveoutput.exe 1.87MB
L1_005.cpp 1KB
huiwen2.cpp 543B
L1_002.exe 1.83MB
16_to_8.exe 128KB
.gitattributes 378B
16_to_8.cpp 302B
lcm_tri.exe 1.83MB
L1_015.exe 1.83MB
L1_009_net.cpp 757B
L1_012.cpp 126B
sort_find_k.exe 1.83MB
queue_operation.exe 1.87MB
L1_003_string.cpp 676B
M_way_floyed_net.cpp 1KB
queue_operation.cpp 475B
L1_013.exe 1.83MB
area_0f_circle.cpp 246B
4_10.cpp 495B
M_way_floyed.exe 1.83MB
convert_itoa.exe 1.83MB
area_0f_circle.exe 128KB
BinaryTree_ne.exe 1.91MB
L1_011_string.cpp 410B
L1_009.cpp 352B
matrix_multi.exe 1.83MB
L1_004.exe 1.83MB
L1_006.exe 1.84MB
16_to_10.exe 1.86MB
fibonacci_remainder.cpp 277B
shortmap0.exe 1.83MB
qsort.cpp 496B
16_to_10.cpp 402B
L1_004_2.cpp 120B
L1_002.cpp 682B
16_to_8_net.exe 1.86MB
L1_006.cpp 602B
16_to_8_net.cpp 821B
4_10_sub.cpp 381B
L1_003.cpp 462B
L1_008.cpp 427B
queue_saveoutput.cpp 647B
sort_find_k.cpp 770B
4_10_sub.exe 1.83MB
fibonacci_remainder.exe 128KB
M_way_floyed_net.exe 1.83MB
qsort_3.exe 129KB
L1_003_string.exe 1.83MB
L1_007.cpp 534B
shortmap0.cpp 123B
huiwen.cpp 1KB
10_to_16_binary.cpp 1KB
L1_008.exe 1.83MB
convert_itoa.cpp 312B
BinaryTree.cpp 3KB
L1_004.cpp 122B
.gitignore 649B
L1_015.cpp 370B
4_10.exe 1.83MB
BinaryTree.exe 1.91MB
L1_005.exe 1.84MB
L1_007.exe 1.83MB
test_istringstream.exe 1.91MB
L1_013.cpp 194B
add_up.cpp 168B
M_way_floyed.cpp 1KB
10_to_16.cpp 946B
L1_009_net.exe 129KB
L1_011.cpp 341B
BinaryTree_ne.cpp 4KB
10_to_16_binary.exe 1.83MB
L1_011_string.exe 1.83MB
L1_014.exe 1.83MB
huiwen2.exe 1.83MB
add_up.exe 128KB
共 91 条
- 1
资源评论
土豆片片
- 粉丝: 1555
- 资源: 5641
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功