Question

Let's suppose we have a RedBlack-Tree implementation which consists of 2 classes:

  • Tree - holds the pointer to the Node *root of the tree and defines all operations over the tree (Insert, Delete, etc)
  • Node - a data storage, which holds pointers to Node *parent, Node *left, Node *right nodes and std::string key.

The Tree::Insert() has the following implementation:

void Tree::Insert(const std::string &key)
{
    Node *z = new Node(key);
    // adding node logic
}

Now the task: every node has to store the time of its creation.

Limitations: the base tree implementation should be modified as less as possible and should contain details of specific extensions (so it should know nothing about the creation time property).

My thoughts: extending NodeWithTime : Node and adding unsigned int creation_time property.

Where I'm in stuck: how would we instantiate the node now?

Any proposals?

PS: it's neither a homework or a job task - I'm just learning c++ and data structures.

Was it helpful?

Solution

It's relatively simple. First, the Node struct:

template<typename T> struct Node {
    Node(T t) : value(std::move(t)), time(RightNow()) {}
    T value;
    TimeType time;
    std::unique_ptr<Node> left;
    std::unique_ptr<Node> right;
};

A quick helper make_unique:

template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) {
    return std::unique_ptr<T>(new T(std::forward<Args>(args...)));
}
template<typename T> void Tree<T>::Insert(T key) {
    auto z = make_unique<Node<T>>(std::move(key));
    // insert
}

First, I fixed your crappy new and delete and replaced it with smart pointers. Then I also made your tree a template because who needs a tree that can only do one type? Then I swapped out your const T& with a T so that it might live with move-only types.

Then I just added a Time field and called RightNow() in the constructor. The exact TimeType and RightNow() you use depends on your needs and what exactly you mean by "time of it's creation". Are we talking about "6th July, 2013"? Or a very-high-resolution clock? In any case, these "creation time" details do not impact the tree.

Edit: Wait, you want to have one tree type where only some of the nodes know the creation time? Or just to alter the tree so that all the nodes know the creation time? I did #2, but for #1, you could indeed simply inherit from Node. To wit,

template<typename T> struct Node {
    Node(T t) : value(std::move(t)) {}
    T value;
    std::unique_ptr<Node> left;
    std::unique_ptr<Node> right;
};
template<typename T> struct NodeWithTime : Node<T> {
    TimeType time;
    NodeWithTime(T t) : Node(std::move(t)), time(RightNow()) {}
};
template<typename T> void Tree<T>::insert(T t) {
    std::unique_ptr<Node> nodeptr;
    if (IWantToStoreCreationTime)
        nodeptr = make_unique<NodeWithTime<T>>(std::move(t));
    else
        nodeptr = make_unique<Node>(std::move(t));
    // insert
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top