TreeMap is a data structure that stores key-value pairs in a sorted order based on the keys. In Python, there is no direct built-in implementation of a TreeMap, but the sortedcontainers module provides a SortedDict class that works similarly to a TreeMap.
TreeMap is implemented using a balanced binary search tree (such as Red-Black Tree or AVL Tree), ensuring that the elements are stored in a sorted order and can be efficiently searched, inserted, or deleted.
TreeMap in Python
To create a TreeMap (SortedDict) in Python, we first need to install sorted containers library. To install the library, we need the following command:
pip install sortedcontainers
After installing sortedcontainers, we can import SortedDict from it.
Below is the implementation of TreeMap(or Sorted Dictionary):
Python
from sortedcontainers import SortedDict
# Creating a dictionary
d = SortedDict({2:"GfG", 1:"Geeks", 3:"POTD"})
print(d)
# Insertion in dictionary
d[-1] = "Hello"
d[0] = "World"
print("After Insertion:", d)
# Deletion in dictionary
del d[-1]
d.pop(3)
print("After Deletion:", d)
Output
SortedDict({1: 'Geeks', 2: 'GfG', 3: 'POTD'})
After Insertion: SortedDict({-1: 'Hello', 0: 'World', 1: 'Geeks', 2: 'GfG', 3: 'POTD'})
After Deletion: SortedDict({0: 'World', 1: 'Geeks', 2: 'GfG'})
Alternate Implementation:
If you cannot install sortedcontainers library, then you can also create a TreeMap in Python using a Dictionary and sort it on the basis of keys.
Python
# Creating a dictionary
d = {2:"GfG", 1:"Geeks", 3:"POTD"}
# Sorting by keys
sorted_d = dict(sorted(d.items()))
print(sorted_d)
Output{1: 'Geeks', 2: 'GfG', 3: 'POTD'}
Related Articles:
Similar Reads
Treemap in Pygal Pygal is a Python module that is mainly used to build SVG (Scalar Vector Graphics) graphs and charts. SVG is a vector-based graphics in the XML format that can be edited in any editor. Pygal can create graphs with minimal lines of code that can be easy to understand and write. Treemap in Pygal Treem
2 min read
Binary Tree in Python Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.Introduction to Binary TreeRepresentation of Binar
9 min read
Treemap using Plotly in Python Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
3 min read
Fractal Trees in Python Implementation of Fractal Binary Trees in python. Introduction A fractal tree is known as a tree which can be created by recursively symmetrical branching. The trunk of length 1 splits into two branches of length r, each making an angle q with the direction of the trunk. Both of these branches divid
3 min read
Red Black Tree in Python Red Black Tree is a self-balancing binary search tree where each node has an extra bit representing its color, either red or black. By constraining how nodes are colored during insertions and deletions, red-black trees ensure the tree remains approximately balanced during all operations, allowing fo
11 min read