映射容器-树

映射容器-树


一、目的

-掌握时间复杂度
-掌握树的创建
-掌握中序遍历

二、实验内容与设计思想

函数相关伪代码

1.菜单
2.树的定义与创建
3.创建树的函数,用key来判断位置的放置,返回root
4.创建寻找的函数,通过key,来确定寻找的值
5.创建删除的函数,通过key,来确定,返回root
6.中序遍历树,实现key顺序排序

函数代码

#include<iostream>
#include<vector>
#include <queue>
#include <map>
using namespace std;
struct BiNode {
	string k;
	string s;
	struct BiNode* rchild=NULL;
	struct BiNode* lchild=NULL;
};
BiNode* Create(string k,string s)
{
	BiNode* node = new BiNode;
	node->k = k;
	node->s = s;
	node->lchild = NULL;
	node->rchild = NULL;
	return node;
}
BiNode* Buildtree(string key, string value, BiNode* root);
BiNode* Deletetree(string key, string value, BiNode* root);
BiNode* Findtree(string key, BiNode* root);
BiNode* Changetree(string key, string newvalue, BiNode* root);
BiNode* Showtree(BiNode* root,int& i);
BiNode* menue(BiNode* root);

BiNode* menue(BiNode* root)
{
	int s, m,i=0;
	string key, value, newvalue;
	do {
		i = 0;
		cout << "欢迎使用映射容器管理系统\n";
		cout << "请选择接下来要做什么:\n1.添加key\n2.删除key\n3.通过key查找value\n4.修改联系人\n5.显示所有\n6.退出程序\n请输入对应的数字:" << endl;
		cin >> s;
		switch (s) {
		case 1:
			cout << "请输入key:";
			cin >> key;
			cout << "请输入value:";
			cin >> value;
			root=Buildtree(key, value,root);
			break;
		case 2:
			cout << "请输入要删除的key:";
			cin >> key;
			root = Deletetree(key, value, root);
			break;
		case 3:
			cout << "请输入要查找的key:";
			cin >> key;
			Findtree(key,root);
			break;
		case 4:
			cout << "请输入要修改的key:";
			cin >> key;
			cout << "请输入新的value:";
			cin >> newvalue;
			Changetree( key,newvalue,root);
			break;
		case 5:
			Showtree(root,i);
			if (i == 0) {
				cout << "没有key" << endl;
			}
			break;
		case 6:
			cout << "退出程序";
			break;
		default:
			cout << "输入错误,请重新输入";
			break;
		}
	} while (s != 6);
	return NULL;
}
BiNode* Deletetree(string key, string value, BiNode* root) {
	if (root == NULL) {
		cout << "没有找到该key和value" << endl;
	}
	else if (key < root->k) {
		root->lchild = Deletetree(key, value, root->lchild);
	}
	else if (key > root->k) {
		root->rchild = Deletetree(key, value, root->rchild);
	}
	else {
		if (root->lchild == NULL) {
			BiNode* temp = root->rchild;
			delete root;
			cout << "删除成功" << endl;
			return temp;
		}
		else if (root->rchild == NULL) {
			BiNode* temp = root->lchild;
			delete root;
			cout << "删除成功" << endl;
			return temp;
		}
       else {
			BiNode* temp = root->rchild;
			while (temp && temp->lchild != NULL) {
				temp = temp->lchild;
			}
			root->k = temp->k;
			root->s = temp->s;
			root->rchild = Deletetree(temp->k, value, root->rchild);
		}

	}
	return root;
}
BiNode* Buildtree(string key,string value,BiNode*root) {
	int m;
	if (root == NULL) {
		BiNode* node = Create(key, value);
		cout << "添加成功" << endl;
		return node;
	}
	if (key<root->k) {
		root->lchild = Buildtree(key, value,root->lchild);
	}
	else if(key > root->k){
		root->rchild = Buildtree(key, value, root->rchild);
	}
	if (key == root->k) {
		cout << "该key已存在" << endl;
	}
	return root;
}
BiNode* Findtree(string key,BiNode* root) 
{
	if (root == NULL) {
		cout << "没有找到该联系人" << endl;
		return NULL;
	}else if (key == root->k) {
		cout << "key:" << root->k << "  value:" << root->s << endl;
		return root;
	}
	else if (key < root->k) {
		return Findtree(key, root->lchild);
	}
	else {
		return Findtree(key, root->rchild);
	}
}
BiNode* Changetree(string key,string newvalue, BiNode* root)
{
	if (root == NULL) {
		cout << "没有找到该key" << endl;
		return NULL;
	}
	else if (key == root->k) {
		root->s = newvalue;
		cout << "修改成功" << endl;
		return root;
	}
	else if (key < root->k) {
		return Changetree(key,newvalue, root->lchild);
	}
	else {
		return Changetree(key, newvalue, root->rchild);
	}
}

BiNode* Showtree(BiNode* root,int &i) 
{
		if (root != NULL) {
			Showtree(root->lchild,i);
			cout << root->k << " "<< root->s<<endl;
			Showtree(root->rchild,i);
			i = 1;
		}

		return NULL;
}
int main()
{
	BiNode* root = NULL;
	menue(root);
	return 0;
}

三、实验使用环境

以下请根据实际情况编写

  • 操作系统:Windows 11专业版
  • 编程语言:C++
  • 开发工具:[Visual studio 2022]

四、实验步骤和调试过程

映射容器

本机运行截图



五、实验小结

遇到的问题及解决方法:

  1. 问题:树的创建
  • 解决方法:调试后修改代码,实现功能

实验体会和收获:

时间复杂度为O(logn)

posted @ 2025-04-22 14:56  穗和  阅读(21)  评论(0)    收藏  举报