Javatpoint Logo
Javatpoint Logo

How TreeMap Works Internally in Java

In Java Interview Question, the most commonly asked question is how TreeMap works internally in Java or what is the internal implementation of TreeMap.

In this section, we will learn how TreeMap works internally in Java. Before moving to the internal working, first, understand what is TreeMap.

TreeMap in Java

TreeMap is a class that is a member of the Java Collections Framework. It implements Map, NavigableMap, Sorted, Cloneable, and Serializable interfaces. It extends AbstractMap class. Like HashMap, it also stores data in key-value pairs. The difference between HashMap and TreeMap is that the HashMap does not sort the key while the TreeMap sorts the key in the natural order (ascending order). We can also customize sorting using the Comparator.

It is slow in comparison to HashMap and LinkedHashMap. Its class methods such as get(), put(), containsKey(), etc. provides O(log n) complexity. It is fail-fast in nature which means it is not thread-safe. For the multi-threading environment, we can make it thread-safe.

As we know that, in a tree, a node has three references parent, right, and left element. In the following figure, we have shown the structure of a node in TreeMap.

How TreeMap Works Internally in Java

The tree data structure follows the property, given below:

  • The left element will always less than the parent element.
  • The right element will always greater than or equal to the parent element.
  • The logical comparison of the objects is done by natural order.

Internal Working of TreeMap

Like HashMap and LikedHasMap it does not use hashing for storing key-value pairs. Internally, it uses a data structure called the Red-Black Tree. In other words, it sorts the TreeMap object keys using the Red-Black Tree algorithm. For understanding the internal working of TreeMap, we must understand the Red-Black Tree algorithm.

Red-Black Tree Algorithm

Red-Black Tree is a self-balancing binary search tree (BST). It has the following properties:

  • In the red-black tree, the color of the node is either red or black.
  • The root node is always in black color.
  • The red color node cannot have the same color neighbor node.
  • All paths from the root node to the null should consist of the same number of black nodes.

Let's understand the implementation of TreeMap through an example.

TreemapExample.java

Output:

B Delhi
D Jaipur
F Agra
H Ahmedabad 
P Patna

Let's see how these elements are stored in the TreeMap in the natural order.

Adding the first element

In the above code snippet, we have inserted the first element (H, Ahmedabad). Where H is the key and Ahmedabad is the value. It will be the root node of the TreeMap. The TreeMap will store it, as we have shown in the following figure.

How TreeMap Works Internally in Java

Adding the second element

We have inserted the second element (D, Jaipur). Where D is the key and Jaipur is the value. The key D is logically smaller than the key H. According to the rule, the smaller value will place left to the parent. Hence, the value Jaipur will be placed to the left of Ahmedabad, and the value Ahmedabad becomes the parent of Jaipur. The value Jaipur will be store in the TreeMap, as we have shown in the following figure.

How TreeMap Works Internally in Java

Adding the third element

We have inserted the third element (B, Delhi). Where B is the key and Delhi is the value. The key B is smaller than the key D. Hence, it will add to the left of the Jaipur and the Jaipur becomes the parent of Delhi. The value Delhi will be store in the TreeMap, as we have shown in the following figure.

How TreeMap Works Internally in Java

Adding the fourth element

We have inserted the fourth element (F, Agra). Where F is the key and Agra is the value. The key F is greater than the key B and D. So, it will store in the tree to the right of Jaipur, as we have shown in the following figure. The Jaipur will be the parent of Agra, also.

How TreeMap Works Internally in Java

Adding the fifth element

We have inserted the fifth element (P, Patna). Where P is the key and Patna is the value. The key P is greater than the key B, D, and F. So, it will store in the tree to the right of Ahmedabad, as we have shown in the following figure. The Ahmedabad will be the parent of Patna, also.

How TreeMap Works Internally in Java

After inserting all the elements in the tree, the sorted TreeMap looks like the following:

How TreeMap Works Internally in Java

The above tree represents the sorted key. We read the above tree as, B, D, F, H, P.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA