生活随笔
收集整理的這篇文章主要介紹了
二叉搜索树(城市数据库)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實驗內容:
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
);
}
總結
以上是生活随笔為你收集整理的二叉搜索树(城市数据库)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。