You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 2a27405
Browse filesBrowse the repository at this point in the historyBrowse files
A Trie is a special form of a Nary tree. Typically, a trie is used to store strings. Each Trie node represents a string
@@ -632,6 +631,52 @@ class TrieNode:
632
631
self.child_nodes: Dict[str: TrieNode] =dict()
633
632
```
634
633
634
+
## Min Heap
635
+
636
+
A Min Heap is a complete binary tree where every parent node is smaller than or equal to its children. The smallest element is always at the root. It is commonly backed by an array where for a node at index `i`, its parent is at `(i - 1) // 2` and its children are at `2i + 1` and `2i + 2`.
637
+
638
+
New values are inserted at the end of the array and then "sifted up" — repeatedly swapped with their parent until the heap property is restored.
639
+
640
+
```python
641
+
classMinHeap:
642
+
def__init__(self):
643
+
self.list = []
644
+
645
+
definsert(self, val):
646
+
self.list.append(val)
647
+
self._sift_up(len(self.list) -1)
648
+
649
+
defis_empty(self):
650
+
returnlen(self.list) ==0
651
+
652
+
defsize(self):
653
+
returnlen(self.list)
654
+
655
+
defvalues(self):
656
+
values = []
657
+
for value inself.list:
658
+
values.append(value)
659
+
return values
660
+
661
+
def_get_parent(self, idx):
662
+
return (idx -1) //2
663
+
664
+
def_sift_up(self, idx):
665
+
current_index = idx
666
+
667
+
while current_index >0:
668
+
parent =self._get_parent(current_index)
669
+
670
+
ifself.list[current_index] <self.list[parent]:
671
+
self.list[current_index], self.list[parent] = (
672
+
self.list[parent],
673
+
self.list[current_index],
674
+
)
675
+
current_index = parent
676
+
else:
677
+
break
678
+
```
679
+
635
680
Trie like implementation with a flat dictionary (Hash table)
0 commit comments