二叉搜索树(指针)求助
  • 板块学术版
  • 楼主zhc9426
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/8/17 15:18
  • 上次更新2023/10/27 14:58:06
查看原帖
二叉搜索树(指针)求助
592849
zhc9426楼主2022/8/17 15:18

数据用的是P1801的样例,函数 in_ 用来插入,函数 cout_ 为中序遍历寻找第i小的数并输出 贴代码

#include<iostream>
#include<cstdio>
using namespace std;

typedef struct mmm *position_;
typedef position_ tree;

int i,j,m,n,a;
struct mmm{
	long data_;
	tree left_;
	tree right_;
};
tree tree_all;
////
tree in_( tree tree_in, int x)
{
	cout<<"in"<<" ";
	if(!tree_in)
	{
		tree_in = new mmm;
		tree_in->data_ = x;
		tree_in->left_ = NULL;
		tree_in->right_ = NULL;
	}
	else
	{
		if( x> tree_in->data_)
		{
			in_ ( tree_in->right_, x);
		}
		if( x< tree_in->data_)
		{
			in_ ( tree_in->left_, x);
		}
	}
	return 0;
}
////
tree cout_(tree tree_in ,int x)
{
	cout<<"cout"<<" ";
	if(tree_in->left_)
	{
		cout_ ( tree_in->left_, x);
	}
	a+=1;
	if(a>=x)
	{
		if(a==x) cout<<tree_in->data_<<endl;
		return 0;
	}
	if(tree_in->right_ )
	{
		cout_ ( tree_in->right_, x);
	}
	return 0;
}
////
int main(){
	cin>>m>>n;
	int x[m+1],y[n+1];
	////
	for(i=0;i<m;i++)
	{
		cin>>x[i];
	}
	for(i=0;i<n;i++)
	{
		y[i]=0;
	}
	for(i=0;i<n;i++)
	{
		cin>>j;
		y[j]+=1;
	}
	////
	j=1;
	for(i=0;i<m;i++)
	{
		in_ ( tree_all, x[i]);
		if(y[i])
		{
			while(y[i]!=0)
			{
				cout_( tree_all, ++j);
				y[i]-=1;
				a=0;
			}
		}
	}
	return 0;
}

贴结果

in in cout
2022/8/17 15:18
加载中...