【C++】数据结构-二叉树之二叉排序树的查找

一、 实验题目

1、应用随机函数生成100个随机数,数据控制在1-1000之间,并保存到一个数组中。
2、在上一题的基础上,编写代码实现二分查找,例如对用户输入的某个数据,能够进行二分查找,显示找到或没找到,如果找到,请给出查找时的比较次数。
3、在第1题的基础上,编写代码实现二叉排序树的创建,并输出中序遍历该二叉排序树的结果。
4、在第3题的基础上,编写代码实现二叉排序树上的查找,例如对用户输入的某个数据,能够进行查找,显示找到或没找到,如果找到,请给出查找时的比较次数。

头文件的定义:

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

1、应用随机函数生成100个随机数,数据控制在1-1000之间,并保存到一个数组中。

代码实现:
1)编写生成100个随机数的函数void RAND(int a[])
//生成100个随机数存入数组a中 
void RAND(int a[])
{
    //随机数种子
	srand(time(NULL));
	for(int i = 0; i < 100; i++)
	{
        //生成1-1000之间的随机数
		a[i] = rand() % 1000 + 1;
	}
}
2)编写输出数组前一百个元素的函数void OUTPUT(int a[])
//打印输出数组a中的一百个随机数 
void OUTPUT(int a[])
{
	for(int i=0;i<100;i++)
	{
		printf("%5d\t",a[i]);
        //控制一行十个数字
		if((i + 1 ) % 10 == 0)printf("\n"); 
	}
}
3)主函数 int main()
int main()
{
	int a[10000];
	RAND(a);	//生成随机数
	OUTPUT(a);	//打印输出
    return 0;
}
样例输出:

在这里插入图片描述

2、在上一题的基础上,编写代码实现二分查找,例如对用户输入的某个数据,能够进行二分查找,显示找到或没找到,如果找到,请给出查找时的比较次数。

样例代码:
1)编写对数组前一百个数进行冒泡排序的函数void Bubble(int a[])
//对随机数进行冒泡排序
void Bubble(int a[]){
	for(int i = 0; i < 99; i++){
		for(int j = 0; j < 99 - i; j++){
			if(a[j] > a[j + 1]){
				int temp  = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			} 
		}
	}
}
2)二分查找,在数组的前一百个元素中查找元素x,返回查询次数和位置void BinSearch(int a[],int x)
//二分查找 
void BinSearch(int a[],int x)
{
	int low = 0,high = 99,z = 0;
	while(low <= high)
	{
		z++;
		int mid=(low + high) / 2;
		if(x == a[mid])
		{
			printf("成功找到,数字%d在第%d个位置!",x,mid + 1);
			printf("\n比较次数:%d次\n", z);
			return ;
		}
		else if(x < a[mid])
		{
			high = mid - 1;
		}
		else
		{
			low = mid + 1;
		}
	}
	printf("没有找到数字%d",x);
	return ;
}
3)主函数 int main()
int main()
{
	int a[10000];
	RAND(a);	//随机生成100个数 
	Bubble(a);	//对前100个数排序 
	OUTPUT(a);	//打印输出前100个数 
	int x;
	printf("输出要查找的数:"); 
	scanf("%d",&x);
	BinSearch(a,x); 	//在数组a的前一百个数中查找元素x 
    return 0;
}
样例输出:

在这里插入图片描述

3、在第1题的基础上,编写代码实现二叉排序树的创建,并输出中序遍历该二叉排序树的结果。

样例代码:
1)定义二叉树的结构体
typedef struct node{
	int key;
	struct node *lchild,*rchild;
}BSTNode,*BSTree;
2)编写插入元素到二叉树的函数void InsertBST(BSTree *bst,int key)
void InsertBST(BSTree *bst,int key)
{
	BSTree s;
	if(*bst==NULL)
	{
		s=(BSTree)malloc(sizeof(BSTNode));
		s->key=key;
		s->lchild=s->rchild=NULL;
		*bst=s;
	}
	else if(key<(*bst)->key)
	{
		InsertBST(&((*bst)->lchild),key);
	}
	else if(key>(*bst)->key)
	{
		InsertBST(&((*bst)->rchild),key);
	}	
}
3)编写创建二叉排序树的函数void Create(BSTree *bst,int a[])
void CreateBST(BSTree *bst,int a[])
{
	//静态的int变量m 
	int static m = 0;
	int key = a[m++];
	*bst = NULL;
	while(key!=0)
	{
		InsertBST(bst,key);
		key=a[m++];
	}
}
4)编写用于打印输出二叉排序数的函数void InOrder(BSTree bst)
//打印输出排序好的二叉排序数 (中序)
void InOrder(BSTree bst)
{
	if(bst!=NULL)
	{
		
		InOrder(bst->lchild);
		printf("%5d\t",bst->key);
		InOrder(bst->rchild);
	}
}
5)主函数 int main()
int main()
{
	int a[10000];
	RAND(a);	//随机生成100个数 
	BSTree bst;
	CreateBST(&bst,a);		//使用数组a中的数创建二叉排序树 
	InOrder(bst);			//打印输出排序好的数 
	return 0;
}
样例输出:

在这里插入图片描述

4、在第3题的基础上,编写代码实现二叉排序树上的查找,例如对用户输入的某个数据,能够进行查找,显示找到或没找到,如果找到,请给出查找时的比较次数。

样例代码:
1)编写二叉排序树查找函数用于查找元素x
void SearchBST(BSTree bst, int key)
{ 
    BSTree q;
    q = bst;
    int static i = 0;
    while(q)
    {
    	if(q->key == key){
    		printf("找到了,找了%d次",i);
    		return ;
		} else {
			i++;	
		}
    	q->key > key ? q = q->lchild : q = q->rchild;
    }
    printf("没有找到,找了%d次",i);
    return ;
}
2)主函数 int main()
int main()
{
	int a[10000];
	RAND(a);	//随机生成100个数 
	BSTree bst;
	CreateBST(&bst,a);		//使用数组a中的数创建二叉排序树 
	InOrder(bst);			//打印输出排序好的数 
	int x; printf("\n\n请输入要查找的数:");
	scanf("%d",&x); 
	SearchBST(bst,x); 	//查找 
	return 0;
}
样例输出:
样例1:

在这里插入图片描述

样例2:

在这里插入图片描述

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

孑然R

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

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

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

打赏作者

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

抵扣说明:

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

余额充值