我有一个课程Tree
,我希望将其扩展为更专业的数据结构,例如Order_tree
和Interval_tree
。这些扩充需要添加Node
,例如大小信息,以及对某些算法的微小更改。
我想知道在性能,可读性和可维护性方面实现C ++扩充的最佳方法。树不应该以多态方式使用。到目前为止我所尝试的是公开继承Tree
,然后重载基本方法。 (我为面向对象编程的初学者而道歉)
template <typename T>
class Tree {
protected:
enum class Color : char {BLACK = 0, RED = 1};
struct Node {
T key;
Node *parent, *left, *right;
Color color;
Node() : color{Color::BLACK} {} // sentinel construction
Node(T val, Color col = Color::RED) : key{val}, parent{nil}, left{nil}, right{nil}, color{col} {}
};
using NP = typename Tree::Node*;
NP root {nil};
// nil sentinel
static NP nil;
// core utility algorithms...
};
template <typename T>
typename Tree<T>::NP Tree<T>::nil {new Node{}};
订单树
template <typename T>
class Order_tree : public Tree<T> {
using Color = typename Tree<T>::Color;
using Tree<T>::Tree; // inherit constructors
struct Order_node {
T key;
Order_node *parent, *left, *right;
size_t size; // # of descendent nodes including itself = left->size + right->size + 1
Color color;
Order_node() : size{0}, color{Color::BLACK} {} // sentinel construction
Order_node(T val, Color col = Color::RED) : key{val}, parent{nil}, left{nil}, right{nil}, size{1}, color{col} {}
};
using NP = typename Order_tree::Order_node*;
NP root {nil};
static NP nil;
// overloading on only the methods that need changing
};
template <typename T>
typename Order_tree<T>::NP Order_tree<T>::nil {new Order_node{}};
然而,这并没有正常行为,因为现在我有2个根和2个nils,所有基本方法都在基础上工作,而Tree<T>::NP
而不是Order_tree::NP
所以{ {1}}的尺寸属性无法使用。
一种方法是复制粘贴代码,这是非常难以维护的。我认为另一种方法是在T上模拟树以及NP,因此Order_node
是节点上的别名Order_tree
和特化树。
答案 0 :(得分:0)
如果你真的对所有树&#34;的常规树感兴趣,似乎问题不在树中而在Node中。你需要一些特殊的节点情况,为什么不把它们概括一下呢?例如:
template <typename T>
class Tree {
protected:
struct BaseNode {
//all code you really can generalize here
};
struct Node : public BaseNode {
//You need Node here only if you want your base Tree class to be ready to use.
//If you want to use only its derives such as Order_tree,
//you create special nodes kinds only there
};
// core utility algorithms...
BaseNode * root; //Only one root node, there is no need in duplication!
//You can instantiate it as root = new OrderTreeNode or root = new SpecialTreeNode in any derives.
};
然而,Node虚函数调用的价格相当大。所以你需要清楚地理解 - 你需要概括而不是重复代码,还是需要性能。
答案 1 :(得分:0)
经过一些实验,我找到了达到我想要的最好方法:
现在的样子:
的 tree.h中强>
namespace sal {
// utilities with no dependence on root, outside of class now
template <typename Node>
Node* tree_find(Node* start, typename Node::key_type key) {
while (start != Node::nil && start->key != key) {
if (key < start->key) start = start->left;
else start = start->right;
}
return start;
}
// more of them...
template <typename Node>
class Tree {
protected:
using NP = Node*;
using T = typename Node::key_type;
// nil is static member of each Node type now
NP root {Node::nil};
// virtual methods that could be changed by augmentation
virtual void rotate_left(NP node);
virtual void rotate_right(NP node);
virtual void tree_insert(NP start, NP node);
virtual void rb_delete(NP node);
// non-virtual methods that are never overridden
void rb_insert_fixup(NP node);
void rb_delete_fixup(NP successor);
void rb_insert(NP node); // just a call to tree_insert and rb_insert_fixup
void transplant(NP old, NP moved);
public:
virtual ~Tree(); // does all the clean up so its derived classes don't have to
// interface...
};
template <typename T>
struct Basic_node {
static Basic_node* nil;
using key_type = T;
T key;
Basic_node *parent, *left, *right;
Color color;
Basic_node() : color{Color::BLACK} {} // sentinel construction
Basic_node(T val) : key{val}, parent{nil}, left{nil}, right{nil}, color{Color::RED} {}
};
template <typename T>
using Basic_tree = Tree<Basic_node<T>>;
template <typename T>
Basic_node<T>* Basic_node<T>::nil {new Basic_node{}};
}
<强> order_tree.h 强>
#include "tree.h"
namespace sal {
template <typename Node>
class Order_augment : public Tree<Node> {
using NP = Node*;
using T = typename Node::key_type;
using Tree<Node>::root;
// no need to redefine shared core functions
using Tree<Node>::rb_insert;
using Tree<Node>::transplant;
using Tree<Node>::rb_insert_fixup;
using Tree<Node>::rb_delete_fixup;
// order statistics operations
NP os_select(NP start, size_t rank) const;
size_t os_rank(NP node) const;
// modification of rb operations to maintain augmentation
virtual void tree_insert(NP start, NP node) override;
virtual void rb_delete(NP node) override;
virtual void rotate_left(NP node) override;
virtual void rotate_right(NP node) override;
public:
// augmented interface
};
template <typename T>
struct Order_node {
static Order_node* nil;
using key_type = T;
T key;
Order_node *parent, *left, *right;
size_t size; // # of descendent nodes including itself = left->size + right->size + 1
Color color;
Order_node() : size{0}, color{Color::BLACK} {} // sentinel construction
Order_node(T val) : key{val}, parent{nil}, left{nil}, right{nil}, size{1}, color{Color::RED} {}
};
template <typename T>
Order_node<T>* Order_node<T>::nil {new Order_node{}};
template <typename T>
using Order_tree = Order_augment<Order_node<T>>;
}
结果是,保存扩充数据结构的文件大小现在大约为1/3,并且完全删除了代码重复!这意味着改进核心方法的任何改变都可以只定位到tree.h,并且它的效果也可以在所有增强树中感受到。