C++ 将平衡BST扩展到C++;STL映射
出于实践目的,我实现了一个平衡的二叉搜索树(红黑树)。下面是到目前为止我已经实现的底层节点和方法的数据结构的标题:C++ 将平衡BST扩展到C++;STL映射,c++,c++11,map,stl,C++,C++11,Map,Stl,出于实践目的,我实现了一个平衡的二叉搜索树(红黑树)。下面是到目前为止我已经实现的底层节点和方法的数据结构的标题: #ifndef BST_H #define BST_H template <typename T> class treeNode { public: treeNode *left; treeNode *right; T key; treeNode(T key) : key(key) , left(nullp
#ifndef BST_H
#define BST_H
template <typename T>
class treeNode {
public:
treeNode *left;
treeNode *right;
T key;
treeNode(T key)
: key(key)
, left(nullptr)
, right(nullptr) {
}
};
template <typename T>
class BST {
public:
BST() {
root = nullptr;
nodes = 0;
}
BST(BST const& rhs);
BST& operator = (BST rhs) {
this->swap(rhs);
}
BST& operator = (BST&& rhs) {
this->swap(rhs);
}
~BST() {
clear(root);
}
void swap(BST& other) {
std::swap(root, other.root);
std::swap(nodes, other.nodes);
}
void clear(treeNode<T>* node) {
if(node) {
if(node->left) clear(node->left);
if(node->right) clear(node->right);
delete node;
}
}
bool isEmpty() const {
return root == nullptr;
}
void inorder(treeNode<T>*);
void traverseInorder();
void preorder(treeNode<T>*);
void traversePreorder();
void postorder(treeNode<T>*);
void traversePostorder();
void insert(T const& );
void remove(T const& );
treeNode<T>* search(const T &);
treeNode<T>* minHelper(treeNode<T>*);
treeNode<T>* min();
treeNode<T>* maxHelper(treeNode<T>*);
treeNode<T>* max();
size_t size() const;
void sort();
treeNode<T>* inOrderSuccessor(treeNode<T>*);
bool isBST(treeNode<T>*) const;
bool isBST() const;
private:
treeNode<T> *root;
size_t nodes;
};
#endif
\ifndef BST\u H
#定义BST_H
模板
三烯类{
公众:
treeNode*左;
treeNode*对;
T键;
treeNode(T键)
:键(key)
,左(空PTR)
,右(空PTR){
}
};
模板
BST级{
公众:
BST(){
root=nullptr;
节点=0;
}
BST(BST施工和rhs);
BST和操作员=(BST rhs){
此->交换(rhs);
}
BST和运算符=(BST和rhs){
此->交换(rhs);
}
~BST(){
清除(根);
}
无效掉期(BST和其他){
std::swap(root,other.root);
std::swap(节点,其他.nodes);
}
无效清除(treeNode*节点){
如果(节点){
如果(节点->左)清除(节点->左);
如果(节点->右侧)清除(节点->右侧);
删除节点;
}
}
bool isEmpty()常量{
返回根==nullptr;
}
按顺序排列的空隙(treeNode*);
void transverseInoder();
无效前序(treeNode*);
void transversePreorder();
无效邮购(treeNode*);
void transversepostorder();
无效插入(T常数&);
无效删除(T常数&);
树节点*搜索(常数T&);
treeNode*minHelper(treeNode*);
treeNode*min();
treeNode*maxHelper(treeNode*);
treeNode*max();
大小\u t大小()常量;
无效排序();
treeNode*顺序接受器(treeNode*);
布尔isBST(treeNode*)常数;
bool isBST()常量;
私人:
树根;
节点大小;
};
#恩迪夫
我打算实现C++ STL<代码> MAP<代码>(我已经使用了HASTHIT)实现了STL<代码> unordeDeMAP>/COD>,底层数据结构是红黑树。如何将我的树扩展到键值泛型类型映射
不需要任何类型的源代码。一些直觉就足够了。谢谢:)凭直觉:
T
可能是pair
。我假设您当前使用node.key<另一个\u node.key
进行比较。这是不行的,因为映射应该只使用该对的第一部分。您可以将Compare
functor作为模板参数添加到树中(与map
类的方式类似),使其对实现stl兼容的映射非常有用
您可以选择设计树,使键类和值类是分开的,而不是组合的。以下是模板定义的示例代码:
template<class Key, class Value, class Comp=std::less<Key>>
class BST {
Compare comp;
public:
BST(const Comp& comp = Comp()): comp(comp)
//...
// usage
if(comp(node.key, another_node.key)) {
// node is considered to be strictly before another_node
模板
BST级{
比较comp;
公众:
BST(常数Comp&Comp=Comp()):Comp(Comp)
//...
//用法
if(comp(node.key,另一个_node.key)){
//节点被认为严格在另一个_节点之前
您可以使用std::less
作为树的其他用户的合理默认参数,但是map实现应该转发为map提供的比较器
一个完全兼容stl的容器也应该支持自定义分配器,为了实现这一点,内部树结构也必须支持。扩展
treeNode
,为key
和value
获取两个模板参数,对于BST也是如此,另一个是使用这个类作为助手实现map
使用标准界面时,+1,因为我也想到了类似的东西!但我认为最好将模板
用于节点,将模板
用于树。是的,将键和值分离可能是一个很好的设计选择。在使用比较器时没有必要,但对于比较器,它需要打开使用键作为模板参数,并可将其分解为std::less
。我已经提到树还应支持自定义分配器。关于std::less
,现在一些代码示例将非常有用:)请编辑您的答案!@kaiduli为比较器添加了一些示例代码。