实验五(仅供参考,不能用于阴阳怪气)

如果更好的想法也可以留言分享,不喜勿喷,我只是个菜鸟

任务1:

数据:

        10
        1 acb1 123 0 012345
        1 bcd1 234 1 123456
        1 cde1 345 0 234567
        1 def1 456 1 345678
        1 efg1 567 0 456789
        2 acb2 678 1 567890
        2 bcd2 789 0 678901
        2 cde2 891 1 789012
        2 def2 901 0 890123
        2 efg2 012 1 901234

#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 50
typedef struct Student{
	int id, clas, gender;
	string name, phone;
}Student;
int n;
bool cmp(Student st1, Student st2) {
	return st1.id < st2.id;
}
void Creat(Student *st,int n) {
	for (int i = 0; i < n; i++) {
		cin >> st[i].clas >> st[i].name >> st[i].id >> st[i].gender >> st[i].phone;
	}
	sort(st, st + n, cmp);
}
void Destory(Student* st) {
	delete[] st;
}
int Search_sp(Student *st,int key) {
	for (int i = 0; i < n; i++) {
		if (st[i].id == key) {
			return i;
		}
	}
	return -1;
}
int Search_b(Student* st,int low,int high ,int key) {
	int mid;
	while (low <= high) {
		mid = (low + high) / 2;
		if (st[mid].id == key)
			return mid;
		else if (st[mid].id > key) {
			high = mid - 1;
		}
		else if (st[mid].id < key) {
			low = mid + 1;
		}
	}
	return -1;
}
void Cout(Student a) {
	cout << a.name;
	printf(" %03d\n", a.id);
}
void Traverse(Student* st, void Cout(Student a)) {
	for (int i = 0; i < n; i++){
		cout << i << ": ";
		Cout(st[i]);
	}
}

int main() {
	Student *st = new Student[MAX];
	cin >> n;
	Creat(st,n);
	Traverse(st,Cout);
	cout << "顺序查找123的位置" << endl;
	cout<<Search_sp(st, 123);
	cout <<endl<< "折半查找123的位置" << endl;
	cout << Search_b(st, 0, n, 123);

	cout << "顺序查找891的位置" << endl;
	cout << Search_sp(st, 891);
	cout << endl << "折半查找891的位置" << endl;
	cout << Search_b(st, 0, n, 891);

	cout << "顺序查找999的位置" << endl;
	cout << Search_sp(st, 999);
	cout << endl << "折半查找999的位置" << endl;
	cout << Search_b(st, 0, n, 999);
	return 0;
}

任务2:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef struct Node {
	string data;
	Node* left, * right;
}Node;
void dfs(string a, Node** tree) {
	if (*tree != NULL && (*tree)->data > a)
		dfs(a, &(*tree)->left);
	if (*tree != NULL && (*tree)->data < a)
		dfs(a, &(*tree)->right);
	if (!*tree)
		*tree = new Node{ a,NULL,NULL };
}
void inOrder1(Node* tree) {
	if (tree->left) inOrder1(tree->left);
	cout << tree->data << " ";
	if (tree->right) inOrder1(tree->right);
}
void Search(Node* tree, string a, int* sign) {
	if (tree->data == a) *sign = 1;
	if (tree->left) Search(tree->left, a, sign);
	if (tree->right) Search(tree->right, a, sign);

}
void inOrder2(int a,string *arr,string *tree2,int *id) {
	if (a * 2 + 1 < 12) {
		inOrder2(a * 2 + 1,arr,tree2,id);
	}
	tree2[a] = arr[(*id)++];
	if( (a * 2 + 2) < 12){
		inOrder2(a * 2 + 2,arr,tree2,id);
	}
}
void inOrder3(int a,string *tree2) {
	if (a * 2 + 1 < 12) {
		inOrder3(a * 2 + 1,tree2);
	}
	cout << tree2[a] << " ";
	if ((a * 2 + 2) < 12) {
		inOrder3(a * 2 + 2,tree2);
	}
}
int main() {
	cout << 2.1 << endl;//==========================================================================
	string arr[12]={ "Dec","Feb","Nov","Oct","June","Sept","Aug","Apr","May","July","Jan","Mar" };
	Node* tree1 = new Node{ "Dec",NULL,NULL };
	for (int i = 1; i < 12; i++) {
		dfs(arr[i], &tree1);
	}
	inOrder1(tree1);
	cout << endl << endl << 2.2 << endl;//===========================================================
	int sign = 0;
	Search(tree1, "Sept",&sign);
	if (sign == 1)cout << "Sept存在于树中";
	else cout << "Sept不存在于树中";
	cout <<endl<<endl<< 3 << endl;//==================================================================
	sort(arr, arr + 12);
	string tree2[12];
	int temp = 0;
	inOrder2(0, arr,tree2, &temp);
	cout << "构建后的树的中序遍历" << endl;
	inOrder3(0, tree2);
	cout << endl << "构建后的二叉树的层次遍历" << endl;
	for (int i = 0; i < 12; i++) {
		cout << tree2[i] << " ";
	}
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值