数据结构——二叉排序树

数据结构——二叉排序树

输入一组无序关键字(整数)序列构造一棵二叉排序树并对其进行中序遍历输出;在二叉排序树中查找某一关键字,若存在,显示查找成功;若不存在,将其插入到二叉排序树中,再中序遍历输出。
二叉排序树就是将一组数据利用树结构依据数据大小插入树的一种排序法方法,构建二叉排序树方便于查找。
文件BiSortTree.h
#pragma once
#ifndef _BISORTTREE_H_
#define _BISORTTREE_H_

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
#define EQ(a,b) (!cmp((a),(b)))
#define LT(a,b) (cmp((a),(b)) < 0)
#define GT(a,b) (cmp((a),(b)) > 0)
typedef int TElemType;
typedef struct BiTNode
{
	TElemType data;
	struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
/*
比较函数cmp
*/
double cmp(double a, double b)
{
	return a - b;
}
/*
搜索二叉树,返回bool
*/
bool SearchBST(BiTree T, TElemType key, BiTree f, BiTree & p)
{
	if (!T)//空则返回false, p为双亲节点,方便插入
	{
		p = f;
		return false;
	}
	else if (EQ(key, T->data))//找到结点,返回true,p为当前结点
	{
		p = T;
		return true;
	}
	else if (LT(key, T->data))//小于时,查找左树
	{
		SearchBST(T->lchild, key, T, p);
	}
	else//大于,查找又树
	{
		SearchBST(T->rchild, key, T, p);
	}
}
/*
	结点的插入
	函数名:InsertBST 
	参数:BiTree & T, ELemType temp;
*/
bool InsertBST(BiTree & T, TElemType temp)
{
	BiTree p;
	if (SearchBST(T, temp, NULL, p))//先查找是否树中已存在temp
		return false;//存在则返回false
	else
	{//不存在则开始进行插入操作
		BiTree s = (BiTree)malloc(sizeof(BiTNode));//建立新节点,将temp放入新节点s中
		s->data = temp;
		s->lchild = s->rchild = NULL;
		if (!p)//若是空树,直接将新节点s作为树
		{
			T = s;
		}
		else if (LT(temp, p->data))//根据大小选择插入左子树还是右子树
		{//大于右子树
			p->lchild = s;
		}
		else
		{//小于左子树
			p->rchild = s;
		}
		return true;
	}
}
/*
随机生成二叉排序树
函数名:RandCreatBiSortTree
参数:BiTree & T, int time(生成的结点数) 
*/
BiTree RandCreatBiSortTree(BiTree & T, int time)
{
	int temp;
	printf("生成数为:");
	for (int i = 0; i < time; i++)
	{
		temp = rand() % 50;//设置0到50的随机数
		printf("%d ", temp);
		InsertBST(T, temp);
	}
	printf("\n");
	return T;
}
/*

*/
BiTree CreatBiSortTree(BiTree & T, int time)
{
	int temp;
	printf("生成数为:");
	for (int i = 0; i < time; i++)
	{
		//temp = rand() % 50;//设置0到50的随机数
		scanf("%d", &temp);
		InsertBST(T, temp);
	}
	printf("\n");
	return T;
}
/*
树的中序遍历输出
函数名:InOrderTraversal
参数:BiTree BT
*/
void InOrderTraversal(BiTree BT)
{
	int top = 0;
	BiTree stack[MAX_SIZE], T = BT;
	while (T||top)
	{
		if (T)
		{
			stack[top++] = T;
			T = T->lchild;
		}
		else
		{
			T = stack[--top];
			printf("%d ", T->data);
			T = T->rchild;
		}
	}
	printf("\n");
}
/*
中序遍历递归
*/
void InOrder(BiTree T)           //中序递归输出一棵二叉树
{
	if (T)            // T != NULL 	
	{
		InOrder(T->lchild);//递归中序周游其左子树
		printf("%c ", T->data);
		InOrder(T->rchild);           //递归中序周游其右子树
	}
}
#endif // !_BISORTT1REE_H_

// BiSortTree.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"BiSortTree.h"
#include<stdlib.h>
int main()
{
	BiTree T = NULL;
	int time;
	//RandCreatBiSortTree(T, 10);
	printf("输入数据数:\n");
	scanf("%d", &time);
	CreatBiSortTree(T, time);
	printf("二叉排序树的中序遍历结果如下(非递归):\n");
	InOrderTraversal(T);
	printf("二叉排序树的中序遍历结果如下(递归):\n");
	InOrder(T);
	BiTree p;
	while (true)
	{
		printf("\n请输入查找值:\n");
		scanf("%d", &time);
		if (!InsertBST(T, time))
		{
			printf("%d在树中\n", time);
		}
		else
		{
			printf("无此数,插入成功!\n");
		}
		InOrderTraversal(T);
		InOrder(T);
	}
	system("pause");
	return 0;
}

程序调试如下所示:
此图片为程序调试时截图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值