Podcast Lookup

Java Queue Interface Explained | PriorityQueue, Heap Data Structure & Practical Examples

The Java Developers Hub

  • December 3, 2026
  • 1 h 17 min
  • Episode 22
Episode page
About this episodeThe Queue interface is an important part of the Java Collections Framework, providing a structured way to manage elements based on…
The Queue interface is an important part of the Java Collections Framework, providing a structured way to manage elements based on processing order. Queues are widely used in task scheduling, job processing, resource management, and many other software applications.In this episode, we take a deep dive into the Java Queue interface, exploring how it works and how to use PriorityQueue to organise elements according to their priority.You'll also learn about the Heap data structure, its relationship with PriorityQueue, and how Java efficiently retrieves elements based on priority rather than simple insertion order.Whether you're learning Java collections or preparing for data structures and algorithms interviews, this tutorial will strengthen your understanding of queues and priority-based processing.What You'll Learn
  • Understanding the Java Collections Framework.

  • What the Queue interface is and how it works.

  • Understanding FIFO (First In, First Out).

  • Important Queue methods: add(), offer(), remove(), poll(), element(), and peek().

  • Differences between remove() and poll().

  • Differences between element() and peek().

  • Understanding PriorityQueue in Java.

  • How PriorityQueue organises elements by priority.

  • Understanding Min-Heap and Max-Heap concepts.

  • How the Heap data structure works internally.

  • Using custom comparators to define element priority.

  • Working with PriorityQueue through practical examples.

  • Common applications of queues and priority queues.

  • Best practices for using Java Queue implementations.

Who Should Watch?
  • Beginners learning Java Collections.

  • Computer science and software engineering students.

  • Developers studying data structures and algorithms.

  • Java programmers preparing for technical interviews.

  • Backend developers working with task scheduling and processing systems.

  • Anyone interested in mastering Java's Queue interface.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Queue Interface Explained | PriorityQueue, Heap Data Structure & Practical Examples

00:00—
  1. Speaker 1

    Welcome back coder army to our java series and today we are going to understand Q interface in depth. We will understand how Q is implemented in java and how stack is implemented. Again you will have a lot of fun, you will definitely learn something. So let's go to the screen and start it. So we are going to talk about our Q interface.

  2. Speaker 2

    Okay? Okay.

  3. Speaker 1

    Now, first of all, to understand Q interface, we have to see how we can implement Q by ourselves. Right? That's why we will understand how Java has implemented it internally. But let's revise this a little here. If we want to implement any Q, So we have two ways, one is by using array and the other is by using our link list. We have both the implementations. Same if we want to use stack, because we understand stack with queue. So we can do stack in two ways, one is by using array and the other is by using link list.

  4. Speaker 2

    So these are the two ways we have to implement stack and queue.

  5. Speaker 1

    We just have to remember that whether we are using the array method or the linked list method, in the case of stack and queue, we need our insertion and deletion time complexity to be constant. So basically we can insert an element in a constant time and we can remove an element in a constant time. I want this from any queue and any stack. This is its basic requirement. So, first of all, let's see what is the array implementation of a queue. So, what is a queue? We know that a queue is our first in first out. So, basically, if this is our queue and we put some elements in it, like I put 4, then 4 came here. Then I put 5, so let's say 5 came here. Then let's say I put 6, So 6 came here. So basically, as we imagine a simple line, that is a queue. So as I am adding elements, they are coming in from this direction. Let's say after this, 7 came, after this, 9 came, after this, 10 came, something like this. Now when I will start removing these elements, they will come out from their front. So one part will be its front and one part will be its rear. Elements will enter from the rear and come out from the front. This can be a special thing about Q. First in, first out. The element which came in first, the first four came in first, that will go out first.

  6. Speaker 2

    Okay.

  7. Speaker 1

    So this is our basic Q data structure. We know this. And if we want to implement it using an array, then we will have to think like this. So let's say we will take our one array, something like this. I'll make some indexes in it. 1, 2, 3, 4, 5, 6. Let's say I've made 6 indexes. I've written their indexes too. And I can do one more thing, I can take two pointers. One is front, I will take this one as well. Which will monitor what is the front part of Q. And one is rear. So for now, let's say front and rear, I have taken both as minus 1 and minus 1. That means they are outside our index, somewhere here. Here our rear is also there, front and rear both are minus 1.

  8. Speaker 2

    So what will I do?

  9. Speaker 1

    As soon as I insert an element, let's say I have to insert 4. What will I write in the code? Let's say my add or insert code, generally what do we call it in the Q part? Let's say we say offer. I am offering a new value. In Java, it is called offer. But generally, it is also called NQ operation. In which we put some value in Q. So, let's assume the term offer in Java. So, let's say I said int i. I will put a value. So, how will its code be written? It will be written in this way. First of all, we have to do rar plus plus because rar is minus one. So, we will move rar one level ahead. It will come to zero. And whatever our array is, internal array. We will put our element i on its rare. So here I have put let's say 4. I could have written both of them in one line. I could have simply written arr of plus plus rare equal to i. Right. First increment the rare and insert the value of i in it.

  10. Speaker 2

    Okay. So 4 is inserted. Okay.

  11. Speaker 1

    Now in the same way, suppose I insert another element 5. So again I will do rare as plus plus and 5 will come here and rare will come here. Okay. One more thing, I will update my front element here as well And I will update my front element on the starting element when I will be inserting the first element Because.

  12. Speaker 2

    You know who should I point my front element to?

  13. Speaker 1

    The one that came first in my array So I will remove this to make a place for it so that we can understand how it works Now let's say I insert another element, I can insert as many as I want Let's say I insert 7, again my rare increased so it will be removed from here Then I insert 9 Then it will move from here.

  14. Speaker 2

    To here and so on. If I have to remove an element, I can remove it from the front.

  15. Speaker 1

    To remove an element from a queue, let's use a poll function in Java. And let's say it doesn't take any argument. And we know that the element will always come from the front. So what can I simply do? First of all, I can take an int I and consume it in my int I which is in my ARR of front. Right? So in the starting, it got 4. And after that, what do I do? I do the front plus plus. So I have updated my front. Now my front has moved from here to here. This is the front. And here is my rear. And return it. I, something like this. And what was I? It had a value of 4. Something like this. So I returned 4. Okay? And I updated my front and rear. So I can do this. Now you will say, 4 is still written here.

  16. Speaker 2

    No problem.

  17. Speaker 1

    It will be replaced by some new value. Let's say, now I insert more here.

  18. Speaker 2

    10.

  19. Speaker 1

    So you know, we will update rear. And rear will come here. Then I insert 11. Then my array will come here. In some way, our function will keep running. Okay, now if my array is filled, then what can I do? My array is filled, but you think, I still have this location empty. Because the front is here. So I can use this array as a circular array. That after this, if someone asks me to insert something else, then I do rarer + + and that + + comes here automatically.

  20. Speaker 2

    How can I do this?

  21. Speaker 1

    We have understood that modular operation is very useful in this. So what can I do? My size of the array, how much size is here? So, I can mod it with n which is 6 in this case. So, I simply write mod 6 because you know that mod 6's output can come from 0 to 5. So, as soon as I do 5 plus 1 mod 6, then 5 plus 1 mod 6. Basically, the next element will be 6 mod 6 which is equal to 0. So, basically, I will be performing the calculation on 0 and I will insert the next element here. So, let's say I have inserted a new element 19 and my rar is here. I can do something like this. But now my array is actually filled. Because now the next element of the rare is front. So whenever the next element of the rare is front, my array is completely filled. Now here I can say that my array or my queue is full. If I want that my queue should be of limited size. But if I don't want it to be full, then I have to resize it. I can take a bigger array internally. Let's say its size can be double. For now, I agree. Later, we'll see how Java does it internally. And I can copy the old values in a new way. Now there are two ways to copy. I can copy from front to rear like this. 5, 7, 9, 10, 11, 19. And this is my front, 5. And this is my rear. I can copy something like this. And the second way to copy is to copy the values as they are here. So here it is 19, this is my rare. And let's give a little gap to the front. Let's say there are 5 elements. So from here I will start the front with 5, 7, 9, 10 and 11. So that the next element, here my front starts with 5. So for my next element, 2 spaces will increase. And if this array was bigger, So I would have started this layer from the back, here I would have got more space. There are different approaches to start, we can use anything. In a while we will see which approach Java uses. But basically you understood how we can implement a queue using array. The only problem we will have is that we will have to do resizing when our queue will be full. And we will have to use circular queue so that we can use every location of our queue properly. Otherwise, when you move the front forward, then the locations at the back will never be covered. If you don't move the rear and store some things in it. Right? So now let's see what can be our linked list implementation. So let's say I'm going to have some elements again. 2, 7, 9, 3, 5, something like this.

  22. Speaker 2

    And what will I do in the starting? I will have all the nodes empty.

  23. Speaker 1

    You know in linked list, I have a class called node. And there are two things in it. One is our data. So let's say int data. And our node pointer is next.

  24. Speaker 2

    Node next. This is how we make a node of our linked list.

  25. Speaker 1

    Okay, so what we will do is, our starting queue will be empty, so as soon as I get an element, I will make a new node. Let's say I made a new node, 2. And this will be my head. Let's say this will be my front and this will be my rear. So front and rear, something like this. Now let's say I want to enter a new element, I want to enter 7. So what will I do? I won't do anything, it's very simple. In this rear, I will insert a new element, let's say the next element is 7 and I will make the rare plus plus, now my rare will be pointing to this, something like this. Then let's say I have to enter 9, again I will create a new node, I will move a pointer next to the rare, now my rare is this and I will insert 9, something like this.

  26. Speaker 2

    Okay, if I have to remove an element.

  27. Speaker 1

    Then I know that my element will be removed from the front, I don't have to do anything, I have to remove an element from the front, which is stored in the front and I have to move the front one step ahead, okay? So let's say I can write something like this, let's say int data, is equal to front.data. So what is the data of front?

  28. Speaker 2

    It is 2. So it is in the data. And I will make front equal to.

  29. Speaker 1

    Front.next, so I moved the front one step ahead and I will return our data something like this and moved the front here. Now if I want I can delete this whole node or I can leave it like this. So now my front is in this position. Now it says in the same way more elements will come. So let's say 3 came here, 5 came here. So I am expanding the link list and there I am Now the best part of the linklist is that we don't need to use circular queue and we don't need to worry that our linklist will get filled.

  30. Speaker 2

    Right?

  31. Speaker 1

    In the case of array, we had to resize it every time to make dynamic array. But there is nothing like that in this. You can put infinite elements in the linklist.

  32. Speaker 2

    Right?

  33. Speaker 1

    And because we are not wasting any node, you can delete this node whenever you want. Whenever you visit this node, delete it and move the front forward. So, if you are not wasting any node, then we will never need to use a circular queue, right? So, in the link list, it seems to be a very good way to implement the queue.

  34. Speaker 2

    Although, the time complexity of both is the same.

  35. Speaker 1

    In both, insertion and deletion takes a constant time, takes an order of one time, one insertion and one deletion. But yes, the linked list looks more optimized because we don't have to waste any node or any circular queue. Now we have seen the queue, now let's see the stack. We have another data structure called stack. Now the stack queue is the opposite of LIFO. LIFO means last in, first out. If we understand the stack conceptually, it looks like this. Let's say you put some elements in it, so let's say I put elements here 5, 10, 3, 4 and 6 something like this. So first you have to put 5 in the stack, then you have to put 10 on top of that, then you have to put 3, then you have to put 4 and then you have to put 6. So your stack looks like this. Now you know what last in first out means. So I must have entered in the last, at the very end 6 must have gone inside. Now when I take something out of this stack, then 6 will come out first, so the last in is coming out.

  36. Speaker 2

    That's why it's called a Leapfrog.

  37. Speaker 1

    Because we know that in a stack, entry and exit are from the same place. It's not like a queue where we enter from the rear and exit from the front. Right?

  38. Speaker 2

    So here, basically, we have to keep entry and exit together.

  39. Speaker 1

    Now if I want to implement this kind of data structure with an array, then how can I do it? It's simple. I'll take an array. Let's say I took an array. It will have some indexes. For now, let's take it like this. And I'll take a pointer, let's say, top. Which will maintain the current top of my array. And now, initially, I'll make the top minus 1. So, if these are my indexes, 0, 1, 2, 3, 4, 5. Now, currently, my top is pointing to minus 1. Now, as soon as I put an element, let's say, in the language of stack, we call it push. So, let's say, I'll say push 5. So, push an element. What will I do?

  40. Speaker 2

    Nothing.

  41. Speaker 1

    First, I'll make the top I will move the step forward and put the element on the index So basically arr of top is equal to i Before that I have added top as plus So I can do something like this Same let's say I want to add 10, I have added 10 and moved the top forward Now I have to add 3 and my top is here Something like this So now we know how to handle it. Now let's say I add 4 and 6. 4 will come here and 6 will come here. And my top will move here.

  42. Speaker 2

    Right? Something like this.

  43. Speaker 1

    Now if I want to remove the element from the stack. So in the language of stack we call it pop. Something like this. Right? Now if I want to pop an element from the stack, what can I do? I can return whatever is currently stored in my top and I can minus one step from the top. So if I write a pop code, it should look something like this. First of all, I will take an int i which is stored in my arr of top. I will do minus minus the top and return our i. I can do something like this. So basically I have decremented my top by one element. Now there is still an element here but what difference does it make? Our top is always this, right? Now if someone asks me to push it again, what will I do? I will update it. Let's say someone said push 7. So what will I do? Again I will do plus plus on top and update whatever was here with 7. Top again came here. Now I don't need to use any circular array here. Because imagine if I insert another element, 8, then my top is here. Now my array is filled, no problem, I will resize my array. I will again take the size of the double array and copy all the old elements in my new array, 7, 8, something like that. And I will have some spaces empty here. So 8 was my current top, now I can enter more elements. Now because my entry and exit are happening from the same place, I don't need to use the concept of circular array. Okay. Similarly, I can implement this in a linked list as well. In a linked list, I have an element i.e. 2, I have placed the top element here and inserted it. Then I have an element i.e. 3, I have moved the top element forward and placed the top element here. Then I got an element of 4, I made a new node of 4 and moved the top in front of it. Something like this. Yes, now there can be a problem here. If my linked list is single, let's say I am using a.

  44. Speaker 2

    Single linked list.

  45. Speaker 1

    So if I have to pop, then I will return 4, but I can't take the top one step back. Right, because this node does not have access to the node at the back. So in that case, either I will have to use a doubly linked list. In fact, Java uses all the linked lists as double linked lists from inside. So we don't have a problem with that. Or if you want to understand conceptually how to do it in a single linked list, then we will have to change our design a little. In that case, let's say the first element that will come is 2, I will call it top. But when a new element comes, I will insert it behind it, not in front of it. So basically insertion will always be on the head and my linked list will keep on increasing. For example, now if I want to insert 3, then what I will do is, first of all, I will make a new node. 3, okay.

  46. Speaker 2

    And I will point it with a variable temp.

  47. Speaker 1

    So let's say the code will be like this. I am pushing the element. So push int i. So the code will be like this. First of all, I will make a new temporary node. Equal to new node. And I will put value in it. I comma null. Now it is not pointing to anyone. After that, I will do the next method of temp. I will put current top Because here was top So basically now temp will point our top And finally I will put in top Whichever is in temp So now finally top will move from here to here And now I can remove this stamp. So basically my element is inserted. Now if I imagine my stack, then 2 comes, then 3 comes, and the top will always be ahead. Now if I want to pop, then I can pop 3 and move the top one step ahead. So in a way, 3 pops from here. So in the same way, you can understand that our link list is 2 and 3, something like this. If a new element comes, let's say a new element 4 comes, then I will do the same thing, I will make a new node, I will put 4 in it, I will make it point to.

  48. Speaker 2

    The 3 one and I will update the top here.

  49. Speaker 1

    So this is one way if you want to use a single linked list, but we don't need to do this because internally java always uses a double linked list. So we saw the implementation of both stack and queue in both ways, whether we used an array Even if we used a linked list But we understood how to implement them in a conceptual way by writing our own code Now let's talk about our hierarchy So you know that we have an iterable interface at the top And just below the iterable comes our collection interface So let's say our collection is here Okay Now this collection interface extends to a new interface which we call Q interface. And here we talk about our Q. Now see what is the further hierarchy. From this Q interface, we get another interface called DQ.

  50. Speaker 2

    What is DQ? DQ means doubly ended queue. W ended queue is nothing special.

  51. Speaker 1

    Imagine a simple queue in which you can put an element in the front and also in the rear. You can also remove an element from the front and also from the rear. Basically, insertion and deletion can happen from both the sides.

  52. Speaker 2

    This is called W ended queue or DQ.

  53. Speaker 1

    In Java, we have an interface called DQ which extends our normal queue interface. And below this comes our proper class which is named ArrayDQ. This is a proper class. So what will it do? It will extend our DQ, Q, collection and iterable indirectly. So basically it will override all the methods of ArrayDQ. Now we can use ArrayDQ properly as a queue data structure. And which queue can it work as? It can work as a double-ended queue. Now think that the queue that is already double-ended can also work as a single-ended queue. Don't insertion and deletion from both the sides. Insertion from one side and deletion from the other side. So it becomes a single-ended queue. If you do it from both the sides, it becomes a double-ended queue. So you can use ArrayDQ as a single-ended queue or as a double-ended queue. Now the interesting thing here is that this arrayDq name is array implementation of queue. It is an error implementation of a queue. But we know that queue can be implemented from a linked list as well. The answer is yes, it can be. So, has Java given such a method to implement a linked list queue? The answer is yes, it has given it. If you go back in the hierarchy a little bit, then you know that from this collection, we get another interface, which we call list interface. Right?

  54. Speaker 2

    You will remember.

  55. Speaker 1

    And this list interface is implemented by our very special class, which we call linked list. Right?

  56. Speaker 2

    You must know that this is a class.

  57. Speaker 1

    Now interestingly, our link list class implements this DQ interface as well. This means that we have an implementation of such a queue which is done through the link list the linklist. So the linklist implements the list and the linklist implements the DQ. So I said that you can use the same linklist normally as a list. or you can use it as a queue. So you can use ArrayDQ or you can use LinkList. LinkList is your normal implementation and ArrayDQ is your normal array implementation of queue. But where did the stack go in all this? We didn't make any class of stack. In fact, we had made this, that there is a special class in our list, stack class, and you said, now that has become legacy class. Right?

  58. Speaker 2

    Now we don't use it much. So, what do we use to make stack?

  59. Speaker 1

    Answer is, same class, arrayDq, we use. So, arrayDq class, not only implements our queue, but also implements stack.

  60. Speaker 2

    How, brother?

  61. Speaker 1

    Now think, what is stack? If you put a little brain in it, What is stack? Stack is a data structure from where you can enter and exit from the same site. But we also have double ended queue in Java which we call DQ. Right? Which is implemented by two classes. Array DQ and a link list. We just saw the.

  62. Speaker 2

    Link list above.

  63. Speaker 1

    Now think, if we already have double ended queue, then can we use it like a stack?

  64. Speaker 2

    What happens in double ended queue?

  65. Speaker 1

    There can be insertion from both sides and there can be deletion from both sides. If I close one side completely, let's say I said that I will not use this side. So basically I put a wall here. So now this is not my whole stack. This is the entry and this is the exit.

  66. Speaker 2

    Right?

  67. Speaker 1

    The design of the collection framework of Java is so beautiful that Java has only one class ArrayDQ which you can use as a queue or as a stack. Because stack is nothing but a restricted use case of a double ended queue. This is our double ended queue and this is our stack. Entry from one side and exit from the other side. Double ended queue says that I will close one side and you can exit from the other side. So stack is basically a restricted use case of a double ended queue.

  68. Speaker 2

    So I can use it.

  69. Speaker 1

    In fact, if you think carefully, you are using this array as a single queue. You can also use it as a double queue. And you can use it as a stack. So again, this is a very special class. It has a lot of use cases. In fact, why only ArrayDQ? Because our link list also implements this DQ interface. So I can use the link list like a stack. Because stack is nothing. Stack is not a new data structure in our Java. Java says that if you want to use stack, then we have some functions of DQ. You think like this. If I explain it carefully. So let's say this is my DQ. It can have entry from both sides and exit from both sides. So Java says that if you want to use this queue as a stack, then don't exit from both sides. Imagine that one side is closed. So you basically use only one side. So let's say I call this side as front and this side as rear. So Java says that you just use front. Now Java must have given a function to insert from front and remove from front. Right? Of DQ. Right? So let's say I call that function offer first Basically if you want to insert an element in starting then that is offer first Use that for insertion So let's say you put 4, 5, 7 something like that And to remove that element use poll first So whenever you call it I will remove the first element from this front So basically to implement the stack you can use these two functions You ignore these two functions So basically we will have an offer last which will insert an element i in the last or we will also have an element pole last a method pole last which will remove an element from the last I am saying that you don't use these two methods you just use the starting methods right so java gives us this privilege that we use the same data structure in multiple ways in fact as a data structure In stack, we use push keyword to push data and pop keyword to pop data. This is a naming convention that comes from another language and generally used in data structure. So Java said that name should be second, so call this offer first as push i and call this poll first as pop. This is the same name, whether you use this one or that one, it will work the same way. And whether you use pop or pole first, it will work the same way. So Java did something like this, so that we can use stack, using array deque, and also gave special methods for it. That special method is nothing, internally they are calling this method, it is just a wrapper on them. So that some people get confused, if they are using stack, then why are they using this kind of method? Offer, pole, this should be a queue method. So he said, okay, I will give you a wrapper on it, push and pop, we have a habit of this, right? So Java also gave us this. In the end, we will look at all the methods of our queue and stack, then you will understand better which methods we have. Now let's see what is the internal implementation of our java, internal implementation of both the data structures of queue and stack.

  70. Speaker 2

    Look, first of all.

  71. Speaker 1

    We talk that we had an interface called DQ, this was our interface and it was implemented by two classes, one was our array DQ, array DQ, which I had told you is array implementation of queue and we also had a link list here which link list was a class in a list which is basically a linked list implementation of our queue. Now, whether it is ArrayDQ or linked list, both of them implement DQ from inside. That means both of them make a doubly ended queue. And we can use that doubly ended queue as a normal queue, or we can use it as a doubly ended queue, or we can use it as a stack.

  72. Speaker 2

    Okay, so we know this.

  73. Speaker 1

    Now, first of all, let's see how ArrayDQ implements this queue from inside. Now, this is exactly the same way that we had imagined that Java would do for ArrayDQ class.

  74. Speaker 2

    Let's say ArrayDQ.

  75. Speaker 1

    In this class, there will be an array like this. It will have some indexes like this. And we will have two pointers. One will be rear and one will be front. Something like this. Right? It's the same implementation. And it's the same if we want to enter an element. Let's say I want to put 5 in this queue. So what I'll do is move the rare one forward.

  76. Speaker 2

    My rare is here. And I'll insert 5.

  77. Speaker 1

    Okay? And again, I'll bring my front to 0. Because now I have an element in the queue that exists. So the front and the rare will be at 0. Now if I want to insert one more element. Let's say I call it 7.

  78. Speaker 2

    So you know what I'll do. I'll move the rare one forward.

  79. Speaker 1

    Here.

  80. Speaker 2

    And at that place I will insert 7.

  81. Speaker 1

    My front will be here till I don't pop. Again I have to do 9. You know 9 will come here. My rear will move here. So basically what we have just imagined is what Java does. You know if my array is full. Let's say I put a 10 here.

  82. Speaker 2

    So my rear will be here.

  83. Speaker 1

    And let's say after 10, I put another element 11, then you know what I will do, I will move the rare one more step ahead, my rare is here and 11 has come here.

  84. Speaker 2

    Now my queue is.

  85. Speaker 1

    Filled, now if I try to push one more element, let's say I said push 6, then what will happen from inside, it will be resized. I will make a new array and how big will I make an array? I will make a 50% big array, right? It is the same concept that we use in the list. So what was its size just now? 0, 1, 2, 3, 4. Okay, so it was the size of 5 elements. Next time, I will take its 2.5, i.e. 7.5.

  86. Speaker 2

    Now 7.5 is not a number.

  87. Speaker 1

    So I will take its ceiling value, let's say 7. I will make an array of 7 sizes.

  88. Speaker 2

    So 1, 2, 3, 4, 5, 6, 7. Something like this. I have done its indexing. Okay.

  89. Speaker 1

    Now, if I copy the old value in this, then let's say, here comes 5, 7, 9, 10, 11, and this becomes my front, this becomes my rear, where did it go? Yes, this becomes my rear. Now, I have more spaces to enter the value. Okay, does Java use circular queues from inside?

  90. Speaker 2

    The answer is yes, it does.

  91. Speaker 1

    Now imagine that someone has pulled the element out of the current queue twice So once I pull it out, my front will be updated here once So my front will come here and I will ignore this element, I will assume that there is nothing here I will have returned it, same if someone pulls it out once more, then this time it will come out 7 You know what will happen, my front will come here again And I will ignore this 7. Something like this. Now I know that two spaces are empty here. And next time when I have to insert, I can insert here. Again, Java can use the modulo operation from inside. So what I will do? I will use modulo 5. So whatever new element I have to push, next I have to find the index. So how do I find the index? Basically, whatever my current index is, i plus 1 mod 5.

  92. Speaker 2

    Okay?

  93. Speaker 1

    So if I want a new index in front of 4, then 4 plus 1 is 5. So if I do 5 modulo 5, the answer will be 0. So my new index is 0. So I can insert it in the next element. So let's say I insert the next element as 10. So I insert 10 here and move the rare one step ahead. So the rare is here. Now I have one more empty space. So let's say I insert another element, 12. So I insert 12 and update the rare. Now you know whenever the next element of my rare is front, it means that my array is full. Now if I have to insert a new element, then I don't have any space left in it.

  94. Speaker 2

    I have to create a new array.

  95. Speaker 1

    So again I create an array of 7 sizes. 1, 2, 3, 4, 5, 6. 0, 1, 2, 3, 4. 5, 6, something like this. And I copy the old values. Now java does the same thing here. It copies the values as it is. So for example, here it will copy 10 and 12. So till here mine is rare. And the front values 9, 10, 11, it will insert them in the last. So basically it will insert 9 10, 11 here. To maintain that circularity, my front will come here. So what Java tries to do is to maintain this circularity. If the front was here and the rear was here, then Java will copy it as well.

  96. Speaker 2

    10, 12, as it is, is copied here, so the rear is here.

  97. Speaker 1

    And 9, 10, 11 were in the last, so 9, 10, 11 was inserted in the last and the front is in the front. Okay? So that the new element that I will insert now, I will insert it in both these spaces. If Java wanted, then it could have removed all of these. It would have started from the front, 9, 10, 11 and then 10, 12. And it would have filled them here, 9, 10, 11, 10, 12. And it would have made an array of them. It could have done this as well. And it would have left the previous elements empty. But it preferred this technique. You just need to understand that the Q in Java is a double-ended Q, right?

  98. Speaker 2

    So it's not like we'll just remove.

  99. Speaker 1

    An element from the front and put an element in the rear If you want, you can put an element in the front or remove an element from the rear Now if I want to put an element in the front, I'll put it in this position If I want to put it in the rear, I'll put it in this next position Something like this Again, the front will come in front of my rear and I'll say that my Q is full There are a few optimizations, there will be a few changes But you know, you can insert from anywhere and you can delete from anywhere Now if we go to our linklist implementation, then you know that our linklist class also implements our DQ interface. So it will do this, if we want to insert some elements 2, 5, 10, then it will make a node of 2, it will call this let's say rare. Now if someone wants to insert 5, then again it will make a new node of 5 and move the rare forward, here will come our front, basically about which we had talked above, something like this. Now again, if we use double ended link list in java, then we can go forward and backward. Insertion can be done from front, rear and deletion can be done from front and rear.

  100. Speaker 2

    Why?

  101. Speaker 1

    Because we use DQ from inside. So this design is exactly the same which we talked about earlier. Now if we talk about stack, then we understood that stack is not a new data structure in java. Who implements stack? ArrayDQ can also do it. So if ArrayDQ implements the stack data structure, then it will make the same array from inside. Java doesn't even know that you want to use it like a stack. Java has given you some methods, with those methods you can either push something in the starting or remove something from the starting. You can push something in the same end or you can remove something from the end. It's up to you that you thought that I will not remove anything in the last, I will not put anything. I will just remove the starting element and put the starting element. So basically I will use my LIFO property. So you used the stack in a way. So you can use this array internally as a stack. Java doesn't need to know this or Java doesn't need to use any new data structure. What will you do in that case? You will insert from the same end, you will delete from the same end. Okay, now let's move ahead and see that out of these two classes, we had an array DQ and a linked.

  102. Speaker 2

    List.

  103. Speaker 1

    Out of these two classes, Which is the best class to use queue? It may seem that we think that linked list implements stack and queue in a very good way. Right? Specifically in the case of queue, because in queue we don't have to use any concept like circular array, we can just insert the element ahead. Something like this. And because it is a double ended queue, we can scale it infinitely. We will never have a problem that our linked list will be filled, because linked list never fills. We can push any number of elements in it. and neither will we ever have to use any circular. So basically, we won't have to apply any mod operation.

  104. Speaker 2

    Right?

  105. Speaker 1

    So in thinking or seeing, it seems that the linked list is a good implementation. And again, whether it is array or linked list, both have constant time complexity. To insert or delete an element, to insert or delete one element, right?

  106. Speaker 2

    Not all of them, right?

  107. Speaker 1

    But still, if you talk about Java docs, then Java's Oracle docs prefer that you always use ArrayDQ to implement your stack or queue. In almost 99% cases, ArrayDQ is the best use case for any queue or stack implementation. Link list can be a very specific use case, where you need specifically link list implementation, then you can use it. But Java docs themselves say that if you want queue or stack, then always use ArrayDQ version. Now, why is it like this? Think about it yourself. To implement any queue or stack, what advantages do we get in the form of an array? First of all, because the array is contiguous, its biggest advantage is that it is cache-friendly.

  108. Speaker 2

    Cache-friendly. Right?

  109. Speaker 1

    I had told you that because of the continuity of the array, what advantage do we get? When we fetch the array in our RAM, in our CPU processors, then we fetch the index around it too. Let's say 0, 1, 2, 3, 4 indexes. If I had to fetch this 0th element, So I fetched these 3 memory from 0, 1, and 2. If I want to fetch the next element, I don't have to ask the memory again. So this is called array cache friendliness and it is very useful. Especially when you are talking about queue or stack. Because you know we have to play with pointers. When you insert a new element in the queue, then you insert it in the next element, in the next position. So in the case of array, it gets optimized a lot. Whereas in a linked list, your memory location is not continuous, it has different nodes. One could be on 1000 memory, one could be on 3000 memory, something like that, right? So you may like the linked list, but array dequeue is always a better way to implement the queue or the stack. One interesting thing is that if you use array dequeue, to implement queue, then you can't insert null in it. Same goes for stack queue, because you know both are the same data structure from inside. But if you use linked list to implement stack or queue, then you can insert null. This is allowed, here null is not allowed. So basically ArrayDQ is a little bit stricter than Linkless But generally when you talk about queue or stack So what is preferable is that you don't insert null Because null is always the way to find out that there is no element Rather than null itself is an element You think you put 4, 5 in queue and put a null.

  110. Speaker 2

    And then 10. So how do we know what null means?

  111. Speaker 1

    So for that separation, we always prefer that we never use null as an element. We always use null to find out that the element is not present there. Many of you might be thinking that we already had a stack class in our list interface. We don't use the stack class in the legacy class. Instead, we use ArrayDq as a stack.

  112. Speaker 2

    Why?

  113. Speaker 1

    Because I told you that our stack class is a legacy class. And it is very slow. We don't prefer to use this stack. We prefer that we use our ArrayDQ like a stack. ArrayDQ is a double ended queue. What we will do is, we will call the same methods that will support us to use the stack. So, last in first out, we need to enter from the same place and exit from the same place. In this way, Java doesn't need to know that we are using the stack. Although, Java has given us specific methods like push, pop, which are calling the same queue methods from inside. But we get a wrapper so that we remember that we were working with a stack, right? Let's say we don't forget in the middle that we can enter something from the back, let's say we don't do that, that's why we have met the methods named push and pop as a wrapper of our queue methods. Now if you understood all this then let's go to code and implement our stack and queue and also look at all their methods. So here we have made our demo.java project again. So first of all let's implement our queue. So let's say I say ArrayDQ of type integer. So I make it an integer type. And let's say I call it normal queue equal to new ArrayDQ.

  114. Speaker 2

    So first of all, I.

  115. Speaker 1

    Will import a star in the ArrayDQ version of my queue. Now here I could have written queue because queue is our main interface. So queue can point ArrayDQ. But the problem is that queue is our upper interface. You know that first of all we have our queue interface and below that we have our Dequeue interface. dq interface and below that comes our array dq interface. Something like this, right? If we write q here, here, right? Then you know what will happen? Inside our array dq object will be made, inside our heap memory. But who will be doing the point? Q named reference variable. And in q named reference variable, we can call the same method which is inside the q interface.

  116. Speaker 2

    Okay?

  117. Speaker 1

    We will slowly see which methods are inside it. But you can understand that inside q, there will be the same methods which are q specific.

  118. Speaker 2

    Means single q specific.

  119. Speaker 1

    But there will be all those methods in our arrayDq that will help us to implement our double ended queue. So now we use queue as a single ended queue. Which is our normal queue. We are not using it as a double ended queue, that's why I can write queue here. If I want to push the element in queue, then I have two methods. One method is queue.add. So I can write here, let's say I pushed 1. And another method is queue.offer. Q.offer. So I have offered 2. So in this way, I have added 2 elements. Once 1, once 2. I can use any method from these two. Now what is the difference between these two? The difference is that our add method will throw an exception if it fails. And this offer will never throw an exception. If it fails, it will return a false. Now what does it mean to fail? It means that you couldn't add it for any reason. Let's say your memory is full or whatever happened, then it will throw you an exception that it won't be able to add. But what will the offer do? It will always return you a false if it won't be able to add. So this is a safer version and this is a little unsafe version. Now if you add an element to the queue, then how many merge elements can you add? Now after that, let's say I add one more element.

  120. Speaker 2

    First Q dot I will add 3.

  121. Speaker 1

    After offering Now you know I have 1, 2, 3 in my queue And I want to implement single ended queue So if I want to print the first element Which is the top element, basically the front element So now my queue will look like this So first came 1, then came 2, then came 3 Now if I take it out, then what will be my first element? It will be 1 I make it upside down, 3, 2, 1, like this. So first comes 1 in the line, then 2, then 3. Now when it comes out, 1 comes out first. But I don't want to remove 1, I just want to see who is at the top. So these were all the methods of addition. We can also call this NQ method.

  122. Speaker 2

    So basically I.

  123. Speaker 1

    Have inserted in some queue. Now let's see the method of accessing the front. I just want to see what's in the front, I don't want to remove it. So I have a method, q. peak, something like this. So Q.peak means I just have to check which element is in front of me. It will return me 1. So if I run it and show you, then see I got.

  124. Speaker 2

    1, right?

  125. Speaker 1

    Now instead of this Q.peak, I have one more method, exactly the same. I can do Q.element call, okay? This also works exactly the same. Okay, let's run it once and see. See, I got one. So basically, I have two ways to do everything. Again, the difference between these two is that this element can throw an exception. And if I don't have an element in the front, it will return null. But if it doesn't throw an exception, then return null. So again, this is safe. Our peak is safe. But the element is unsafe. Now, when can this null return? Imagine if your queue is empty. Let's say I didn't do any insertion and deletion. And if I now do q.peak on it, My queue is empty, I just made a queue and didn't do any insertion. If I do q.peak on it, then it will return me null. See, null came.

  126. Speaker 2

    Is it safe or not?

  127. Speaker 1

    But if I use my q.element method by mistake on it, then it will throw me an exception. So if you see, no such element exception. So sometimes if you don't want an exception, in that case you can use this safe alternative.

  128. Speaker 2

    Right, this null one.

  129. Speaker 1

    But if you want an exception, you can use this alternative. We have accessed it. Apart from that, we can remove the element from the front. So again, I have two methods to remove the element. One is q.remove. I can do this too. This will remove the element. And the other is q.pull.

  130. Speaker 2

    I can do this too.

  131. Speaker 1

    This will remove the element. Again, this is our safer alternative. If the element is not removed, it will return null. return null return null but this remove is unsafe because it throws exception throw exception so these are all the methods we have for normal queue now if I divide all the methods of queue into groups then I have these methods to insert, remove or inspect in queue these upper add, remove and element are unsafe methods and offer, poll and peek are safe methods But let's say I want to use my queue as a double ended queue because I have an interface called DQ below the queue and it will give me some methods which are for double ended queues. In that case, nothing will happen. All your methods will be divided into two parts. To add an element in the front and to add in the rear. Same front remove and remove from the rear and peak in the front and.

  132. Speaker 2

    Peak in the rear.

  133. Speaker 1

    So if I talk about a DQ, then the alternatives of insert will be like this that I will write add first and add last. So I can add it in front or in last. In the same way, I will have offer first and offer last. So I will have two alternatives of all. Now I can do the same in remove as well. Remove first, remove last. And pole first, pole last. And in inspect, I will change the name a bit. I will write element first and element last instead. Get first and get last. But the peak will be the same. Peak first and peak last. Here I have written the alternatives of all three. Every method is divided into two parts. Add method is add first add last. Remove method is remove first remove last. And element is get first get last. And I have done the same for the three below. Now again you know that all the methods above me are unsafe. Because they throw exceptions. And the methods below are safe alternatives. Interestingly, you know that this DQ interface is implemented by ArrayDQ as well as by LinkList. All these methods are present in both ArrayDQ and LinkList. You can call all these methods in LinkList or ArrayDQ. Now suppose I want to implement the stack. To implement the stack, I can use these methods. Do you know why the stack is doubly ended? I will insert it and remove it from here. So basically, I can use the first alternative of deque. I will do offer first, poll first, and peak first. I will not use this offer last, poll last, and peak last. So my stack implementation is done. So basically, I can do here, Offer first is called to add an element. So I can add an element. I can also do a poll first to remove an element. And if I just want to see, I can do.

  134. Speaker 2

    A peek first.

  135. Speaker 1

    These are all safe alternatives. If I want unsafe alternatives, then I can obviously add first. And here I can remove first. And I can get first. So if I want I can use these three methods So many people have a habit of naming convention of push and pop So Java has given you three new methods One is your push Which will take an element One is your pop And one is your normal peak So when you will use push It's as equivalent to using offer first So again this is also safe If you will use pop then it's as equivalent to using poll first And if you use peak, then it's as equivalent of using peak first. Something like this. So you can either say this or that. It's better to use push, pop or peak because naming conventionally looks good on stack. And you remember that you are using a stack from inside. So Java has given you this alternative. Now here comes a very interesting question that our Q interface will be using collection interface from above, right? And we have a lot of methods in that collection interface. There is also a method of add in it, right? So what will that add do? That add will do exactly what it does. It will insert its elements in the last. And if you insert the last element in the queue, then you are inserting it in a way in the rear.

  136. Speaker 2

    No problem.

  137. Speaker 1

    Same we have a lot of other methods in the collection interface, like remove a particular element. So I have removed or object O. So I can basically say in my queue, remove 5. So will I remove 5 from the middle of my queue? Suppose my queue is filled with 4, 5, 6, 7. And this is my front. And this is my rear. So generally, what do I prefer in my queue? Insertion and deletion should be from my front or rear. Not from the middle. But this method, remove object O, can remove an element from anywhere. The answer is, it can do it. So Java says here, that the rules of a stack Ki usko LIFO hona padega Aur ye jo Q ka ek rule hai.

  138. Speaker 2

    Ki usse FIFO hona padega.

  139. Speaker 1

    These rules are behavioral contracts. Basically, as a developer, you have to make sure that you don't use any method on the stack that breaks the property of the stack. And same goes for you. Don't use any method on Q that breaks the property of Q. Now, if you want to implement a stack, Using ArrayDQ.

  140. Speaker 2

    Okay. So you know. Which 3 operations you can do in that. Push, Pop and Peek.

  141. Speaker 1

    Right.

  142. Speaker 2

    Now what is push? Push is nothing. Rather. Offer first is the alternative. Offer first. Right.

  143. Speaker 1

    Now what happens. I call offer last on it. Offer last. So will I not insert the last element? The answer is yes. Although my stack should look like a new element will enter from the top. But I can enter an element from the back as well. Using my offer last, no one is stopping me. But Java says this is a behavioral contract. As a developer, you have to make sure that if you are calling something a stack, then don't call such methods in it that break the property of its stack. Same goes for, if you are using queue, then don't break the property of queue. Meaning, if there is any method in my collection interface, which will break the property of queue or stack, like remove method, which removes the element from the middle, let's say remove 5. My job will be to not call this method in my stack or queue. So, Java doesn't make sure. Java has given all the methods because it wants a good inheritance and it wants to use polymorphism in the hierarchy of its collection. So, it says that you can use a queue as a single queue, double ended queue or as a stack. This is your contract. Java has given a method to use everything. You can break that contract if you want or you can make that contract. I hope you have understood all this. Now, let's move on to our third type of queue. So, if we look at the interface of our queue again, then this is our queue interface. And below this, you know, this is our DQ interface. And two classes used to come out of DQ. One used to come out of our array DQ and one used to come out of our link list. Because two classes used to implement it. Now there is another hierarchy here. A special queue comes out of this queue which we call priority queue. Priority queue is our class and not an interface. It directly implements our queue interface. Now let's understand what is priority queue and it is very interesting. Priority queue is a special kind of queue in which elements will not follow the property of PFO but will follow a special property. So let's say I made a queue, this is my priority queue, so I call it PQ and there are some elements in it, let's say 5, 10, 1, 7, 3, something like this. Now when I will do in this queue, let's say its name is PQ, so I will do PQ dot poll. Now when I do q.poll, you know the element is removed from somewhere. And when I have done single poll, you know it will be removed from front. So if I have this front, then it should be removed from front. But for priority queue, neither there is any front nor there is any rear. And here you can consider them as indexes.

  144. Speaker 2

    Here elements are entered.

  145. Speaker 1

    Now priority queue says that whenever you do pq.poll, you know which element will be removed.

  146. Speaker 2

    That element will be removed.

  147. Speaker 1

    Whose highest priority will be Now how do we decide the priority is highest or lowest? This is decided by their natural ordering. What is natural ordering? That which element is small and which is big. If you have made a normal priority queue in which you have not passed anything in the constructor, you have passed an empty constructor, which we will see now, then the priority is the largest of the smallest elements. So which is the smallest element in this?

  148. Speaker 2

    The answer is 1.

  149. Speaker 1

    So whenever you do pq.poll, you will get the answer 1. If you do pq.poll again, 1 has been removed from here because you did a poll. Now if you do pq.poll again, then 3 is greater than 1, this time you will get 3. And if you do pq.poll again, then this time you will get 7. Something like this. This is our priority queue. If you want, you can reverse its priority. That the highest priority is the biggest one. When you do pq.poll again, then first you will get 10, then 7, then 5, then 3, then 1.

  150. Speaker 2

    Something like this.

  151. Speaker 1

    Now priority queue is used a lot if you want to do scheduling. So what happens in scheduling? Let's say someone's priority is more, someone's priority is less, priority is less. So this is very useful in job scheduling questions. That's why Java has made a special class for it. Now if you see, there is no priority queue, right? Because there is no FIFO, no RARE, no FRONT in it. Any element can be removed from the middle, but its priority should be more. And the priority will either be of the smallest element or the largest element, depending upon how you have made it. By giving an empty constructor or by passing something in the constructor. We will see what to pass, what is its syntax. But I hope you must have got a rough idea of what a priority queue is. Now, let's see how the internal implementation of Priority Queue is made. Priority Queue implements a new data structure from inside which we call as Heap Data Structure. Now, this heap is different from the heap memory.

  152. Speaker 2

    Its name is Heap.

  153. Speaker 1

    If you don't have any idea about Heap Data Structure, we will understand it. Or else, you can read it in-depth. heap data structure and specifically here we are talking about our binary heap binary heap, so binary heap means two things that you imagine a complete binary tree which we have implemented using stack okay what does it mean? So let's see what happens. First of all, we have to understand what is a complete binary tree. From binary tree, we can understand that every node should have two children, maximum two children. It can be one, it can be two, but not more than two. It can never be like this because it is a binary tree. Apart from that, the second rule of the complete binary tree is this. So if I want to insert new elements in it, then I can insert them level by level and left to right at every level. So see, did my maximum nodes come at this level? Yes, only one could have come, one came, two could have come, both came. Now let's say I am.

  154. Speaker 2

    Inserting a new node.

  155. Speaker 1

    So I will insert it first in the left, then in the right, then again here in the left and then in the right.

  156. Speaker 2

    So in the same way, left right, left right, left right, left right. Something like this.

  157. Speaker 1

    So basically, I can't go to any new level until my old level is fully filled. And when I come to a new level, I'll start inserting from here.

  158. Speaker 2

    First left, then right, then left, then right.

  159. Speaker 1

    Something like this. I can't go in the middle and insert. So this is the rule of complete binary tree. So I hope you understood this rule. Now we'll use this. to implement our heap data structure. Now, what is a heap data structure? Imagine a data structure in which the root node is the smallest. Let's say it has two children, one is 20 and the other is 30.

  160. Speaker 2

    Both of them will be bigger than this.

  161. Speaker 1

    Now, let's say there are two children of 20, one is 40 and the other is 50. Both of them will be bigger than this. So, basically, every parent node should be smaller than both the children. Like 20 is smaller than 40 and 50, 10 is smaller than 20 and 30, something like this. So imagine that I have created a binary tree, which is a complete binary tree because it is filled level by level. Now if I want, I can fill the next one here and if I don't want, I can leave it here. But yes, I can't go anywhere and insert it directly here. Now if I have to do the next insertion, then first I will do it here on the left, then I will do it here on the right.

  162. Speaker 2

    Now look, this is a conceptual overview of it.

  163. Speaker 1

    I will not literally store it like I used to make a class node to implement a binary tree and I wrote data here and wrote two nodes, one node pointer left and one node right I used to store BST like this but I will not store it like this, this is just its conceptual overview To store this, I will use array.

  164. Speaker 2

    Why array? You will understand now.

  165. Speaker 1

    How many indexes are there in this?

  166. Speaker 2

    How many things do I have to store? 1, 2, 3, 4, 5.

  167. Speaker 1

    I have to store 5 things, so I will make 5 indexes. So let's say 1, 2, 3, 4, 5. So let's do their numbering. 0, 1, 2, 3, 4. Now see, what is the rule to store this tree in this array? Because my tree is a complete binary tree. What did I tell you about a complete binary tree? That it is completely filled at every level. And until a level is filled completely, I don't start the next level. And the second level is always left to right filled. Right? That means if I have to put the next element, then I will put it here, then here, then here, then here, something like that. So now it's very simple to fill this area. I just have to go level by level and left to right like this. So I'll put 10 here, then 20, then 30. Right? So I'll go from here to here. Then I'll put 40 and 50. So 40 and 50. Something like this. It's so simple. Right?

  168. Speaker 2

    If I look at it carefully, then a formula will be formed here. Okay?

  169. Speaker 1

    If I put all this indexing here on this tree, then the indexing will be like this. Here will be your 0, this 1, this 2, this 3 and this 4. If you look at this tree carefully, any parent, if any index is present on i, then you will get its left child on 2i plus 1 and you will get its right child on 2i plus 2, where i is an index. Now look carefully, this is present on 10, 0, so I will get its left child on 2i plus 1. What is 2 into 0 plus 1? 1 on 1. And its right child will be 2i plus 2. What is 2 into 0 plus 2?

  170. Speaker 2

    It is 2. Now, you can take any index. Take 1.

  171. Speaker 1

    You got 1's left on 3 and 4. Now, put 1 in the formula. So, 2 into 1 is 2 plus 1 is 3. So, 3 will come here and 4 will come here. So, this formula is working. So, if this formula is working, then the same formula will work in this array. Because indexing is the same. Now, many people have a wrong perception that we will always get a sorted array if we implement a heap. But the answer is absolutely wrong. Imagine if it wasn't 20 here, it would be 30 here. Let's say here it would be 30 and here it would be 20 Something like this So even now this is a heap See our 10 is smaller than 30 and 20 30 is smaller than 40 and 50 But in that case here instead of 20 comes 30 and here comes 20 So see now our array is not sorted 10, 30, 20, 40, 50 is coming So heap doesn't promise you that you will.

  172. Speaker 2

    Get array sorted.

  173. Speaker 1

    Heap just promises you that whenever you ask for an element, I will give you the smallest element. After that, when you ask for a new element again, I will give you a bigger element, something like this.

  174. Speaker 2

    Now we will see how.

  175. Speaker 1

    If you have got a little idea of this, then let me tell you. Heap data structure is also of two types. This one we have made, we call it min heap. Because what happens in it, the minimum is at the root and after that its two children are bigger than it. Same here, the parent is the minimum from both the child, something like this. And one is the opposite of this, which we call max heap. Now what will happen in this?

  176. Speaker 2

    Absolutely opposite. In that, the parent will be the largest from both the child, right?

  177. Speaker 1

    By default, when we implement priority queue, then priority queue uses the min heap from inside. If we want, we can make it use max heap also. It will change a little in the constructor. We will see that when we declare. it.

  178. Speaker 2

    But now let's see further. Okay.

  179. Speaker 1

    So, we understood that the priority queue in Java uses the heap data structure from inside.

  180. Speaker 2

    Now, whether it uses min.

  181. Speaker 1

    Heap normally or max heap if we want, but it will use heap data structure from inside. Now, if heap data structure is responsible for implementing priority queue, so, as we took an example above that we have a priority queue, it has some elements 10, 30, 20, 40, and when I will do, let's say the name of this priority queue is PQ, and I will do PQ dot pole, then.

  182. Speaker 2

    What will I get?

  183. Speaker 1

    The element whose highest priority will be, and whose highest priority is the smallest element, then I should get 10, right?

  184. Speaker 2

    We saw this above, okay?

  185. Speaker 1

    So now you must be getting a little idea of how it will be implemented from inside. We just saw that we conceptually represent a heap like this and actually like this, right? We don't need to make a node here, we can represent it directly in an array because it is a complete binary tree, okay?

  186. Speaker 2

    And it follows this formula.

  187. Speaker 1

    Now if I have made this array like this and if someone tells me pq.pol Then I have to get the smallest element and I know where I will get the smallest element.

  188. Speaker 2

    I will get it on the root. So I can get it.

  189. Speaker 1

    But if I remove this 10 from here. So where will the child of this 30-20 go? Because they are all dependent on each other. 10 is the parent of 30-20. So this bonding should not break. For that we follow an algorithm. And you will also enjoy understanding about it. We call that algorithm as heapify algorithm. If you have no idea about heap data structure then you can watch a video on it. But you will get an overview from this. You won't be able to solve heap questions but you will understand about heap.

  190. Speaker 2

    Now imagine I will take this heap from here.

  191. Speaker 1

    I made an array and you know that I can make a tree just for conceptual representation. So 10, 30, 20 something like this and here it will be 40 and 50 something like this. So now imagine this is your priority queue because we know that priority queue is made like this from inside. It uses heap data structure and conceptually this is also your priority queue. This is just our conceptual representation, it will be represented like this from inside. Now, if I have already put so many elements in this priority queue, 10, 30, 20, 40, 50, in some way, now we will see how they are put. Let's say now I want to put one more element, so I write something like this, I write here, let's say, let's write above, I write pq.offer, again I can call all the methods of my queue, offer, poll, peak, so let's say I put here, let's say, 5. So pq.offer 5. So how can I put it? First of all, I will tell you how I can put it conceptually. And then I will see how we can put it in array.

  192. Speaker 2

    Okay.

  193. Speaker 1

    You just have to remember that rule. That you know if any thing's i is parent.

  194. Speaker 2

    Okay.

  195. Speaker 1

    So its left node will be on 2i plus 1. And right node will be on 2i plus 2. So this is left and this is right. If you remember this formula, your whole work will be done. Now see, the rule says that as I have to insert 5, I will insert 5 in the last. First I will show you the overview of the tree. Now see what is the last, you know this is a complete binary tree which is filled level by level and left to right. So now the next element that can be filled here is this space.

  196. Speaker 2

    Left space at this level. Here I will insert 5 first. Okay.

  197. Speaker 1

    And after that, as soon as I insert 5 here, my story is over. Absolutely not. Because look, 5 is smaller than its parent. So, I have to swap 5 somewhere. Here, my work comes up. Heapify algorithm.

  198. Speaker 2

    Okay. And specifically, I am going to use up heapify. Up heapify.

  199. Speaker 1

    This algorithm is very simple. It says that I will compare 5 with its parent. So here I compared 5 with 20 and saw that 20 is small or 5 is small. The answer is 5 is small. So I will swap these two. Now here comes our 5 and here comes our 20. Something like this. Now, the story is not over yet. 5 is here. Now, I will compare it with its parent again. Is 10 small or 5 small? The answer is 5 is small. That means, 5 has more priority than 10. 5 should go up and 10 should go down. So, I will swap these two.

  200. Speaker 2

    Okay.

  201. Speaker 1

    So, basically, my 5 should come here. And my 10 should come here. Something like this. Now, 5 has come to the top. There is nothing at the top. That means, my algorithm worked properly. Now, I don't need to do anything else. Now see my algorithm is absolutely correct. 5 as a parent is smaller than both the child. 30 is smaller than both the child and 10 is smaller than the child. Now I have shown this conceptually. How will this work in array? It is very simple. First of all, what did I say? I will insert a new element in the last. And you know what last means, on the last index. Now you will say that the array is full. No problem, you understand. We have resized it. It can increase as much as it wants.

  202. Speaker 2

    It is saying dynamic. So, we have.

  203. Speaker 1

    Inserted 5 in the last. This is the fifth index. Now, I have to swap it with its parent. Now, what is the parent of 5? In this case, how will I get the parent of the fifth element? Here, I can use this formula's technique. I know that every i has two children, 2i plus 1.

  204. Speaker 2

    And 2i plus 2. Okay.

  205. Speaker 1

    Now if I want to go from 2i plus 1 to i. Or if I want to go from 2i plus 2 to i. Then I will have to make a formula of my own. And I will have to make a formula like this. So whether it is for left node or right node.

  206. Speaker 2

    For both.

  207. Speaker 1

    For both I should get i. Because I don't know which node I will add. Left node or right node. Left right is our conceptual view. After all we will add in the array. So I need a formula like this. That I should go from left to top or right to top. I should always get i. So see that is very simple. Whatever index I am standing on, let's say I call this index j and this index k. So what I can do, whatever the index is, j or k, I just have to do j minus 1 by 2 and its linear is ceiling value. Same I can do here, k minus 1 by 2 and its linear is ceiling value. Now think, if I write this carefully, I will remove this now.

  208. Speaker 2

    You imagine that.

  209. Speaker 1

    Let's say I make an arbitrary node of 100, something like this. If I write the indexing of 2 childs, 0, 1, 2, 3, 4, then there will be 2 childs, 5 and 6. If I write my formula, then the parent of 5 will be 5-1 by 2, which is 4 by 2, which is 2. And in the same case, if I talk about the right node, then my index will be 6. So I will write 6-1 by 2. I will write it with a pencil. 6-1 by 2, which is 5 by 2. And 5 by 2 is 2.5. I can take the floor value of this, which is equal to 2. So, I got 2 here too. So, basically what I will do is, whatever my index is, I will do minus 1 and divide it by 2. And whatever the answer is, I will take the floor value of it. So, in this way, I will get a parent for both the children with the same formula. So, now think carefully. Here, I will remove this. This was my arbitrary node. Now if I want the index of 5, I will do 5-1x2 which is 4x2 which is equal to 2. So I will get its parent on the second index. Now I can compare 20 with 5.

  210. Speaker 2

    The small one is 5 so 5 should come up. So I will swap them.

  211. Speaker 1

    So here comes my 20 and here comes my 5. Something like this. Now again I will compare this 5 with its parent. Now what is the index of 5? 2. So what will be the parent of 2? 2-1x2 which is 1x2 which is equal to 0.5. And if I take its ceiling, then the answer is 0. Absolutely correct. What is the index of 5 here? It is 0 only. Here the index of 5 was 0. If I compare 5 with 10, who is small? 5 is small. Again there will be swapping between them. Here will be our 10. And here will be our 5 Something like this And my array is completely correct Okay, very good I hope you understood. Now let's say I did another insertion in the same way. I wrote pq.offer. Let's say I want to insert 8. So what will happen? You know, again, first of all, if I show it in conceptual, then I will add 8. First of all, in the last, something like this. And I will befi it now. Again, I will compare 8 with my parent. If 10 is small, then I will swap it. You know what will happen after swapping? My 10 will come here and my 8 will come here. Now I will compare 8 with its parent 5. Now 8 is bigger than 5. Obviously 5 is smaller than 5 so 5 will be higher. And my heapify is done. Now every parent will be smaller than both their children. Now how can I do this in array? Again I will add a new node whose answer will be I will put the value of 10 and it will be 6. Now again I will compare 6 with its parent and I will get 6-1 by 2 which is equal to 2.5 and its floor value is 2. So I compared it with 2. Now I will compare the new value with 10. Let's write it as 8.

  212. Speaker 2

    Okay.

  213. Speaker 1

    So I'll compare this 8 with this 10. The answers will be swapped. So here will be 10 and here will be 8. Something like this. Then again I'll compare 8 with its parent. The answer is that they won't be swapped. And finally my priority.

  214. Speaker 2

    Queue will be this. So this algorithm that I was using, I was calling it upheapify.

  215. Speaker 1

    Okay. So upheapify is enough. Now assume if someone does pq.poll. So I have to give it the element of highest priority. And whose is the highest priority? Of the smallest element. Means I have to give the element from the top.

  216. Speaker 2

    But if I take out the element from the top like this.

  217. Speaker 1

    So what will happen to the relationship between them? It is related to 5. I can't take out 5 directly from here. It is a parent-child relationship. So for this, I'll use another algorithm. This one's down heapify version. This was up heapify. I'll do down heapify here.

  218. Speaker 2

    Okay, what does this say?

  219. Speaker 1

    Again, first of all, I'll explain this conceptually and then in the array. So down heapify says, first I'll make this with seek. Yes, I made it in a better way. Now, what will the down heapify or poll function do? First of all, it wants me to remove this 5. Now, I can't remove 5 directly. The best way is to swap this 5 with the last element. So, the last element is 10. So what I will do is, I have replaced 5 with 10. Now as soon as I have brought 5 in the last, I can easily remove it because there is no relationship above it. So I can cut this bond if I want and return 5. Now I have to bring this 10 to the right place. I have to do down heapify. Let's say I removed 5 from here. I wrote 5 somewhere outside and returned it. And now my heap is left like this. Now how can I do down heapify? If I have i, then my left will be 2i plus 1 and right will be 2i plus 2. So this way I will get its child. So first I will compare 10 with its left child, 30. And who is smaller between 10 and 30?

  220. Speaker 2

    10 is smaller, right?

  221. Speaker 1

    So this is at its right place. No problem here. Then I will compare 10 with its right child. And who is smaller between 10 and 8?

  222. Speaker 2

    8 is smaller.

  223. Speaker 1

    That means I have to do down heapify. Here comes 10 and here comes 8. Now I will compare 10 with its left and right shell. Now I can do the same in array. First of all I will replace 5 with 10. here comes 5 and I will return 5 which means nothing will happen I will assume that my array is over here this whole index is out of my array and I have returned 5 in the case of array you know what we do in dynamic array we assume that we have this much array and now what I have to do is down heap 5 I will compare 10 with its child you know where I will get 10's child at 2i plus 1 and 2i plus 2 so again when I compared it at 30 it will not be replaced then I compared it at 8 and it will be swapped so here comes my 8 And here will be my 10. Now again 10 will not go below this. And my array is absolutely correct. Right. If I tell you this broadly. So I have only two algorithms of heapify. One is down heapify. And one is up heapify. And between these two there is a call that depending upon what I want to do. I want to add an element.

  224. Speaker 2

    Offer.

  225. Speaker 1

    Or I want to remove the element which is poll. If I am removing the element, I replace the element from the bottom and call downHepify.

  226. Speaker 2

    DownHepify is called for polling. Now, when does it call?

  227. Speaker 1

    When I am offering a new element. That's why I take it to the top. Let's say I give an element of 10. Now, if we do both of our functions, one was offer in which I used to put a new element let's say 10 and one was a pole in which I used to remove the element the highest priority element if I remove the time complexity of both of them then you will understand that the time complexity of both of them will be the order of log n it is fixed right why it will be you can think for yourself because see if I put a new element then where do I put it at the end And in the worst case, it will come to the top while comparing with its parent. Same if I remove an element, how do I remove it? First, I replace the top with my lowest element and bring the top down and in the worst case, it will come to its tail node. So in both the cases, it will run log n times. So the order of log n is the complexity of both. Whether I talk about upheapify or offer or poll. I hope you have understood the priority that how it uses the heap data structure from inside. And again it uses the min heap by default which I will show you after making it.

  228. Speaker 2

    It also has a max heap.

  229. Speaker 1

    Version in which I will change the constructor a little bit and your heap will be opposite. In that you will get maximum element on top and when you will pop then you will get the biggest element first. If you understood its conceptual view and its exact view, how it is stored in memory and how this heapify algorithm performs, then you can also write its code by yourself. Again, this is mostly a part of DSA. Java only takes the use of heap to implement priority queue. So to use it, I have made a demo2. java empty file and have imported priority queue. So first of all, I make an object of priority queue. Let's say priority queue of integer. Now I will add some elements to this pq. And again I have the same method to add elements. So let's say I added 10 and I will add the same elements that I talked about.

  230. Speaker 2

    Let's add some more elements.

  231. Speaker 1

    30 add, 20 add, 40 add, and let's say 50 add. Okay? Now, what I said was, that if by default, the priority queue that I make, I make it with an empty constructor, then I'm implementing a type of min heap, right? In which parent is the smallest. So, if now in this case, pq dot, If I do poll, I should get the smallest element. Because the highest priority is the smallest. So I should get 10. And after that, 10 should be removed. That means if I do poll again, I will get 20, 30, 40, and 50. So let's see if I am getting those elements. The answer is that I am getting 10, 20, 30, 40, 50. Now I can make an alternative max heap of this. Let's say I make a new heap. I will give it a name pq2 I will have to do a small change in the constructor I will pass a lambda expression a, b gives b-a.

  232. Speaker 2

    Something like this. We haven't understood what a lambda expression is yet.

  233. Speaker 1

    So, you can consider it as a black box that we have to pass this. In the coming video, we will understand the lambda expression well. But for now, you can assume that you have passed a lambda expression here. You have to pass this syntax. And because of this, we can make a max heap here.

  234. Speaker 2

    Now, you can.

  235. Speaker 1

    Understand that I have added the same elements in this max heap. So, I will again call this pq2. So, this is my pq2. This too, this too, this too and this too. and same I will pull all the elements like this and I will make it comment now what I am expecting in maxheap which will be my top priority element which is the biggest element so first I will get 50 then 40 then 30 then 20 and then 10 so let's see this once.

  236. Speaker 2

    And 50, 40, 30, 20, 10. Okay?

  237. Speaker 1

    And you know, from inside, these heap algorithms will be using its max heapify and mini heapify function depending upon whether I'm offering or I'm polling.

  238. Speaker 2

    Something like this.

  239. Speaker 1

    If you understood all this, then let's see what are the methods in the priority queue. Now see, what is the priority queue? It is a kind of queue. So obviously, there will be all those methods which are in a normal queue. So if you want to add an element, then we will have the same method. We can do add method call, add or offer. Right, something like this. If you want to remove, then you know that you have two methods. One is normal remove and the other is pole. Right? And if you just want to inspect which element is on top, then you have an element version and a peak version. Right? And again, we know that the offer, pole and peak are your safer alternatives.

  240. Speaker 2

    Safer alternatives. Why?

  241. Speaker 1

    Because they don't throw you an exception. But the add, remove and element above can throw you an exception. So, it can be unsafe to use it.

  242. Speaker 2

    Okay. Obviously, all the methods are the same as you expect in a normal queue. Okay.

  243. Speaker 1

    I hope you understood everything well. Why, what happens, how it works and what is the priority. That's all in this video. See you in the next video. Till then, thank you so much.