1340:【例3-5】扩展二叉树

1340:【例3-5】扩展二叉树

信息学奥赛一本通(C++版)在线评测系统

1340:【例3-5】扩展二叉树_1340:【例3-5】扩展二叉树_暖i的博客-CSDN博客

信息学奥赛一本通【1340:【例3-5】扩展二叉树】详细讲解

信息学奥赛一本通【1340:【例3-5】扩展二叉树】详细讲解_哔哩哔哩_bilibili

扩展二叉树(#号法)先序和后续可确定二叉树,中序不可

扩展二叉树(#号法)先序和后续可确定二叉树,中序不可_zhanghuaichao的博客-CSDN博客
 





C++参考代码一:

/*
1340:【例3-5】扩展二叉树-课本上代码 
http://ybt.ssoier.cn:8088/problem_show.php?pid=1340

https://blog.csdn.net/weixin_44270812/article/details/100585600
*/
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

typedef struct node;
typedef node *tree;
struct node
{
	char data;
	tree lchild, rchild;
};
tree bt;

int i;
string s;

void build(tree &bt)  //建树
{
	if(s[++i]!='.')
	{
		//bt = new node; 
		bt = new node; 
		
		bt->data = s[i];
		
		build(bt->lchild);  
		build(bt->rchild);  
	}
	else bt = NULL;
}
/*
【输入样例】tree_b.in
	ABD..EF..G..C..

【输出样例】tree_b.out
	DBFEGAC
	DFGEBCA

*/ 
//输出中序序列 
void printzx(tree bt)  
{
	if( bt )
	{
		printzx(bt->lchild);
		cout << bt->data;
		printzx(bt->rchild);
	}
}
void printhx(tree bt)  //输出后序序列 
{
	if( bt )
	{
		printhx(bt->lchild);
		printhx(bt->rchild);
		cout << bt->data;
	}
}
int main( void )
{
	//freopen("tree_b.in", "r", stdin);
	//freopen("tree_b.out", "w", stdout);
	
	cin >> s;
	
	i = -1;
	build(bt);
	
	//中序遍历 
	printzx(bt);
	cout << endl;
	
	//后序遍历 
	printhx(bt);
	cout << endl;
		
	return 0;
}

C++参考代码二:

/*
1340:【例3-5】扩展二叉树01
http://ybt.ssoier.cn:8088/problem_show.php?pid=1340
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
string s;
int i,a[1000],l[1000],r[1000];
void build(int k)
{
	i++;
	
	if ( l[k]==0 )
	  l[k]=i;
	else
	  r[k]=i;
	
	int m;
	m=i;
	if ( s[i-1]!='.' )
	{
		build(m);
		build(m);
	}
}
void printzx(int k)
{
	if (s[k-1]=='.') return;
	else
	{
		printzx( l[k] );
		cout<<s[k-1];
		printzx( r[k] );
	}
}
void printhx(int k)
{
	if (s[k-1]=='.') return;
	else
	{
		printhx( l[k] );
		printhx( r[k] );
		cout<<s[k-1];
	}
}
int main( void )
{
	cin>>s;
	
	i=1;
    build(1);
    build(1);
    
    //中序遍历 
	printzx(1);
    cout<<endl;
    
    //后序遍历 
	printhx(1);
    cout<<endl;
    
    return 0;
}

C++参考代码三:

/*
1340:【例3-5】扩展二叉树02
http://ybt.ssoier.cn:8088/problem_show.php?pid=1340
*/
#include <iostream>
using namespace std;
#include <cstdio>
	
char s[2555];
int i=-1;

struct tree
{
	char a;
	tree *lchild,*rchild;
};

tree *root,*tzx,*thx;

//ABD..EF..G..C..
tree *build()
{
	i++;
	tree *reft;
	if (s[i]!='.')
	{
		reft=new tree;
		reft->a=s[i];
		
		//建立左子树 
		reft->lchild=build();
		
		//建立右子树 
		reft->rchild=build();
	}
	else reft=NULL;
	return reft;
}

//中序 
void printzx(tree *twork)
{
	if ( twork != NULL )
	{
		printzx(twork->lchild);
		cout<<twork->a;
		printzx(twork->rchild);
	}	
}
//后序 
void printhx(tree *twork)
{
	if ( twork != NULL )
	{
		printhx(twork->lchild);
		printhx(twork->rchild);
		
		cout<<twork->a;
	}	
}

int main( void )
{
	cin>>s;
	
	//建立扩展二叉树
	root=build();
	
	//中序遍历 
	printzx(root);
	cout<<endl;
	
	//后序遍历 
	printhx(root);
	
	return 0;
}




 


 


 




【洛谷比赛】洛谷入门赛 #11

【洛谷比赛】洛谷入门赛 #11

NOI 大纲(2023年修订版)正式发布

NOI 大纲(2023年修订版)正式发布

中小学信息学相关比赛汇总(C++类)

中小学信息学相关比赛汇总(C++类)-CSDN博客

信息学竞赛有什么好的比赛网站?

信息学竞赛有什么好的比赛网站?_信息学奥赛一本通(c++版)在线评测系统_dllglvzhenfeng的博客-CSDN博客

高含金量国际计算机编程竞赛

高含金量国际计算机编程竞赛_牛津大学编程思维挑战赛_dllglvzhenfeng的博客-CSDN博客

【国际竞赛-计算机篇】2022年高含金量的计算机竞赛有哪些

【国际竞赛-计算机篇】2022年高含金量的计算机竞赛有哪些_计算机竞赛含金量排名_dllglvzhenfeng的博客-CSDN博客

 




信息学奥赛寒假、暑假、国庆十一假期 如何进行集训

信息学奥赛寒假、暑假、国庆十一假期 如何进行集训_dllglvzhenfeng的博客-CSDN博客

信息学奥赛学习、训练、测试的顺序,思路及方法

信息学奥赛学习、训练、测试的顺序,思路及方法_dllglvzhenfeng的博客-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dllglvzhenfeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值