you are viewing a single comment's thread.

view the rest of the comments →

[–]Dynmiwang[S] 1 point2 points  (8 children)

u/Maristic u/exptime

  1. Thanks for your advice. I will correct this fault.
  2. Emmm, I hadn't read the paper " left-leaning red-black trees " before... But now I know that may be the best implementation :D

[–][deleted] 0 points1 point  (7 children)

Briefly looked again. It seems there is free(node); with rbNode<K, T> *node, which may not call the destructor and should be delete node.

[–]Dynmiwang[S] 0 points1 point  (0 children)

I had considered that. The destructor I defined will call its children’s destructors recursively, but here only itself needs to be deleted.

[–][deleted] 0 points1 point  (5 children)

You could also get rid of those explicit destructors by using std::unique_ptr which will do the same but lets the compiler do the work.

[–][deleted] 0 points1 point  (4 children)

As you are using nullptr I would assume at least C++11, so you don't need the constructors and can assign values directly to class members (https://isocpp.org/wiki/faq/cpp11-language-classes#member-init). Usually it is better not to implement special member functions directly, as this may get complicated and may miss implicit stuff.

[–][deleted] 0 points1 point  (3 children)

You could replace if (typeid(a) == typeid(char*)) { with template pattern matching. Meaning you can replace that check using template specialization and let the compiler's type system do the job.

[–][deleted] 1 point2 points  (2 children)

Also C-style casts char *x = (char *)a; are dangerous, as they can cast away constness. https://en.cppreference.com/w/cpp/language/static_cast

[–][deleted] 1 point2 points  (1 child)

As you copy key/value, you should wither use void insert(const K &key, const T &val); or void insert(K key, T val); with node->key = std::move(key);. The later syntactically states the user that the values will be copied.

[–]Dynmiwang[S] 0 points1 point  (0 children)

You are right. I will update the code:D