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

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

#include 
#include 
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张图片

你可能感兴趣的:(数据结构,二叉树,数据结构)