Open In App

Which data structure is used by Map?

Last Updated : 29 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

What is a Map?

Before learning the data structure used by a map, let us have an overlook of map.

Map is the part of the STL library that stores key value pairs in it and no two values have the same keys but the different keys can store similar values. The map stores keys in sorted order.

These are some functions which map uses with their Time Complexities:

  • insert() – Time Complexity O(Log N)
  • delete() – Time Complexity O(Log N)
  • erase() – Time Complexity O(Log N)
  • find() – Time Complexity O(Log N)

Which Data Structure is used by Map?

Now coming to the point that which data structure is used by map. The internal implementation of map is self-balancing Binary Tree. There are generally two methods for implementing a self-balancing binary tree:

These are the two methods but 

For map’s internal implementation it uses Red-Black Tree. 

To balance the tree after an insertion/deletion both algorithms use the notion of rotations where the nodes of the tree are rotated to perform the re-balancing. While in both algorithms the insert/delete operations are O(log N), in the case of Red-Black tree re-balancing rotation is an O(1) operation while with AVL this is an O(log N) operation, making the Red-Black Tree more efficient in this aspect of the re-balancing stage and one of the possible reasons that is more commonly used.

What is Red-Black Tree?

A red-black tree is a kind of self-balancing binary search tree where each node has an extra bit, and that bit is often interpreted as the color (red or black) which is used to ensure that the tree remains balanced during insertions and deletions.

To learn more about the Red-Black tree please refer to the article on “Red-Black Tree

Red-Black Tree Diagram

Red-Black Tree Diagram


Similar Reads

Static Data Structure vs Dynamic Data Structure
Data structure is a way of storing and organizing data efficiently such that the required operations on them can be performed be efficient with respect to time as well as memory. Simply, Data Structure are used to reduce complexity (mostly the time complexity) of the code. Data structures can be two types : 1. Static Data Structure 2. Dynamic Data
4 min read
Remove duplicates from unsorted array using Map data structure
Given an unsorted array of integers, print the array after removing the duplicate elements from it. We need to print distinct array elements according to their first occurrence. Examples: Input : arr[] = { 1, 2, 5, 1, 7, 2, 4, 2} Output : 1 2 5 7 4 Explanation : {1, 2} appear more than one time. Approach : Take a hash map, which will store all the
4 min read
Map Policy Based Data Structure in g++
There are some data structures that are supported by g++ compiler and are not a part of the C++ standard library. One of these is: Policy-Based Data Structure, which is used for high-performance, flexibility, semantic safety, and conformance to the corresponding containers in std. This can also be used as a map, to store key and a value (pair) in a
3 min read
Sort a 2D vector diagonally using Map Data Structure
Given a 2D vector mat[][] of integers. The task is to sort the elements of the vectors diagonally from top-left to bottom-right in increasing order.Examples: Input: mat[][] = {{9, 4, 2}, {7, 4, 6}, {2, 3, 3}} Output: 3 4 2 3 4 6 2 7 9 Explanation: There are 5 diagonals in this matrix: 1. {2} - No need to sort 2. {7, 3} - Sort - {3, 7} 3. {9, 4, 3}
8 min read
Implementation on Map or Dictionary Data Structure in C
C programming language does not provide any direct implementation of Map or Dictionary Data structure. However, it doesn't mean we cannot implement one. In C, a simple way to implement a map is to use arrays and store the key-value pairs as elements of the array. How to implement Map in C? One approach is to use two arrays, one for the keys and one
3 min read
map::at() and map::swap() in C++ STL
Maps are the container in STL which is used to store the elements in the form of key-value pair. Internally, the elements in a map are always sorted by its key. Maps are mainly implemented as binary search trees. map::at() at() function is used to return the reference to the element associated with the key k. Syntax: map1.at(k) Parameters: k is the
3 min read
Searching in a map using std::map functions in C++
Usually, main purpose of using map stl container is for efficient search operations and sorted order retrieval. As map stores key-value pair, all the search operations take "O(log(n))" time (n is size of map). Different types of search functions exists in C++ language, each having different functions. In the context of competitive programming, this
7 min read
Find optimal weights which can be used to weigh all the weights in the range [1, X]
Given an integer X, the task is to find an optimal set of weights {w1, w2, w3, ..., wn} such that we can weigh/determine all the weights from 1 to X using a two-sided weighing balance pan. Note that all the weights must be unique and n should be as minimum as possible.Examples: Input: X = 7 Output: 1 3 9 WeightsLeft SideRight Side11122 + 13333441 +
4 min read
Which strategy can be used to solve branch and bound problem?
Branch and Bound problem can be solved using different strategies such as Least Cost (LC) Search, Breadth-First Search (BFS) and Depth-First Search (DFS). These strategies help traverse the state space tree effectively, ensuring optimal solutions. Branch and bound is a problem-solving technique used in optimization problems to find the best possibl
2 min read
Create a customized data structure which evaluates functions in O(1)
Create a customized data structure such that it has functions :- GetLastElement(); RemoveLastElement(); AddElement() GetMin() All the functions should be of O(1) Question Source : amazon interview questions Approach : create a custom stack of type structure with two elements, (element, min_till_now) implement the functions on this custom data type
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg