二叉排序树实现储存航班信息
二叉排序树又称二叉搜索树、二叉查找树
二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有节点的值均小于它的根节点的值;
(2)若右子树不空,则右子树上所有节点的值均大于它的根节点的值;
(3)左、右子树也分别为二叉排序树;
(4)没有键值相等的节点。
/在写程序的时候我没发现书上有代码,于是我就自己写了一个排序算法…
想按较为标准来的请参照《大话数据结构》313页或《算法导论》161页/
宏定义及结构体定义
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
typedef struct Data {
int departure_time;
char flight_number[8] = { '\0' };
char airline[12] = { '\0' };
int operator>(struct Data other)
{
if (departure_time > other.departure_time) {
return 1;
}
else {
return 0;
}
}
int operator<(struct Data other)
{
if (departure_time < other.departure_time) {
return 1;
}
else {
return 0;
}
}
};
typedef struct BiNode {
Data data;
struct BiNode* left;
struct BiNode* right;
}BiNode, *BiTree;
typedef struct DataList {
Data list[20];
int count = 0;
}DataList;
输入输出函数定义//后面会用
int input_time()
{
int t1, t2, time;
printf("请输入航班起飞时刻 : ");
scanf_s("%d:%d", &t1, &t2);
time = 60 * t1 + t2;
return time;
}
Data input_data()
{
Data temp_data;
temp_data.departure_time = input_time();
getchar();
printf("请输入航空公司名称: ");
gets_s(temp_data.airline);
printf("请输入航班号: ");
gets_s(temp_data.flight_number);
return temp_data;
}
int output_data(Data data)
{
int t1, t2, time;
time = data.departure_time;
t2 = data.departure_time % 60;
t1 = data.departure_time / 60;
printf("%s - %s - %d:%d\n", data.airline, data.flight_number, t1, t2);
return time;
}
创建根节点函数、结点比较函数和结点插入函数
BiTree CreateRoot()
{
BiTree root;
root = (BiTree)malloc(sizeof(BiNode));
root->data = input_data();
root->left = NULL;
root->right = NULL;
return root;
}
void NodeCompare(BiTree T, BiTree node)
{
int flag = 0;
if ((node->data < T->data) && T != NULL) { //比当前结点小
if (T->left == NULL) { //如果左结点为空,插到左结点
T->left = node;
flag = 1;
}
else {
if (node->data > T->left->data) { //如果左结点非空且比左结点大
if (T->left->right == NULL) { //如果左结点的右结点为空,插到左的右结点
T->left->right = node;
}
else { //如果左结点的右结点非空,与左结点的右结点进行比较
NodeCompare(T->left->right, node);
}
}
else { //如果左结点非空且比左结点小,与左结点进行比较
if (T->left->left == NULL) {
T->left->left = node;
}
else {
NodeCompare(T->left->left, node);
}
}
}
}
if ((node->data > T->data) && T != NULL) { //比当前结点大
if (T->right == NULL) { //右结点为空,插到右结点
T->right = node;
flag = 1;
}
else { //右结点非空
if (node->data < T->right->data) { //比右结点小
if (T->right->left == NULL) { //右结点的左为空,插到右结点的左
T->right->left = node;
}
else { //右结点的左非空,与其进行比较
NodeCompare(T->right->left, node);
}
}
else { //比右结点的右结点大,与其进行比较
if (T->right->right == NULL) {
T->right->right = node;
}
else {
NodeCompare(T->right->right, node);
}
}
}
}
if (flag == 1) {
return;
}
}
void TreeInsert(BiTree T)
{
BiTree node = (BiTree)malloc(sizeof(BiNode));
node->data = input_data();
node->left = NULL;
node->right = NULL;
NodeCompare(T, node);
}
有了上面这些函数,就可以创建二叉排序树了
排序输出函数
void SortTree_put(BiTree T)
{
if (T == NULL) {
return;
}
SortTree_put(T->left);
output_data(T->data);
SortTree_put(T->right);
}
用上面这个函数就可以按顺序输出了
要实现输入时间查找最近的航班需要以下两个函数
储存信息函数和按时间查找函数
void SortTree_store(BiTree T,DataList &message)
{
if (T == NULL) {
return;
}
SortTree_store(T->left, message);
message.list[message.count++] = T->data;
output_data(T->data);
SortTree_store(T->right, message);
}
void TimeSearch(DataList message)
{
int i, position, position_2 = 0, time;
printf("根据起飞时间查询\n ");
time = input_time();
if (time < message.list[0].departure_time) {
position = 0;
}
if (time > message.list[message.count - 1].departure_time) {
position = message.count - 1;
}
for (i = 0; i < message.count; i++) {
if ((time >= message.list[i].departure_time) && (time <= message.list[i + 1].departure_time)) {
if ((time - message.list[i].departure_time) > (message.list[i + 1].departure_time - time)) {
position = i + 1;
}
else if ((time - message.list[i].departure_time) == (message.list[i + 1].departure_time - time)) {
position = i;
position_2 = i + 1;
}
else {
position = i;
}
break;
}
}
printf("距离您所查找的时间最近的航班是:\n");
if (position_2) {
output_data(message.list[position]);
output_data(message.list[position_2]);
}
else {
output_data(message.list[position]);
}
}
然后是配套的main函数及switch交互界面
int main(void)
{
int i, sl, num;
Data data;
DataList message;
BiTree T;
T = CreateRoot();
do {
start:printf("插入结点...1\n按时间顺序输出...2\n输出数据...3\n按时间查询数据...4\n按航班号查询...5\n离开...0\n");
scanf_s("%d", &sl);
switch (sl) {
case 0:exit(0);
case 1:system("cls"); printf("请问要插入几次? "); scanf_s("%d", &num);
for (i = 0; i < num; i++) {
TreeInsert(T);
}system("pause"); system("cls"); break;
case 2:system("cls"); message.count = 0; SortTree_store(T, message); system("pause"); system("cls"); break;
case 3:system("cls"); printf("当前航班数为: %d", message.count); system("pause"); system("cls"); break;
case 4:system("cls"); TimeSearch(message); system("pause"); system("cls"); break;
default:goto start;
}
} while (1);
return 0;
}
是我自己原创(瞎写)的算法,所以麻烦给我点个赞,谢谢喽Thanks♪(・ω・)ノ