This got me passed my bootcamp. I watched as many as 5 different binary search tree node remove videos but I could not figure out how how to do it. I stumbled onto this video and I watched it multiple times. It finally clicked. Thank you. I would still be sitting at my computer without this.
At 11:50 I think it should be if index > self.length() instead of index >= self.length() This will ignore the last element and it cannot be accessed. I am still a newbie, correct me if I am wrong. Anyway, great series, thank you sir
I know I'm really late about these ones, but thanks a lot for all your videos. They could'nt be more useful before my python test. I don't understand why arn't u already famous ! Ur explanations are so clear please never stop doing video's
This week I found your videos, for a topic that I really didn't understand and I just want to thank you for your work! you explain very well, I love to learn like this. Keep it up, you must upload videos transmitting your knowledge
Instead of doing another costly iteration through the linked list to find the size of it. We can add a self.length variable to the init() with an initial size of 0, and then add a 1 in the append() and a subtract a 1 in the remove method
doesn't work if you repeat deletion of the same value fyi For the function delete_value, we need to catch this edge case. def delete_value(self, value): if self.find(value) == None: #if we can't find the value return None return self.delete_node(self.find(value))
Thank you I was wondering how do you create pointers in Python. My favourite languages are C++, C#. It took me three years to learn them. I think I’m going to give Python a try, it looks easy. Thank you.
Actually this implementation is not 100% correct. The actual first element or the head of the linkedList will always be none as per the provided implementation. You would have to check if the head has a next element and if its data is none. If so, the head node should get the data: def append(self, data): new_node = Node(data) current = self.head while current.next is not None: current = current.next if current == self.head and self.head.data is None: current.data = data else: current.next = new_node
class node: def __init__(self, data=None): self.data = data self.next = None class linked_list: def __init__(self): self.head = node() # Adds new node containing 'data' to the end of the linked list. def append(self, data): new_node = node(data) if self.head.data == None: self.head = new_node else: cur = self.head while cur.next != None: cur = cur.next cur.next = new_node # Returns the length (integer) of the linked list. def length(self): cur = self.head count = 1 while cur.next != None: count += 1 cur = cur.next return count # Prints out the linked list in traditional Python list format. def display(self): elems = [] cur_node = self.head while cur_node.next != None: elems.append(cur_node.data) cur_node = cur_node.next elems.append(cur_node.data) print(elems) # Returns the value of the node at 'index'. def get(self, index): if index >= self.length() or index < 0: # added 'index<0' post-video print("ERROR: 'Get' Index out of range!") return None cur_idx = 0 cur_node = self.head while cur_node.data != None: if cur_idx == index: return cur_node.data cur_node = cur_node.next cur_idx += 1 # Deletes the node at index 'index'. def erase(self, index): if index >= self.length() or index < 0: # added 'index<0' post-video print("ERROR: 'Erase' Index out of range!") return curr_idx = 0 last_node = self.head if index == 0: self.head = self.head.next else: curr_idx = 1 while True: curr_node = last_node.next if index == curr_idx: last_node.next = curr_node.next return last_node = curr_node curr_idx += 1 l = linked_list() l.append(1) l.append(2) l.append(3) # 2 l.append(4) l.display() l.erase(1) l.display() little bit readable code
I love you dude thanks for explaining this stuff so clearly. So many other channels explain this stuff so badly, but you make it so easy to understand.
Beware. While this is a good presentation of how linked lists work, it gets the single most important detail wrong. The whole point of using a linked list as opposed to a regular list in python is that it has better time complexity when you need to frequently add and remove items from the FRONT, like a FIFO data structure or queue. The append method in the code presented here has to iterate through the entire data set to add a new element which is O(n), but a proper linked list should add new data to the FRONT, which improves time complexity to O(1). Instead of an append method(or at least in addition to) it should have something like an add() method which could look something like this: def add(self, data): new_node = node(data) new_node.next = self.head.next self.head.next = new_node
He replied 5 years ago with this when someone mentioned about the append being O(n) Hi Xiufeng, thanks for the nice words! Yes the way the append function works currently is O(n), and is not the most efficient. By adding in an extra class member variable, we can call 'tail' for example, we can reduce the complexity of the append function down to constant time, or O(1). A possible implementation could be as follows: > def append(self,data): > new_node=node(data) > self.tail.next=new_node > self.tail=new_node Take note that for this to work, we will need to declare this 'tail' variable in the constructor of the class: > def __init__(self): > self.head=node() > self.tail=self.head If you wish to take this approach, you'll also need to be careful to set the 'tail' correctly inside of the 'erase' function (for example, if you try to erase the last element in the list, you'll need to set the tail to the new last element). The following 'erase' function includes a single 'if' statement which should achieve this: > def erase(self,index): > if index>=self.length() or index<0: > print "ERROR: 'Erase' Index out of range!" > return > cur_idx=0 > cur_node=self.head > while True: > last_node=cur_node > cur_node=cur_node.next > if cur_idx==index: > last_node.next=cur_node.next > if last_node.next==None: self.tail=last_node ## Here is where we set the tail > return > cur_idx+=1 I've done some simple tests with this new implementation and it seems to be working, but feel free to let me know if you find any issues or have any other questions! I'm going to pin this comment so any others with the same question can see it.
as u can see a single neuron in a neural network is one complete output on a single layer. so a single layer with one output is just one single neuron.
that ai winter is just bullshit. how much other bullshit is going around and ppl dont notice just passes for truth?!?!? do ppl know and say to themselves "oh ppl think thats true at the moment so i just continue it on." u just cant trust anyone.
Thanks for this video, you've explained the code and data structure really thoroughly and I'm starting to understand the implementation of LinkedLists. However, I found a bug with the length method that you wrote in the video. I found that if you try to access the last element of LinkedList it will raise the error due to the count starting at 0 rather than 1. The fix is simply start the count at 1 because it counts the empty node and makes the final element inaccessible via the get method that you defined. Also, with the change above, you want to move the `if cur_index == index: return cur_node.data` statement to the top of the while loop or you'll get an error stating cur_node.data is None (the get method)
Worth mentioning that `d = {}` could actually become a `set` if you're not careful. Some suggest using the constructer object instead. E.g. `d = dict(my_key="my_value")`