实验内容:
1.利用BST实现一个城市数据库:每个数据库结点包括城市名称和以整数x与y表示的城市坐标,根据城市名称组织该BST;
2.在该数据库上实现按城市名称进行的插入、删除和检索;
3.打印出以指定字母打头的所有城市记录;
4.打印出与指定点的距离在给定值之内的所有城市记录;

#include <iostream>
#include <string>
using namespace std;
class BSTNode {//二叉树节点
private:string key;//城市名字int* value;//城市坐标BSTNode* ln;//左右孩子BSTNode* rn;
public:BSTNode() {//无参构造函数ln = rn = NULL;value[0] = 1000;value[1] = -999;}//有参构造函数BSTNode(string key, int* a, BSTNode* ln = NULL, BSTNode* rn = NULL) :key(key), value(a), ln(ln), rn(rn) {}~BSTNode() {}inline int* getValue() {//获取城市坐标return this->value;}inline void setValue(int* value) {//设置城市坐标this->value = value;}inline string getKey() {//获取城市名字return key;}inline void setKey(string key) {//设置城市名字this->key = key;}inline BSTNode* left() {//返回左孩子指针return ln;}inline void setLeft(BSTNode* ln) {//设置左孩子指针this->ln = ln;}inline BSTNode* right() {//返回右孩子指针return rn;}inline void setRight(BSTNode* rn) {//设置右孩子指针this->rn = rn;}
}
;
class BST {//检索二叉树
private:BSTNode* inserthelp(BSTNode*, string&, int*);//插入节点BSTNode* removehelp(BSTNode*, string&);//删除节点BSTNode* getmin(BSTNode*);//获取最小节点BSTNode* deletemin(BSTNode*);//删除最小节点BSTNode* findhelp(BSTNode*, string&);//检索节点void findhelpH(BSTNode*, char&);//输出二叉树void findhelpD(BSTNode*, int, int, int);//输出按特定字母开头的城市信息void printhelp(BSTNode*);//输出距离某点范围之内的城市信息void clearhelp(BSTNode*);//清空二叉树
public:BSTNode* root;//二叉树根节点BST() {//无参构造root = NULL;}~BST() {//有参构造clearhelp(root);}void insert(string& key, int* value) {//插入root = inserthelp(root, key, value);}void findH(char key) {//检索首字母城市findhelpH(root, key);}void findD(int x, int y, int d) {//检索定点范围内城市findhelpD(root, x, y, d);}void remove(string key) {//删除BSTNode* temp = findhelp(root, key);if (root->getKey() != "") {removehelp(root, key);}}void print() {//输出二叉树if (root != NULL)printhelp(root);}
}
;
void BST::printhelp(BSTNode* root) {if (root == NULL) return;printhelp(root->left());cout << root->getKey() << endl;printhelp(root->right());
}
BSTNode* BST::getmin(BSTNode* root) {if (root->left() == NULL) return root; else return getmin(root->left());
}
BSTNode* BST::deletemin(BSTNode* root) {if (root->left() == NULL) return root->right(); else root->setLeft(deletemin(root->left()));return root;
}
BSTNode* BST::removehelp(BSTNode* root, string& key) {if (root == NULL) return NULL; else if (key < root->getKey())root->setLeft(removehelp(root->left(), key)); else if (key > root->getKey())root->setRight(removehelp(root->right(), key)); else {BSTNode* temp = root;if (root->left() == NULL) {root = root->right();delete temp;}else if (root->right() == NULL) {root = root->left();delete temp;}else {BSTNode* t = getmin(root->right());root->setValue(t->getValue());root->setKey(t->getKey());root->setRight(deletemin(root->right()));delete t;}}return root;
}
BSTNode* BST::findhelp(BSTNode* root, string& key) {if (root == NULL) {return NULL;}if (key < root->getKey())return findhelp(root->left(), key); else if (key > root->getKey())return findhelp(root->right(), key); else {return root;}
}
void BST::findhelpH(BSTNode* root, char& key) {if (root == NULL) {return;}if (key < root->getKey().at(0))findhelpH(root->left(), key); else if (key > root->getKey().at(0))findhelpH(root->right(), key); else {findhelpH(root->left(), key);cout << root->getKey() << " " << root->getValue()[0] << " " << root->getValue()[1] << endl;findhelpH(root->right(), key);}
}
void BST::findhelpD(BSTNode* root, int x, int y, int d) {if (root == NULL) return;findhelpD(root->left(), x, y, d);int* city = root->getValue();double distance = (city[0] - x) * (city[0] - x) + (city[1] - y) * (city[1] - y);if (distance <= d*d) cout << root->getKey() << " " << root->getValue()[0] << " " << root->getValue()[1] << endl;findhelpD(root->right(), x, y, d);
}
BSTNode* BST::inserthelp(BSTNode* root, string& key, int* value) {if (root == NULL) return new BSTNode(key, value);if (key < root->getKey()) root->setLeft(inserthelp(root->left(), key, value)); else root->setRight(inserthelp(root->right(), key, value));return root;
}
void BST::clearhelp(BSTNode* root) {if (root == NULL) return;clearhelp(root->left());clearhelp(root->right());delete root;
}
int main() {BST r;int m;cin >> m;//输入多个城市for (int i = 0; i < m; i++) {int* a = new int[2];int q, w;string s;cin >> s >> q >> w;a[0] = q;a[1] = w;r.insert(s, a);}//不定次的插入删除while (1) {int x;cin >> x;if (x == 2)break;if (x == 0) {string s;cin >> s;r.remove(s);}if (x == 1) {string s;int q, w;int* a = new int[2];cin >> s >> q >> w;a[0] = q;a[1] = w;r.insert(s, a);}}char c;int f, g;cin >> c;cin >> f >> g >> m;//输出各种检索r.print();r.findH(c);r.findD(f, g, m);
}

二叉搜索树(城市数据库)相关推荐

  1. B-树(B-Tree)与二叉搜索树(BST):讲讲数据库和文件系统背后的原理(读写比较大块数据的存储系统数据结构与算法原理)...

    人类总喜欢发明创造一些新名词(比如说,简写/缩写/简称什么的),并通过这些名词把人群分成了三六九等.弄到最后,把自己都绕晕了. 你看,首先就是,B树,不要与Binary tree或B+tree混淆. ...

  2. b+树时间复杂度_深入理解数据库系统之存储存引擎(二叉搜索树)

    B树是数据库存储引擎使用的最多的存储结构之一.许多开源数据库系统也都大量使B用树作为存储结构,多年来已经证明它们能够胜任大多数使用场景. 早在1971年鲁道夫·拜尔(Rudolph Bayer)和爱德 ...

  3. 数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)

    原文:http://blog.csdn.net/sup_heaven/article/details/39313731 数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B ...

  4. 二叉搜索树的2层结点统计_植树节,程序猿种的那些树

    公历 3 月 12 日是一年一度的植树节.旨在宣传保护森林,并动员群众参加植树造林活动.说到树,程序猿们肯定不陌生,趁着这个植树节到来之时普及一下程序猿们经常遇见的树. 1. 二叉搜索树 定义 二叉搜 ...

  5. 二叉搜索树,和红黑树,

    二叉搜索树 class node():def __init__(self,a):self.val=aself.left=Noneself.right=None #当函数写起来复杂时候,考虑给函数加变量 ...

  6. 《数据结构与算法之二叉搜索树(Java实现)》

    说在前头:本人为大二在读学生,书写文章的目的是为了对自己掌握的知识和技术进行一定的记录,同时乐于与大家一起分享,因本人资历尚浅,能力有限,文章难免存在一些错漏之处,还请阅读此文章的大牛们见谅与斧正.若 ...

  7. 08_Python算法+数据结构笔记-二叉搜索树查询/删除-AVL树旋转/插入/应用-贪心算法

    b站视频:路飞IT学城 清华计算机博士带你学习Python算法+数据结构_哔哩哔哩_bilibili #71 二叉搜索树:查询 import randomclass BiTreeNode:def __ ...

  8. java面试(二)--(1)EJB的几种类型(2)Hibernate的悲观锁和乐观锁(3) 二叉搜索树与双向链表

    1.请说明一下EJB的几种类型分别是什么? EJB(Enterprise JavaBean)是J2EE服务器端的组件模型,EJB包括会话Bean(Session Bean).实体Bean(Entity ...

  9. (面经总结)一篇文章带你整理面试过程中关于 二叉树、二叉搜索树、平衡二叉树、B 树和 B+树的相关知识

    文章目录 一.二叉树(Binary Tree) 二.二叉搜索树(Binary Search Tree) 三.平衡二叉树(AVL Tree) 四.B树(B Tree) 五.B+树(B+ tree) 五. ...

  10. 动态展示二叉搜索树之实践进行时

    目录 一.选题展示 二.思考题目 三.阶段一:图形显示二叉搜索树之插入 1.图形显示二叉搜索树的要求 2.原博主的代码和输出 四.阶段二:图形显示二叉搜索树之查删 1.二叉搜索树的查找操作 2.二叉搜 ...

最新文章

  1. NLP 解决方案是如何被深度学习改写的?
  2. python面试-Python面试技巧合集(建议收藏)
  3. check上传模板中的金额字段中的千分位
  4. cannot resolve symbol ‘R‘ 程序包R不存在
  5. 【深度学习】21个深度学习调参技巧,一定要看到最后一个
  6. Jw-alipay 1.0.0版本发布,开源支付窗管理平台
  7. C#.NET 消息机制
  8. 传感器系列之4.6雨滴传感器
  9. DirectoryEntry 对象
  10. JAVA里面一加到一百等于多少_从1加到100等于多少 【求和算法汇总】
  11. 用jquery制作2048小游戏(超详细)
  12. 文儿结婚生子再被提起!利、文两家互喷,老利回怼:豆豆妈好!文儿暴怒:阴损至极!小洲专场欠礼之人!利、曼等人名利其中!
  13. [WARNING] fpm_children_bury()
  14. 风速风向 UV 相互转换
  15. MySQL使用Navicat自动生成ER图
  16. CS5265参数说明|CS5265设计资料|CS5265设计电流|type-CtoHDMI2.0拓展坞资料
  17. 无盘服务器汇聚,Microsoft PowerPoint - 网吧无盘系统网卡汇聚与交换机汇聚教程(包会).pdf...
  18. 【翻译】压缩Windows XP中的ServicePackFiles/i386文件夹
  19. IDEA: 1分钟学会一键部署并运行项目,Alibaba Cloud Toolkit插件,太火!
  20. Python 开胃菜

热门文章

  1. MySQL——MHA原理介绍及VIP配置
  2. 为什么很多炫酷的产品并没能流行起来?
  3. 参与流片是一种怎样的体验
  4. mysql中selectform_数据库中select...from....where....各表示什么意思
  5. 初尝scrapy捉取数据保存到mysql
  6. python使用虚拟内存_深入浅出虚拟内存(一)
  7. 性能测试脚本用例模版
  8. python不用api爬twitter
  9. 计算机健康小知识,电脑一族护肤保健小常识
  10. 01-Epicor开发总结