Binary Trees in Python: Level-order Traversal

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 137

  • @missngbrownie
    @missngbrownie Рік тому +1

    Your technical communication skills are exceptional. Truly appreciate your videos!

    • @LucidProgramming
      @LucidProgramming  Рік тому +1

      Very kind of you to say, thank you for the kind words!

  • @TheArun11143
    @TheArun11143 4 роки тому +3

    Using list instead of Queue ;)
    def levelorder(root):
    Queue=[ ]
    if root is None:
    return
    else:
    Queue.append(root)

    while(Queue!=[ ]):
    curr=Queue[0]
    if(curr.left):
    Queue.append(curr.left)
    if(curr.right):
    Queue.append(curr.right)
    print(curr.data)
    Queue.pop(0)
    PS: That was a great explanation.. thank you!!

  • @rajeyshkumar
    @rajeyshkumar 5 років тому +16

    Appreciate your coding fluency and teaching skills

    • @LucidProgramming
      @LucidProgramming  5 років тому +2

      Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
      I really appreciate the support, and if you want to support the channel, I do have a PayPal link
      paypal.me/VincentRusso1
      for donations that go directly to the creation of content on this channel.
      I hope that the content I provide there will enhance the videos on my UA-cam page.
      bit.ly/lp_email

  • @samarasimhachalla3509
    @samarasimhachalla3509 3 роки тому +1

    If you are watching his content and learning from it, you better subscribe !! Do it right now !! This guy is a legend

  • @icycold007
    @icycold007 3 роки тому +1

    one of the best teacher on youtube no-cap

    • @LucidProgramming
      @LucidProgramming  3 роки тому +1

      Sincerely appreciated. Thank you for your kind words!

    • @icycold007
      @icycold007 3 роки тому

      @@LucidProgramming i would highly appreciate it if you could do a video on hashmaps , i am following your course on educative.io too

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      @@icycold007 My time these days has been sincerely limited, but I will try at some point in the future to do so!

  • @Magmatic91
    @Magmatic91 3 роки тому +1

    Hi Mr.Lucid, your content is really one of a kind. This channel definitely deserves more views and attention. Your way of explaining is great and you obviously master your stuff.

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @0161rt
    @0161rt 5 років тому +13

    As ppl's comments on the Queue FIFO(first in first out), I finally under stand your logic of using .insert(0, item) for enqueue and .pop(-1) for the dequeue. But I still prefer the sequence of .insert(len(self.items),item) for enqueue and .pop(0) for dequeue. It seems more straight forward following the python index. Albeit that this is probably not as clean on the coding script.

    • @LucidProgramming
      @LucidProgramming  5 років тому +3

      Definitely understand the point you're making and I can see why your approach is clearer, especially from the standpoint of the implementation of the Queue data structure. Thanks for sharing and for watching! :)

    • @tedarcher9120
      @tedarcher9120 3 роки тому +2

      Never use List as a queue. It takes 0(1) to put an element in the back, but 0(N) to pop(0) or in reverse to put an element in front in takes 0(N). You slow down your program by thousands of times on a big dataset, use collections deque, it's included in all python

    • @tedarcher9120
      @tedarcher9120 3 роки тому

      Using list as stack is ok though, if you put items and pop in the back of the list

  • @derrick7968
    @derrick7968 3 роки тому +1

    Thanks sir for explanation.
    I changed the function just using lists instead of Queue class, I use append to enqueue and pop(0) to dequeue.
    Here is the function.
    #Tree
    tree = BinaryTree(1)
    # tree.root.left = Node(2)
    # tree.root.right = Node(3)
    # tree.root.left.left = Node(4)
    # tree.root.left.right = Node(5)
    # tree.root.right.left = Node(6)
    # tree.root.right.right = Node(7)
    # tree.root.right.right.right = Node(8)
    def level_search(self):
    queue = []
    final = []
    queue.append(self.root)
    while len(queue) > 0:
    final.append(queue[0].value)
    elem = queue.pop(0)
    if elem.left:
    queue.append(elem.left)
    if elem.right:
    queue.append(elem.right)
    return final
    #final >>> [1, 2, 3, 4, 5, 6, 7, 8]

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      Cool, that's definitely one way to do it. Thanks for sharing!

    • @derrick7968
      @derrick7968 3 роки тому

      @@LucidProgramming could you do zigzag traversal please. Someone suggested to me to use two stacks.

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      @@derrick7968 Sure you could. Go for it.

    • @derrick7968
      @derrick7968 3 роки тому

      Oh, ok let me try, where can I reach out to you. LinkedIn or email.

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      @@derrick7968 Comments is fine.

  • @akshaykhadka9440
    @akshaykhadka9440 4 роки тому +1

    An effective way to precisely define the Queue class ! Thank you for providing the quality videos .

  • @devanshsolani2593
    @devanshsolani2593 4 роки тому +3

    Your videos are the reason why I feel now coding Is fun and interesting

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @ZSDxGaming
    @ZSDxGaming 4 роки тому +1

    Loved the way you code. Very clear and crisp. Easy to understand. Thanks a lot man

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @jonarmani8654
    @jonarmani8654 4 роки тому +9

    Yo man the level-order algorithm blows my mind.

  • @hamzamusse7853
    @hamzamusse7853 4 роки тому +3

    What a simple, beautiful explanation! THANK YOU!!

  • @bailymartin4305
    @bailymartin4305 3 роки тому +2

    This saved me so much stress. Thank you 🤗

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @percyjackson1662
    @percyjackson1662 Рік тому +1

    Such a great content! Can we have more such videos, especially like a DSA interview questions series?

    • @LucidProgramming
      @LucidProgramming  Рік тому

      I would love to make more, but time for me is a rare commodity these days. Thank you for watching though!

  • @alexandraxu4007
    @alexandraxu4007 3 роки тому +1

    really love your data structure courses, the best that I have found, and I went through a loooot. Thanks!!!

    • @LucidProgramming
      @LucidProgramming  3 роки тому +1

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @yongguangqin3007
    @yongguangqin3007 6 років тому +5

    The queue illustration in the slides was a bit confusing for me in the beginning. I understand now that either way would work, but based on the diagram I thought enqueue inserts an item at index -1 position (end of the python list), whereas dequeue pops the index 0 item. Otherwise very clear explanation, thank you for the great work!

    • @LucidProgramming
      @LucidProgramming  6 років тому

      Hey Yongguang. Indeed, perhaps the diagram in the slides portion could have been a bit more explicit, but it sounds like you got the general gist of it. Thanks for the comment, I definitely try to factor these sorts of things into future videos as I want to attempt to be as clear as possible. Thanks for watching, and happy coding!

    • @rushilkumar9609
      @rushilkumar9609 4 роки тому

      In that case enqueue will append the item and for dequeue u need to pop 0th element everytime

  • @davidrowlands8548
    @davidrowlands8548 2 роки тому

    Yet another excellent video. Thank you.

  • @seungjaeyang1476
    @seungjaeyang1476 4 роки тому

    I don't quite understand the "overriding" part at 11:40. Can someone please explain it to me?? Thanks!

  • @sebastianandrade8500
    @sebastianandrade8500 3 роки тому +1

    Awesome explanation

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @nishantbanjade920
    @nishantbanjade920 4 роки тому +1

    Thank you so much sir

    • @LucidProgramming
      @LucidProgramming  4 роки тому +1

      Thank you very much for your kind words and for watching. Cheers!

  • @playhopGames
    @playhopGames 4 роки тому +1

    Hi , your explanation goes beyond perfect , even i am not a computer scientist student, but your explanation makes me love the data structures and coding :) i want to ask you something, why you stopped making tutorials? and is there any tutorials on graphs?

    • @LucidProgramming
      @LucidProgramming  3 роки тому +1

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @temitopeadeniji1249
    @temitopeadeniji1249 2 роки тому

    Hi LucidProgramming! I just came across your videos, they're very good.
    I have a question about def print_tree. Why did you use tree.root as the function argument e.g. return self.inorder_print(tree.root)? I'm new to this, shouldn't it be self.root instead?

    • @LucidProgramming
      @LucidProgramming  2 роки тому +1

      Hi Temitope. Thank you for the kind words! And yes, you can indeed use `self` in this case for the same result. Cheers!

    • @temitopeadeniji1249
      @temitopeadeniji1249 2 роки тому

      @@LucidProgramming Thanks for the quick response. You're amazing!

  • @dhananjayravi8942
    @dhananjayravi8942 Рік тому +1

    what software are you using for editing the code

    • @LucidProgramming
      @LucidProgramming  Рік тому +1

      It's all mentioned in the description if you'd like more details. Cheers!

  • @sonnychiba5270
    @sonnychiba5270 5 років тому

    Nice video, I think the somewhat confusing part was that the visual drawing of the list showed values/nodes being appended to the end of the list and later dequeued from the front, but the code inserts values/nodes at the beginning and then peeks & pops them from the end. That's the impression I got, but anyways, great video series!

    • @LucidProgramming
      @LucidProgramming  5 років тому

      Thank you for watching, and nice point! It's great to get this type of constructive feedback. Thanks for watching, and for the comment!

  • @nisargbhatt4967
    @nisargbhatt4967 6 місяців тому

    why does the enqeueue method insert an element at first index of the list? isn't a queue FIFO?

  • @alexdarcovich9349
    @alexdarcovich9349 5 років тому +1

    Very well explained! Thank you so much!

    • @LucidProgramming
      @LucidProgramming  5 років тому

      Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support. I hope that the content I provide there will enhance the videos on my UA-cam page.
      bit.ly/lp_email

  • @sagardhampalwar
    @sagardhampalwar 4 роки тому +5

    Here is simple solution with less lines of code:
    hope u like this
    def levelOrder(self):
    traversal = ""
    l= list()
    t = self.root
    l.append(t)
    while(len(l)!=0):
    traversal += (str(l[0].data) + " ")
    if l[0].left:
    l.append(l[0].left)
    if l[0].right:
    l.append(l[0].right)
    l.remove(l[0])
    return traversal

  • @jowadulkader9006
    @jowadulkader9006 6 років тому +1

    sir,hi,you wrote
    if node.left:
    queue.enqueue(node.left)
    if node.right:
    queue.enqueue(node.right)
    isnt the enqueue function inserting nodes at position 0? so isnt there a clash?

    • @jowadulkader9006
      @jowadulkader9006 6 років тому

      ok i get the point.i thought inserting at 0 will remove the previous insertion.anyway thanks

    • @LucidProgramming
      @LucidProgramming  6 років тому

      Hi Jowadul. There shouldn't be any clash, as the enqueue function will automatically place the element in the next spot in the queue. I might be misunderstanding your question as well.

  • @ashishgoyal4958
    @ashishgoyal4958 5 років тому +2

    Please tell why this error this coming:
    while len(queue) > 0:
    TypeError: object of type 'Queue' has no len()

    • @LucidProgramming
      @LucidProgramming  5 років тому

      I'm not seeing that. You sure you have the right code running?

  • @pavassrivastava3323
    @pavassrivastava3323 4 роки тому

    Heyy great tutorial....
    I am not able to understand the concept behind .value in peek function.
    Is it some kind of inbuild function as we didn't define the "value" variable in our queue class.
    In my opinion the function should return self.items[-1]...but its showing error if I'm not using self.items[-1].value..

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      Peek is not an in-built function, we define it ourselves.

  • @eliobteich4133
    @eliobteich4133 4 роки тому

    Why have you used __len__ and size method, couldn't we use only one of them? Isn't it the same?

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      __len__() is just a wrapper for size. Using defining the behavior of __len__ allows us to override the built-in len() function for our object.

  • @fpvcoder9200
    @fpvcoder9200 4 роки тому

    which vim plugin are you using for the intellisense?

    • @LucidProgramming
      @LucidProgramming  4 роки тому +1

      That's all described in the link in the description of the video. Cheers!

  • @KKinHD10
    @KKinHD10 4 роки тому

    Thanks for such amazing tutorials , but I wanna ask - can we apply iteration method for DFS traversals aswell? I always found Recursion confusion so I was wondering can't I use just the iteration method to implement DFS :)

    • @LucidProgramming
      @LucidProgramming  4 роки тому +1

      Sure, generally anything you can do iteratively you can do recursively and vice versa.

    • @KKinHD10
      @KKinHD10 4 роки тому

      @@LucidProgramming Thanks for replying sir but one more thing : Can you make a detailed video on how recursion works in such cases?? I understand basics of it but i am not able to understand your implementation and how the traversal string is updated .
      I request you sir, if you get the time please make a short video explaining how recursion works in such situations where we update a given string via function calls.
      I really need to understand how it works in order to implement it for any use :)

    • @LucidProgramming
      @LucidProgramming  4 роки тому +1

      @@KKinHD10 You can feel free to make suggestions on my Patreon page. This would streamline what you want to see and also help to support my channel.
      www.patreon.com/lucidprogramming

  • @thedevsaddam
    @thedevsaddam 4 роки тому

    I really appreciate your effort. Why there is no video on heap, priority queue

    • @LucidProgramming
      @LucidProgramming  4 роки тому +2

      Thanks! And to answer your question, it's because I do not have the time :P

  • @jojoesekyisagoe
    @jojoesekyisagoe 4 роки тому

    Hi Sir, thank you for the series. I have a question. In the code for the level order traversal, why didnt you use the built in python list for the queue. like below:
    def levelOrderPrint(self):
    ret = []
    track = []
    cur = self.root
    track.insert(0, cur)
    while len(track) != 0:
    cur = track.pop()
    ret.append(cur.data)
    if cur.left != None:
    track.insert(0, cur.left)
    if cur.right != None:
    track.insert(0, cur.right)
    return ret
    Because the built in list can do all of the needed operations. Is there any reason for the queue implementation?

    • @LucidProgramming
      @LucidProgramming  4 роки тому +1

      Sure, it can do all the same things, but the queue object is going to give better runtime if you want to remove from the front / add to the back / etc.

  • @iamcodemaker
    @iamcodemaker 4 роки тому +1

    Thanks for sharing this content!

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @shuaishao2584
    @shuaishao2584 4 роки тому

    a question about line 17, queue.peek() only returns the node, shouldn't we use queue.peek().value?

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      It depends on the intent. For how we use the peek function, no.

  • @ravis7643
    @ravis7643 5 років тому +1

    This is really great :) Just one qn - Could you explain the reason for overwriting length function, when we have a built in method (len) for list data structure

    • @LucidProgramming
      @LucidProgramming  5 років тому +3

      Thanks. And yeah, the reason we overload it is because the built-in length function is not defined for arbitrary data structures like this. It works on strings, lists, etc. but how would Python know how length is defined for this if we don't explicitly tell it how to quantify it?

    • @ravis7643
      @ravis7643 5 років тому +1

      @@LucidProgramming Thanks for clarifying

    • @LucidProgramming
      @LucidProgramming  5 років тому

      @@ravis7643 Sure, hope that made sense!

  • @kimber8324
    @kimber8324 4 роки тому

    I had to watch the video twice to make sure I understood why we use a -1 index for the peek() function. Because of the nature of a queue, when you're inserting into the queue (enqueuing), you can use 0 index to add an element to the front of the queue... but when you're dequeuing an element, you have to "flip" the structure and use -1 as the index since a queue is "first in, first out". Essentially, the front becomes back. Does that make any sense at all? If so, is that the right way of thinking about it?

    • @LucidProgramming
      @LucidProgramming  4 роки тому +1

      Yeah, I think that's a fine way to visualize it. A queue is like the queue in a shop where the first one in is the first one out, while a stack is based on being the first one in is the last one out; like a stack of plates at a buffet or a stack of pancakes.

  • @Captainadityaa
    @Captainadityaa 4 роки тому +1

    class Node(object) see this object was not in linked list class why?

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      That's the way you define a class.

    • @Captainadityaa
      @Captainadityaa 4 роки тому

      @@LucidProgramming but sir, in linket list it was just class Node:

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      @@Captainadityaa Your comment makes no sense to me.

    • @Captainadityaa
      @Captainadityaa 4 роки тому

      @@LucidProgramming leave it. I'll google

    • @kimber8324
      @kimber8324 4 роки тому

      @@LucidProgramming He's referring to the Linked List videos that you created. When creating classes in those videos, you defined the class as:
      'class LinkedList:', whereas in this video you defined the Queue class as: 'class Queue(object):' -- I'm kind of curious about this myself as I'm fairly new to creating classes in python. If the inclusion of (object) necessary in certain cases and not others?

  • @tanishktripathi217
    @tanishktripathi217 4 роки тому

    Hello sir, amazing explanation
    I just had one doubt that why did you return items in sizeof()

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      Can you timestamp? Not sure where you're seeing this.

  • @Ishanmahesh1992
    @Ishanmahesh1992 5 років тому

    Just one doubt. Whether we can use Len function directly instead of modifying it ??

    • @LucidProgramming
      @LucidProgramming  5 років тому

      I don't understand your question. Where and how are you intending to use the "len" function?

  • @Ishanmahesh1992
    @Ishanmahesh1992 5 років тому

    I am slightly confused..pls advice.
    peeking for first element in line doesn't mean we should grab the element with index 0 ?
    But -1 is taken in peek.

    • @LucidProgramming
      @LucidProgramming  5 років тому

      No. We are looking for the element at the top, therefore it's the last element in the list, which is given by the "-1" syntax.

    • @dewanggedia685
      @dewanggedia685 5 років тому

      @@LucidProgramming Thanks for posting programming videos. It has been a great learning. The concept of peek still isn't clear to me. Why don't we do peek at 0th element since 0th element will be first in queue and -1th element will be last element in queue. Also, while inserting aren't we supposed to append to the end of the list? Here, we are always inserting at 0th element so by that logic right element would precede the left element. Please help me to clarify this. Thanks in advance.

    • @LucidProgramming
      @LucidProgramming  5 років тому +2

      @@dewanggedia685 Ah, I see, and you know what, the peek for a queue should definitely take from the front. I suppose I was implementing a stack, really, for this level order traversal. The data structure implementation here does the trick, but I can see where this is misleading. I'll update the code on my github with appropriate comments. Thanks for pointing that out to me, and sorry for the confusion!

    • @rishikeshpuri6101
      @rishikeshpuri6101 5 років тому

      @@LucidProgramming sir, same confusion
      in Queue -1 index is from starting ?
      sir clarify it.

    • @LucidProgramming
      @LucidProgramming  5 років тому

      @@rishikeshpuri6101 -1 index is from the back.

  • @kinjalvora3352
    @kinjalvora3352 2 роки тому

    just used your logic and instead of creating a queue, used an array and came with the following:
    def level_order(self, start, traversal):
    # create a queue
    # append the first root element
    # pop the first element, print it, and check if it has a left and a right node and append it to the stack
    # pop again the first element that has been appended in the stack, print it and then check again
    # 1, 1 has 2 and 3 to append
    # 2 pops, 2 has 4 and 5 to append
    # 3 pops, 3 has 6 and 7
    stack = [start]
    while stack:
    start = stack.pop(0)
    traversal += (str(start.value)+'-')
    # print(start.value, end="-")
    if start.left:
    stack.append(start.left)
    if start.right:
    stack.append(start.right)
    return traversal

  • @funnykid_purab4020
    @funnykid_purab4020 3 роки тому

    can you share the link for graph also

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      For graph?

    • @funnykid_purab4020
      @funnykid_purab4020 3 роки тому

      @@LucidProgramming graph data structure

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      @@funnykid_purab4020 I'm not sure I understand. What makes you think I have a link for this?

    • @funnykid_purab4020
      @funnykid_purab4020 3 роки тому

      @@LucidProgramming am asking have you made any video on graph data structure like tree?

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      @@funnykid_purab4020 Trees, yes. You can find those on my channel under the "Trees" playlist. Graphs, sadly not.

  • @cliffordbamert7728
    @cliffordbamert7728 3 роки тому +1

    thanks

  • @songshub_smitvavliya
    @songshub_smitvavliya 4 роки тому +1

    def levelorder_traversal(self):
    arr = list()
    arr.append(self.root)
    traversal = ""
    while len(arr) > 0:
    if arr[:1][0].left:
    arr.append(arr[:1][0].left)
    if arr[:1][0].right:
    arr.append(arr[:1][0].right)
    traversal += str(arr[:1][0].data + " ")
    arr = arr[1:]
    return traversal

  • @GOSPGABO
    @GOSPGABO 4 роки тому

    What is de difference between Level-OrderTraversal and BSF?

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      Breadth-first traversal and level order are basically the same thing.

  • @prathamgupta7605
    @prathamgupta7605 3 роки тому

    why are you not making any video now?

    • @LucidProgramming
      @LucidProgramming  3 роки тому +1

      Life? Honestly, I'm just quite busy these days. I hope to get back to it though

    • @prathamgupta7605
      @prathamgupta7605 3 роки тому +1

      @@LucidProgramming I just wanted to say I love your videos and your organized way of representing the things. I hope to see you soon.

    • @LucidProgramming
      @LucidProgramming  3 роки тому

      @@prathamgupta7605 Thanks!

  • @Bayo106
    @Bayo106 3 роки тому

    Is this also known as Breadth First Search?

  • @AmitKumar-tl6sx
    @AmitKumar-tl6sx 6 років тому

    thanks for creating and sharing the nice video .
    i am not able to get the logic of peek function in class queue
    you are using -1 index , please explain.
    self.items[-1].value

    • @LucidProgramming
      @LucidProgramming  6 років тому +1

      Hi Amit. No problem, I hope that the video was useful to you! The general gist of the negative indexing is that it goes backward in the list. For instance self.items[-1] is the last item in the self.items list. The element self.items[-2] is the second to last item in the list and so on. Does that make sense?
      Actually, if you're interested in staying up-to-date with the content on my channel, you might want to sign up for the mailing list I'm starting:
      bit.ly/lp_email
      Cheers!

    • @mikedotleb
      @mikedotleb 5 років тому +2

      Each time enqueue is called (inside the if conditions), the start/item argument (whichever node you're on), is placed in the 0 index (first position) of the Queue self.items list.
      The peek method is returning the value of the last index position of the items list: self.items[-1].value
      This works because the enqueue method keeps placing the nodes it checks in sequential order at the beginning of the list (the zero argument in self.items.insert(0, item)).
      Meaning that even if it checks if a left node exists and places it in the first element, it will then go and check if a right node exists and then place THAT element in the 0 position (before the previous left node check), allowing the peek function to return the value of the left node - which is now the last item in the list.
      After calling the peek method, the dequeue method is called to pop out the last element of the Queue self.items list, allowing the process to repeat now with the peek method returning the next value in the self.items list (which would be the right node after a left node's value was returned).
      Hope that made some sense.

    • @mohitweekendvlogs
      @mohitweekendvlogs 5 років тому

      @@mikedotleb now it make sense thanks

  • @jhoanmartinezsilva2609
    @jhoanmartinezsilva2609 4 роки тому

    You have to be a hater to dislike this video, big thanks

    • @LucidProgramming
      @LucidProgramming  4 роки тому

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!