• 10 hours ago
Transcript
00:00Next time we are going to talk about our tree set. The tree set is sorted in order to arrange
00:05all our keys and they are by default in ascending order. If I have taken a set and I have
00:13created a tree set type, then if I add one, two, three, four elements,
00:20I will arrange in this way that when I will iterate on the set, I will have 1i, then I
00:26will have 2i, then I will have 3i and then I will have 4i. So the order will be sorted
00:32in that to my ascending order sorted. Besides, there is another special thing that tree sets are implemented internally in
00:38the form of tree map. That means tree map uses a self-balancing binary search tree. There is an implementation
00:44of tree map in red and black trees.
00:49Red Black Trees we have already read, what are the reading materials that you have given you in binary search
00:53trees. In AVL Trees we have also read in BSTs that they are self-balancing trees. Now Red Black Tree
01:01is a self-balancing BST, which is in the form of tree map and tree set is implemented. So, who
01:08will be implemented with this? We will remember that it will be easy to tell in interviews. With the tree
01:14set, we can't insert null values inside the tree set.
01:16My hash set or my linked hash set was allowed to insert the null value as my key
01:27But tree set is not allowed. This is not allowed because tree set is not sorted internally. And what do
01:34you need to do for sorting? You need to compare elements 1 and 2. If it has elements 1 and
01:392, then you will see which small and which big is. So that they can arrange it in a tree
01:44set. But because with null value, it doesn't matter if null value is bigger or small. So null values are
01:53simply not allowed to be added in a tree set.
01:57With the tree set, insert operations, remove operations or other operations, the operations are usually in hash set and linked
02:06hash set are constant in the tree set. In the log in time, we have to log in because we
02:13have to sorting internally. That's why the tree set is less optimized as compared to hash set and linked hash
02:20set. For this, we are going to write code.
02:23I have already created a hash set in which many cities are named. I have created a linked hash set
02:28in which many cities are named. Now what will I do? I will create a tree set in the same
02:34manner. I will create a tree set. I will create a string type. I will give it the name. ts
02:39is equal to new tree set. I am going to add the same values. Here I will have ts.
02:56And now I will print my tree set in last. Now we will compare these three. How do we live
03:02internally in the hash set? How do we live in the linked hash set? And how do we live in
03:05the tree set? So what happened in the hash set? In the hash set there is a normal order. In
03:14any random order, my cities are printed. In the linked hash set, my cities are printed. And in the tree
03:22set, this is the sorted order.
03:26that to ascending order, meaning B comes first, so B will be printed first from B, which is Bangalore, then
03:33Delhi will be printed, then Mumbai will be printed,
03:38So what did tree set? Automatically arranged them in sorted order Now all the functions are in my hash set
03:45Those functions are in my tree set So all the functions are in my tree set So all the functions
03:49are in clear function, is empty function, size function, remove function, contains function and iterator or advanced we talk about
03:57the for loop Those things are in the hash set, linked hash set and in the tree set exist