Course Project for Data Structures:Based on Red-black tree, simply implemented std::map
#include "lib\simple_map.h" // for Windows. '\' --> '/' on Linux
#include <string>
#include <iostream>
SimpleMap<int, std::string> foo;
foo.insert(Entry<int, std::string> (1220, "alpha"))
std::cout << foo[1220] << " version pushed on 1220" << std::endl;alpha version pushed on 1220
key_typeEntry<Key, Value>.Keyvalue_typeValue- std::map::mapped_type
- not std::map::value_type
mapped_typethe same as aboveelement_typeEntry<Key, Value>- std::map::value_type
allocator_typeRedBlackTree< Node<Entry<Key, Value> >iteratorSimpleMap::MapIterator
- (constructor)
- (destructor)
- operator=
- Iterators (Forward Iterator)
beginReturn the iterator of the first Entry with min key of inorder traversalendReturn the iterator of the last Entry of inorder traversal, i.e.,(SimpleMap::iterator)nullptr
- Capacity
emptyReturn true if empty, otherwise falsesizeReturn the current number of Entrymax_sizeReturn the theoretic max amount of Entry, i.e.,UINT_MAX
- Element Access
operator[](k)return the reference of Entry.value where key == k, otherwise, insert new Entry with key == k and return the reference of its defaultly constructed valuefind(k)Return the iterator of Entry of which the key is k, otherwise SimpleMap::end, i.e.,(SimpleMap::const_iterator)nullptrcount(k)Return the count of Entry of which the key is k, i.e., 0 / 1minReturn the iterator pointing to Entry with the minimum keymaxReturn the iterator pointing to Entry with the maximum keydataReturn the pointer to RBTree
- Modifiers
insert(element)Return iterator pointing to newly inserted Entry, otherwise pointing to the old element identified by keyerase(k)Delete Entry by key,returning true when successful, otherwise falseerase(position)Delete Entry by iterator,returning true when successful,otherwise falseclear()Delete all elements and re-allocate the storage position of Red-black tree in the heap
template class SimpleMap<Key, Value>fake std::maptemplate class Entry<Key, Value>the element of SimpleMap,fake std::pair
template class RedBlackTree<node_data_type>Red Black Tree, in which node.data.key increases in inorder traversaltemplate class RedBlackTree : public SearchTree<node_data_type> : public Tree<node_data_type>put the implementation of Search Tree and Binary Tree into the parent classes of RBTreetemplate class Node<node_data_type>the node of the tree, of which node_data_type == Entry
lib\red_black_tree\rbtree.hfollowing Google C++ Style Guidelib\simple_map.hlike std::map
There were plenty of comments in simple_map.h, including:
- the definition of interfaces
- implementation details
- TODOs
- Test all behaviors of SimpleMap
- Further test the stability of Red-black tree performing random insertions and deletions
- Showcase the final gains
- Write the experiment report
- STL-style improvement
- alpha (Dec. 20, 2018)
- beta (Dec. 21, 2018)
- Due to the poor initial design, cancelled the usage of
const_iteratorthat incurred the conflicts of variousconst - Completed the dynamic memory management under multiple layers of nested data structures
- Added Member types into README
- Initially tested the interfaces and fixed some bugs
- Due to the poor initial design, cancelled the usage of
- released! (Jan. 5, 2019)
- v1.0 (Jan. 5, 2019)
- Systematically tested RedBlackTree
- Tested all interfaces of SimpleMap
- Fixed SimpleMap::count()
- Fixed SimpleMap::iterator::operator++()