In this exam you are required to implement a template binary search tree (BST). A BST, is a hierarchical (ordered) data structure where each node can have at most two children, namely, left and right child. Each node stores a pair of a key and the associated value. The binary tree is ordered according to the keys. Given a node N
, all the nodes having keys smaller than the key of the node N
can be found going left. While all the nodes with a key greater than the key of the node N
can be reached going right.
From the implementation point of view, you node has two pointers left
and right
pointing to the left and right child respectively. The pointers points to nullptr
if they have no children.
It is useful to add an additional pointer (head, root, whatever you like) pointing to the first node, called root node.
The tree must be traversed in order, i.e., if I "print" the tree in the picture, I expect to see on the screen the sequence
1 3 4 6 7 8 10 13 14
node 1
is the first node, and node 14
is the last one.
You have to solve the following tasks in C++11 (C++14 and 17 are welcomed as well).
-
implement a template binary search tree
-
it must be templated on the type of the key and the type of the value associated with it.
-
optional you can add a third template on the operation used to compare two different keys.
-
implement proper iterators for your tree (i.e.,
iterator
andconst_iterator
) -
the tree must have at least the following public member function
insert
, used to insert a new pair key-value.print
, used to printkey: value
of each node. Note that the output should be in order with respect to the keys.clear()
, clear the content of the tree.begin()
, return aniterator
to the first node (which likely will not be the root node)end()
, return a properiterator
cbegin()
, return aconst_iterator
to the first nodecend()
, return a properconst_iterator
balance()
, balance the tree.find
, find a given key and return an iterator to that node. If the key is not found returnsend()
;- optional
erase
, delete the node with the given key. - optional implement the
value_type& operator[](const key_type& k)
function int theconst
andnon-const
versions). This functions, should return a reference to the value associated to the keyk
. If the key is not present, a new node with keyk
is allocated having the valuevalue_type{}
.
-
implement copy and move semantics for the tree.
-
-
test the performance of the lookup (using the function
find
) before and after the tree is re-balanced. Use proper numbers (and types) of nodes and look-ups. Does lookup behaves asO(log N)
? -
optional document the code with
Doxygen
-
write a short report
-
test everything
- you can use
std::pair<key_type,value_type>
found in the headerutility
- use recursive functions
- Big hint start coding and using the iterators ASAP.