关于二叉搜索树实现(悬赏1关)
查看原帖
关于二叉搜索树实现(悬赏1关)
703085
Myosotis_alpestris楼主2023/4/26 15:18

RT,以下是我写的二叉搜索树实现的代码,为什么实现不了啊,有没有大佬帮忙看看

#include<bits/stdc++.h>
using namespace std;
int data[10]={1,3,5,7,8,9,2,4,6,10};
struct node{
	int val;
	node *right,*left;
	node()
	{
		val=0;right=nullptr;left=nullptr;
	}
	node(int x,node* lf,node* rf)
	{
		val=x;left=lf;right=rf;
	}
};//树
node* newnode(int x)//插入
{
	node *root=new node();
	//cout<<"shsdahdsa"<<endl;
	root->val=x;
	root->left=nullptr;
	root->right=nullptr;
	return root;
}
void build(node* root,int x)//搜索
{
	if(root==nullptr) root=newnode(x);
	if(root->val>x) build(root->left,x);
	else build(root->right,x);
}
node* create()//建树
{
	node* root=nullptr;
	for(int i=0;i<10;i++)
	{
		build(root,data[i]);
	}
	return root;
}
void show(node* root)//先序遍历
{
	if(root==nullptr) return;
	cout<<root->val;
	show(root->left);
	show(root->right);
}
int main()
{
	node* root=create();
	show(root);
	return 0;
}

2023/4/26 15:18
加载中...