• 10 hours ago
Transcript
00:00Next, we are going to talk about our tree map data structure. The tree map data structure is like a
00:04hash map, but there is also a small special property. The tree map's special property is that its keys are
00:11sorted. It is arranged in sorted order. And since there is sorting, the time complexity always increases due to the
00:19sorting. So in the key map, which I have inserted, removed,
00:24I call put and remove function, which is my get function. The time complexity of all these are big of
00:31log in. So, in the tree map, my key is sorted in order. My put, get and remove time complexity
00:39is log in. And how do they make the tree map? I will write for tree map.
00:44After that, I write the pair of key and value here, I will show the key here and here I
00:48will show the value here and then I will write new tree map. In this way, I will create an
00:52internal map which automatically when I add keys, it will go in sorted order. The methods are the same, so
01:00the time complexity increases to big off login. The internal implementation of tree map, we don't use Arrays, Link List
01:06or W Link List. We use the tree map using red-black trees.
01:13Red Black trees are a special tribe of BST trees that are self-balancing. Self-balancing means that they are
01:21balanced by themselves. Self-balancing trees we call them. And by their help.
01:27My tree map data structure is implemented. You will have a reading material about red-black trees. We can go
01:33and study it. We will understand more detail about how this tree is implemented, how the nodes are deleted, how
01:39the nodes are added, how we search elements. So my tree map nodes will be made of red-black tree
01:46and it will be implemented internally in Java my tree map data structure. Usually I have not asked a detailed
01:53explanation from this.
01:57you can tell Java that the tree map is a data structure, which internal implementation is in the internal implementation.
02:04So you should know that it is red-black trees. What will we do? We will do the basic implementation
02:09of the tree map. What did I do in my code? I made a hash map, added keys in it
02:14and made a linked hash map. Now what will I do in this linked hash map simply? I will replace
02:19it from tree map. Here is my tree map. This will be tm. I will add it here.
02:26in the tree map, all the key values and pairs. We will save it and we will print our tree
02:32map and run it once again. In the tree map, when I have added the keys, you will see the
02:40order of the keys.
02:41First of all, I have a hash map printed. In the hash map, I have first came China, US, and
02:47India. This order is a jumbled order. It is a unordered map. But what happened in the tree map? First
02:54China came, then India came, then US came. If you think about it, this is an alphabetical order. If we
03:00sorting strings, it is in alphabetical order.
03:03C value always comes from I, that's why China came first. I value always comes from U, in A to
03:10Z. That's why India came first. After last, I have US. In fact, if I add another one here, if
03:16I put in tree map, I can take my key, which I can take? Indonesia. And here I have passed
03:24the value as 6. So sorting is always on the key basis, not on the value basis. So here,
03:30There are 4 values printed inside the tree map. And here what happened? In India, IND is the same. In
03:36Indonesia, IND is the same. But because I is the first of my O, that's why India is the first
03:42of my O, and Indonesia is the second of my O. So what does the tree map do based on
03:46the key?
03:47sort my elements. Now we are going to use HashMap to solve some questions. We are going to see some
03:53implementation, some examples and questions. Then we will expand on the Sets. We will explore HashSet, LinkedHashSet, TreeSet. We will
04:03explore their internal implementation and then we will do questions of Sets. So HashMap is our important data structure and
04:10we have to understand our questions.