PTA:是否完全搜索二叉树

核心:完全二叉树第i个节点的左节点,对应的下标一定是2*i,右节点一定是2*i+1。

 


用num表示这个节点应该有的下标(虽然不用数组储存,以1开始),如果以构建完全二叉树的方式去构建树。若建树过程中,num大于节点数n,则不是完全二叉搜索树,否则是;


  1. 搜索树的构建:
int n;
int cnt = 0;
int num = 1;
array<int, 100>das;
typedef struct node* POS;
struct node {
	int d;
	int id;
	POS l,r;
};
bool _yes = 1;
void creat(POS& tree, int x, int num)
{

//注意tree传入递归以后,只是增加了节点,tree任然是头节点
	if ( num > n )

//大于表明不是完全二叉树
		_yes = 0;
	if ( !tree )
	{
		tree = new node;
		tree->d = x;
		tree->l = tree->r = nullptr;
		return;
	}
	if ( x > tree->d )
		creat(tree->l, x, num * 2);//左节点
	else creat(tree->r, x, num * 2 + 1);//右节点
}
  1. 层次遍历
void ord(POS&a)
{
	if ( !a ) return;
	queue<POS> en;
	en.push(a);
	bool once = 1;
	while ( !en.empty() )
	{
		POS te = en.front();
		en.pop();
		if ( once )
		{
			cout << te->d;
			once = 0;
		}
		else {
			cout << " " << te->d;
		}
		if ( te->l )
		{
			en.push(te->l);
		}
		if ( te->r )
		{
			en.push(te->l);
		}
	}
}
  1. 完整源码(指针变量以&传参,与指针value传参的区别)

int n;
int cnt = 0;
int num = 1;
array<int, 100>das;
typedef struct node* POS;
struct node {
	int d;
	int id;
	POS l,r;
};
bool _yes = 1;

//传参引用
void creat(POS& tree, int x, int num)
{
	if ( num > n )
		_yes = 0;
	if ( !tree )
	{
//由于是引用相当于tree对应的实参 被重新赋值了new node的地址
//如果不是ref 语义,则会导致tree指向的地址改变,不在指向原有的实参的地址
		tree = new node;
		tree->d = x;
		tree->l = tree->r = nullptr;
		return;
	}
	if ( x > tree->d )
		creat(tree->l, x, num * 2);
	else creat(tree->r, x, num * 2 + 1);
}

//非ref语义creat函数
POS creat(POS tree, int x, int num)
{
	if ( num > n )
		_yes = 0;
	if ( !tree )
	{
		POS te = new node;
		te->d = x;
		te->l = te->r = nullptr;
		return te;
	}
	if ( x > tree->d )
		tree->l=creat(tree->l, x, num * 2);
	else tree->r=creat(tree->r, x, num * 2 + 1);
	return tree;
}
/*main code haved changed;
	ifor(i, 1, n)
	{
		int x;
		cin >> x;
		h=creat(h, x, 1);
	}*/



void ord(POS&a)
{
	if ( !a ) return;
	queue<POS> en;
	en.push(a);
	bool once = 1;
	while ( !en.empty() )
	{
		POS te = en.front();
		en.pop();
		if ( once )
		{
			cout << te->d;
			once = 0;
		}
		else {
			cout << " " << te->d;
		}
		if ( te->l )
		{
			en.push(te->l);
		}
		if ( te->r )
		{
			en.push(te->l);
		}
	}
}
int main(int args, char** argv)
{
	/*ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin.tie(nullptr);*/
	cin >> n;
	POS h = nullptr;
	ifor(i, 2, n)
	{
		int x;
		cin >> x;
		creat(h, x, 1);
	}
	ord(h);
	coute;
	if ( _yes ) cout << "YES";
	else cout << "NO";
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值