日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

二叉搜索树(城市数据库)

發布時間:2023/12/16 数据库 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉搜索树(城市数据库) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實驗內容:
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); }

總結

以上是生活随笔為你收集整理的二叉搜索树(城市数据库)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。