mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-21 11:06:07 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -12,13 +12,13 @@ from modules import print_heap
|
||||
|
||||
|
||||
class MaxHeap:
|
||||
"""Max-heap"""
|
||||
"""Max heap"""
|
||||
|
||||
def __init__(self, nums: list[int]):
|
||||
"""Constructor, build heap based on input list"""
|
||||
# Add all list elements into the heap
|
||||
# Add list elements to heap as is
|
||||
self.max_heap = nums
|
||||
# Heapify all nodes except leaves
|
||||
# Heapify all nodes except leaf nodes
|
||||
for i in range(self.parent(self.size() - 1), -1, -1):
|
||||
self.sift_down(i)
|
||||
|
||||
@@ -32,7 +32,7 @@ class MaxHeap:
|
||||
|
||||
def parent(self, i: int) -> int:
|
||||
"""Get index of parent node"""
|
||||
return (i - 1) // 2 # Integer division down
|
||||
return (i - 1) // 2 # Floor division
|
||||
|
||||
def swap(self, i: int, j: int):
|
||||
"""Swap elements"""
|
||||
@@ -43,62 +43,62 @@ class MaxHeap:
|
||||
return len(self.max_heap)
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if heap is empty"""
|
||||
"""Check if heap is empty"""
|
||||
return self.size() == 0
|
||||
|
||||
def peek(self) -> int:
|
||||
"""Access heap top element"""
|
||||
"""Access top element"""
|
||||
return self.max_heap[0]
|
||||
|
||||
def push(self, val: int):
|
||||
"""Push the element into heap"""
|
||||
"""Element enters heap"""
|
||||
# Add node
|
||||
self.max_heap.append(val)
|
||||
# Heapify from bottom to top
|
||||
self.sift_up(self.size() - 1)
|
||||
|
||||
def sift_up(self, i: int):
|
||||
"""Start heapifying node i, from bottom to top"""
|
||||
"""Starting from node i, heapify from bottom to top"""
|
||||
while True:
|
||||
# Get parent node of node i
|
||||
p = self.parent(i)
|
||||
# When "crossing the root node" or "node does not need repair", end heapification
|
||||
# When "crossing root node" or "node needs no repair", end heapify
|
||||
if p < 0 or self.max_heap[i] <= self.max_heap[p]:
|
||||
break
|
||||
# Swap two nodes
|
||||
self.swap(i, p)
|
||||
# Loop upwards heapification
|
||||
# Loop upward heapify
|
||||
i = p
|
||||
|
||||
def pop(self) -> int:
|
||||
"""Element exits heap"""
|
||||
# Empty handling
|
||||
# Handle empty case
|
||||
if self.is_empty():
|
||||
raise IndexError("Heap is empty")
|
||||
# Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
# Swap root node with rightmost leaf node (swap first element with last element)
|
||||
self.swap(0, self.size() - 1)
|
||||
# Remove node
|
||||
# Delete node
|
||||
val = self.max_heap.pop()
|
||||
# Heapify from top to bottom
|
||||
self.sift_down(0)
|
||||
# Return heap top element
|
||||
# Return top element
|
||||
return val
|
||||
|
||||
def sift_down(self, i: int):
|
||||
"""Start heapifying node i, from top to bottom"""
|
||||
"""Starting from node i, heapify from top to bottom"""
|
||||
while True:
|
||||
# Determine the largest node among i, l, r, noted as ma
|
||||
# Find node with largest value among i, l, r, denoted as ma
|
||||
l, r, ma = self.left(i), self.right(i), i
|
||||
if l < self.size() and self.max_heap[l] > self.max_heap[ma]:
|
||||
ma = l
|
||||
if r < self.size() and self.max_heap[r] > self.max_heap[ma]:
|
||||
ma = r
|
||||
# If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
# If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
if ma == i:
|
||||
break
|
||||
# Swap two nodes
|
||||
self.swap(i, ma)
|
||||
# Loop downwards heapification
|
||||
# Loop downward heapify
|
||||
i = ma
|
||||
|
||||
def print(self):
|
||||
@@ -108,30 +108,30 @@ class MaxHeap:
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize max-heap
|
||||
# Initialize max heap
|
||||
max_heap = MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2])
|
||||
print("\nEnter list and build heap")
|
||||
print("\nAfter inputting list and building heap")
|
||||
max_heap.print()
|
||||
|
||||
# Access heap top element
|
||||
# Get top element
|
||||
peek = max_heap.peek()
|
||||
print(f"\nHeap top element is {peek}")
|
||||
print(f"\nTop element is {peek}")
|
||||
|
||||
# Push the element into heap
|
||||
# Element enters heap
|
||||
val = 7
|
||||
max_heap.push(val)
|
||||
print(f"\nElement {val} after pushed into heap")
|
||||
print(f"\nAfter element {val} enters heap")
|
||||
max_heap.print()
|
||||
|
||||
# Pop the element at the heap top
|
||||
# Top element exits heap
|
||||
peek = max_heap.pop()
|
||||
print(f"\nHeap top element {peek} after exiting heap")
|
||||
print(f"\nAfter top element {peek} exits heap")
|
||||
max_heap.print()
|
||||
|
||||
# Get heap size
|
||||
size = max_heap.size()
|
||||
print(f"\nNumber of heap elements is {size}")
|
||||
|
||||
# Determine if heap is empty
|
||||
# Check if heap is empty
|
||||
is_empty = max_heap.is_empty()
|
||||
print(f"\nIs the heap empty {is_empty}")
|
||||
print(f"\nIs heap empty {is_empty}")
|
||||
|
||||
Reference in New Issue
Block a user