Map ADT based on C++ map implemented with binary search tree. More...
Classes | |
class | map_iterator |
Forward declare node class. More... | |
struct | node |
Internal structure for binary search tree. More... | |
Public Member Functions | |
Constructors | |
map () | |
Constructor. More... | |
map (const map &m) | |
Copy constructor. More... | |
~map () | |
Destructor. More... | |
map & | operator= (const map &m) |
Copy assignment. More... | |
Iterators | |
iterator | begin () |
iterator | end () |
reverse_iterator | rbegin () |
reverse_iterator | rend () |
const_iterator | cbegin () const |
const_iterator | cend () const |
const_reverse_iterator | crbegin () const |
const_reverse_iterator | crend () const |
Capacity | |
size_t | size () const |
bool | empty () const |
Element Access | |
Value & | operator[] (const Key &k) |
Value & | at (const Key &k) |
const Value & | at (const Key &k) const |
Modifiers | |
std::pair< iterator, bool > | insert (const value_type &v) |
Insert element into map, i.e., put(k, v) from the Map ADT. More... | |
iterator | erase (const_iterator position) |
Remove element at specified position. More... | |
size_t | erase (const Key &k) |
Remove element at specified position. More... | |
void | clear () noexcept |
Removes all elements. More... | |
Operations | |
iterator | find (const Key &k) |
Search the container for an element with key k . More... | |
const_iterator | find (const Key &k) const |
Search the container for an element with key k . More... | |
size_t | count (const Key &k) const |
Count elements with specific keys. More... | |
Public Types | |
Types | |
typedef Key | key_type |
Public access to Key type. More... | |
typedef Value | mapped_type |
Public access to Value type. More... | |
typedef std::pair< const key_type, mapped_type > | value_type |
Entry type. More... | |
typedef map_iterator< value_type > | iterator |
Bidirectional iterator. More... | |
typedef map_iterator< const value_type > | const_iterator |
Const bidirectional iterator. More... | |
typedef std::reverse_iterator < iterator > | reverse_iterator |
Reverse bidirectional iterator. More... | |
typedef std::reverse_iterator < const_iterator > | const_reverse_iterator |
Const reverse bidirectional iterator. More... | |
Private Member Functions | |
Helpers | |
node * | finder (const Key &k) const |
Utility for finding a node with Key k . More... | |
std::pair< node *, bool > | inserter (const value_type &v) |
Utility for inserting a new node into the data structure. More... | |
node * | eraser (node *n) |
Erase a node from the tree. More... | |
Private Attributes | |
Data | |
node * | root |
size_t | sz |
Number of nodes. More... | |
Map ADT based on C++ map implemented with binary search tree.
Key | Key type |
Value | Value type |
Assumes the following: There is always enough memory for allocations (not a good assumption, just good enough for our purposes); Functions not well-defined on an empty container will exhibit undefined behavior.
typedef Key mystl::map< Key, Value >::key_type |
Public access to Key type.
typedef Value mystl::map< Key, Value >::mapped_type |
Public access to Value type.
typedef std::pair<const key_type, mapped_type> mystl::map< Key, Value >::value_type |
Entry type.
typedef map_iterator<value_type> mystl::map< Key, Value >::iterator |
Bidirectional iterator.
typedef map_iterator<const value_type> mystl::map< Key, Value >::const_iterator |
Const bidirectional iterator.
typedef std::reverse_iterator<iterator> mystl::map< Key, Value >::reverse_iterator |
Reverse bidirectional iterator.
typedef std::reverse_iterator<const_iterator> mystl::map< Key, Value >::const_reverse_iterator |
Const reverse bidirectional iterator.
mystl::map< Key, Value >::map | ( | ) |
Constructor.
mystl::map< Key, Value >::map | ( | const map< Key, Value > & | m | ) |
Copy constructor.
m | Other map |
mystl::map< Key, Value >::~map | ( | ) |
Destructor.
map& mystl::map< Key, Value >::operator= | ( | const map< Key, Value > & | m | ) |
Copy assignment.
m | Other map |
iterator mystl::map< Key, Value >::begin | ( | ) |
iterator mystl::map< Key, Value >::end | ( | ) |
reverse_iterator mystl::map< Key, Value >::rbegin | ( | ) |
reverse_iterator mystl::map< Key, Value >::rend | ( | ) |
const_iterator mystl::map< Key, Value >::cbegin | ( | ) | const |
const_iterator mystl::map< Key, Value >::cend | ( | ) | const |
const_reverse_iterator mystl::map< Key, Value >::crbegin | ( | ) | const |
const_reverse_iterator mystl::map< Key, Value >::crend | ( | ) | const |
size_t mystl::map< Key, Value >::size | ( | ) | const |
bool mystl::map< Key, Value >::empty | ( | ) | const |
Value& mystl::map< Key, Value >::operator[] | ( | const Key & | k | ) |
k | Input key |
If k
matches an element in the container, the function will return a reference to its mapped value.
If k
is not found in the container, the function should insert a new element with that key and return a reference to its mapped value (constructed through default construction)
Value& mystl::map< Key, Value >::at | ( | const Key & | k | ) |
k | Input key |
If k
matches an element in the container, the function will return a reference to its mapped value.
If k
is not found in the container, the function throws an out_of_range
exception.
const Value& mystl::map< Key, Value >::at | ( | const Key & | k | ) | const |
k | Input key |
If k
matches an element in the container, the function will return a reference to its mapped value.
If k
is not found in the container, the function throws an out_of_range
exception.
std::pair<iterator, bool> mystl::map< Key, Value >::insert | ( | const value_type & | v | ) |
Insert element into map, i.e., put(k, v) from the Map ADT.
v | Key, Value pair |
Insert is the "put(k, v)" function of the Map ADT. Remember that Maps store unique elements, so if the element existed already it is returned.
iterator mystl::map< Key, Value >::erase | ( | const_iterator | position | ) |
Remove element at specified position.
position | Position |
size_t mystl::map< Key, Value >::erase | ( | const Key & | k | ) |
Remove element at specified position.
k | Key |
|
noexcept |
Removes all elements.
iterator mystl::map< Key, Value >::find | ( | const Key & | k | ) |
const_iterator mystl::map< Key, Value >::find | ( | const Key & | k | ) | const |
size_t mystl::map< Key, Value >::count | ( | const Key & | k | ) | const |
Count elements with specific keys.
k | Key |
k
Because all elements in a map container are unique, the function will only return 1 or 0.
|
private |
Utility for finding a node with Key k
.
k | Key |
Base your algorithm off of Code Fragment 10.9 on page 436
|
private |
Utility for inserting a new node into the data structure.
v | Key, Value pair |
Inserter is the "put(k, v)" function of the Map ADT. Remember that Maps store unique elements, so if the element existed already it is returned.
Base you algorithm off of Code Fragment 10.10 on page 436
Hint: Will need to use functions node::replace and node::expand
|
private |
Erase a node from the tree.
n | Node to erase |
n
in treeBase your algorithm off of Code Fragment 10.11 on page 437
Hint: will need to use functions node::inorder_next, node::replace, and node::remove_above_external
|
private |
Root of binary tree, the root will be a sentinel node for end iterator. root.left is the "true" root for the data
|
private |
Number of nodes.