【发布时间】:2015-10-29 19:13:05
【问题描述】:
我正在编写一个二叉树搜索程序,但我不确定如何添加节点并通过它们进行搜索。节点来自一个 .txt 文件,该文件正在使用不同的文件读取,因此假设它已经工作。
文本文件如下所示: 名称 位置 旧楼 31.2222 新楼 21.2111
就像我说的,程序已经读入了文件,所以这不是问题。但是,我必须将名称和位置插入到二叉树的节点中。然后我必须搜索范围内的所有内容,该范围是正负值的来源。
旁注:我的复制构造函数也可能不正确,尽管它符合正确。
感谢您的帮助!
#ifndef BINTREE_HPP
#define BINTREE_HPP
#include <utility>
#include <string>
#include <vector>
class bintree {
// A binary search tree for locations in Lineland.
// Notes:
// - Assume a flat, one-dimensional world with locations from -180 to 180.
// - All locations and distances are measured in the same units (degrees).
public:
// Default constructor
bintree() {
this->root = NULL;
}
// Copy constructor
bintree(const bintree &t) {
this -> root = NULL;
*this = t;
}
// Destructor
~bintree() {
}
// Copy assignment is implemented using the copy-swap idiom
friend void swap(bintree &t1, bintree &t2) {
using std::swap;
// Swap all data members here, e.g.,
// swap(t1.foo, t2.foo);
// Pointers should be swapped -- but not the things they point to.
}
bintree &operator= (bintree other) {
// You don't need to modify this function.
swap(*this, other);
return *this;
}
void insert(const std::string& name, double p) {
// insert node with name and location (p)
}
void within_radius(double p, double r, std::vector<std::string> &result) const {
// Search for elements within the range `p` plus or minus `r`.
// Clears `result` and puts the elements in `result`.
// Postcondition: `result` contains all (and only) elements of the
// tree, in any order, that lie within the range `p` plus or minus
// `r`.
}
private:
struct node
{
node *left;
node *right;
};
node* root;
};
#endif
【问题讨论】:
标签: c++ search insert binary-search-tree