二叉排序树

#include<iostream>
using namespace std;

struct BiTree
{
	int data;
	BiTree *Lchild;
	BiTree *Rchild;
};

void BiSort(BiTree *&root)  //数据的初始化
{
	int Insert(BiTree *&root,BiTree *s);
	int num;
	cout<<"请输入节点数为:";
	cin>>num;
	int *value=new int[num];
	int i;
	cout<<endl<<"请输入各节点的值:";
	for(i=0;i<num;i++)
		cin>>value[i];
	BiTree *t;
	for(i=0;i<num;i++)
	{
		t=new BiTree;
		t->data=value[i];
		t->Lchild=NULL;
		t->Rchild=NULL;
		Insert(root,t);
	}
}

int Insert(BiTree *&root,BiTree *s)  //插入数据
{
	if(root==NULL)root=s;
	else
		if(s->data==root->data)return 0;
		else
			if(s->data>root->data)Insert(root->Rchild,s);
			else
				if(s->data<root->data)Insert(root->Lchild,s);
	return 1;
}

BiTree *Search(BiTree *&root,int n)
{
	BiTree *temp=root;
	if(root->data>n)temp=Search(root->Lchild,n);
	else
		if(root->data<n)temp=Search(root->Rchild,n);
			return temp;
}

int main()
{
	BiTree *root;
	BiTree root1;
	root=NULL;
	BiSort(root);
	BiTree *temp;
	cout<<"请输入你要查询的数字:";
	int n;
	cin>>n;
	temp=Search(root,n);
	cout<<"你查询的数字是:"<<temp->data<<"并已经找到!"<<endl;
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值