<jupyter_start><jupyter_text>Heap queue algorithmHeaps are binary trees for which every parent node has a value less than or equal to any of its children.- We use zero-based indexing. This makes the relationship between the index for a node and the indexes for its children slightly less obvious, but is more suitable since Python uses zero-based indexing. - Our pop method returns the smallest item, not the largest (called a “min heap” in textbooks; a “max heap” is more common in texts because of its suitability for in-place sorting).<jupyter_code>import heapq

heap = []
heapq.heappush(heap, 10)
heap
heapq.heappush(heap, 1)
heap
heapq.heappush(heap, 5)
heap
heapq.heappop(heap)
heap
heapq.heappop(heap)
heap
list1 = [1, 3, 5, 2, 4, 6]
heapq.heapify(list1)
list1
heapq.heappushpop(list1, 89)
list1<jupyter_output><empty_output><jupyter_text>- first pop and then push -> heapreplace<jupyter_code>heapq.heapreplace(list1, 100)
list1
heap = [1, 20, 5, 4, 3, 2, 6]
heapq.nsmallest(2, heap)
heapq.nsmallest(4, heap)
heapq.nlargest(3, heap)<jupyter_output><empty_output><jupyter_text>- lets implement a priority queue using heapq<jupyter_code>list1 = [(1, "ria"), (4, "sia"), (3, "gia")]
heapq.heapify(list1)
for i in range(len(list1)):
    print(heapq.heappop(list1))<jupyter_output>(1, 'ria')
(3, 'gia')
(4, 'sia')<jupyter_text>Stack using Deque Module<jupyter_code>import collections

stack = collections.deque()

stack
stack.append(10)
stack.append(20)
stack.append(30)
stack
stack.pop()
stack.pop()
stack.pop()
# no element is present in stack
not stack<jupyter_output><empty_output><jupyter_text>Queue using Deque<jupyter_code>import collections

queue = collections.deque()

queue
queue.appendleft(10)
queue.appendleft(20)
queue.appendleft(30)
queue
queue.pop()
queue.pop()
queue.pop()
not queue<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11796832332058985, 'fraction_numerical': 0.030038230475150193, 'mean_word_length': 6.21259842519685, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '96401', 'license': 'MIT', 'n_tokens_mistral': 700, 'n_tokens_neox': 661, 'n_words': 196, 'path': 'Python Data Structures.ipynb', 'repo_name': 'abdurrehman11/python-stuff', 'size': '1831'}