On CLRS's textbook "Introduction to Algorithm", there's such paragraph on pg. 258.
We can delete an element in O(1) time if the lists are doubly linked. (Note that CHAINED-HASH-DELETE takes as input an element x and not its key k, so that we don't have to search for x first. If the hash table supports deletion, then its linked list should be doubly linked so that we can delete an item quickly. If the lists were only singly linked, then to delete element x, we would first have to find x in the list so that we could update the next attribute of x's predecessor. With singly linked lists, both deletion and searching would have the same asymptotic running times).
What puzzle me is this big parenthses, I failed to understand its logic. With doubly linked list, one still have to find x in order to delete it, how is this different from singly linked list? Please help me to understand it!
In short: if you know the cell to remove in advance, the doubly-linked list lets you remove it in time O(1) while a singly-linked list would require time O(n). If you don't know the cell in advance, then it's O(n) in both cases. Hope this helps!
In a doubly linked list, we need to delete a node from the linked list. we just need to copy the head pointer to pointer ptr and shift the head pointer to its next. Deletion Time Complexity (AVG) Θ(1)
What is the time complexity of delete function in the hash table using a doubly linked list? Explanation: Time complexity of delete function in a hash table is O(1).
The problem presented here is : consider you have are looking at a particular element of a hashtable. How costly is it to delete it?
Suppose you have a simple linked list :
v ----> w ----> x ----> y ----> z | you're here
Now if you remove x
, you need to connect w
to y
to keep your list linked. You need to access w
and tell it to point to y
(you want to have w ----> y
). But you can't access w
from x
because it's simply linked! Thus you have to go through all your list to find w
in O(n) operations, and then tell it to link to y
. That's bad.
Then, suppose you're doubly-linked :
v <---> w <---> x <---> y <---> z | you're here
Cool, you can access w and y from here, so you can connect the two (w <---> y
) in O(1) operation!
It seems to me that the hash table part of this is mostly a red herring. The real question is: "can we delete the current element from a linked list in constant time, and if so how?"
The answer is: it's a little tricky, but in effect yes, we can -- at least usually. We do not (normally) have to traverse the entire linked list to find the previous element. Instead, we can swap the data between the current element and the next element, then delete the next element.
The one exception to this is when/if we need/want to delete the last item in the list. In this case, there is no next element to swap with. If you really have to do that, there's no real way to avoid finding the previous element. There are, however, ways that will generally work to avoid that -- one is to terminate the list with a sentinel instead of a null pointer. In this case, since we never delete the node with the sentinel value, we never have to deal with deleting the last item in the list. That leaves us with relatively simple code, something like this:
template <class key, class data> struct node { key k; data d; node *next; }; void delete_node(node *item) { node *temp = item->next; swap(item->key, temp->key); swap(item->data, temp->data); item ->next = temp->next; delete temp; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With