解题思路:

注意事项:

参考代码:

#include <bits/stdc++.h>

using namespace std;


struct node{

int num;

node *lchild;

node *rchild;

};

node *T = NULL;


void addnum(int num)

{

node *temp = NULL,*now = NULL;

if(T == NULL)

{

temp = (node *)malloc(sizeof(node));

temp->num = num;

temp->lchild = NULL;

temp->rchild = NULL;

T = temp;

}

else

{

temp = (node *)malloc(sizeof(node));

temp->num = num;

temp->lchild = NULL;

temp->rchild = NULL;

now = T;

while(1)

{

if(num > now->num && now->rchild !=NULL)

now = now->rchild;

else if(num < now->num && now->lchild !=NULL)

now = now->lchild;

else if(num > now->num && now->rchild ==NULL)

{

now->rchild = temp;

break;

}

else

{

now->lchild = temp;

break;

}

}

}

}


int length(int num,string &temp)

{

char c[10];

int length = 0;

while(num!=0)

{

c[length++] = num % 10 + '0';

num = num / 10;

}

for(int i = length-1;i>=0;i--)

{

temp = temp + c[i];

}

return length;

}


void print()

{

string c[101];

int num[10000] = {0};

int count = 1;

stack<node*> s;

node *now = NULL,*pre = NULL;

now = T;

s.push(T);

while(!s.empty())

{

now = s.top();

if(now->rchild != NULL && num[now->rchild->num] == 0)

{

now = now->rchild;

s.push(now);

}

else

{

num[now->num] = count++;

s.pop();

if(now->lchild != NULL)

{

now = now->lchild;

s.push(now);

}

}

}

queue<node*> q;

queue<int> add;

int need[101] ={0};

q.push(T);

int line;

int le = 0;

int left,right;

string temp = "";

int flag = 0;

while(!q.empty())

{

temp.clear();

now = q.front();

q.pop();

line = num[now->num];

if(now->lchild!= NULL)

{

left = num[now->lchild->num];

q.push(now->lchild);

}

else

left = line;

if(now->rchild != NULL)

{

right = num[now->rchild->num];

q.push(now->rchild);

}

else

right = line;

le = length(now->num,temp);

int i;

if(flag == 1)

c[line] = c[line]+"-"+temp;

else

{

c[line] = temp;

flag = 1;

}

need[line] = 1;

if(right != left)

c[line] = c[line] + "-|";

for(int i = 1;i<count;i++)

{

if(need[i] == 0)

{

if(i>=right && i<=left && i != line)

{

for(int j = c[i].length();j<c[line].length()-1;j++)

{

c[i] = c[i] + ".";

}

c[i] = c[i] + "|"; 

}

}

}

}

for(int i = 1;i<count;i++)

{

cout<<c[i]<<endl;

}

}


int main()

{

int num;

while(scanf("%d",&num)!=EOF)

{

addnum(num);

}

if(T !=NULL)

print();

return 0;

}


点赞(0)
 

0.0分

3 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 1 条评论

忍耐寒暑的肖某 2年前 回复TA
倒数第三排printf少打了个f呢,宝~