Skip to content

pydsa.heaps

Heaps that hold the items as they are, smallest first or largest first.

maxheappush, maxheappop, maxheapify, maxheappushpop, maxheapreplace, maxheappeek,
maxheapitems

Each function is its heapq namesake with the prefix max, same signature, and the largest item comes out first. Items can be anything with <, tuples included: there is no negation and no wrapper, so h[0] is the largest item and iterating h gives the items.

A heap class and draw_heap, which draws a list heap as the tree it is.

heap

A heap of items, smallest first, or largest first with type=max.

push, pop and peek as the words say; append is push, so a heap can stand in for a list. len and iteration work; iteration is heap order, not sorted order. h is the list of items in heap order, for min and max the same.

Source code in pydsa/heaps.mu
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class heap
  """A heap of items, smallest first, or largest first with type=max.

  push, pop and peek as the words say; append is push, so a heap can
  stand in for a list. len and iteration work; iteration is heap order,
  not sorted order. h is the list of items in heap order, for min and
  max the same."""

  def __init__(items=(), type=min)
    self.before = type is max ? _gt : _lt
    self.h = list items
    for i in reversed(0..<len(self.h) // 2)
      _down(self.h, i, self.before)

  def push(x) -> none: _push(self.h, x, self.before)
  def pop(): _pop(self.h, self.before)
  def peek(): self.h[0]
  def __len__(): len self.h
  def __iter__(): iter self.h
  append = push
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class heap:
    """A heap of items, smallest first, or largest first with type=max.

    push, pop and peek as the words say; append is push, so a heap can
    stand in for a list. len and iteration work; iteration is heap order,
    not sorted order. h is the list of items in heap order, for min and
    max the same."""

    def __init__(self, items=(), type=min):
        self.before = _gt if type is max else _lt
        self.h = list(items)
        for i in reversed(range(0, len(self.h) // 2)):
            _down(self.h, i, self.before)

    def push(self, x) -> None:
        _push(self.h, x, self.before)

    def pop(self):
        return _pop(self.h, self.before)

    def peek(self):
        return self.h[0]

    def __len__(self):
        return len(self.h)

    def __iter__(self):
        return iter(self.h)

    append = push

before instance-attribute

before = _gt if type is max else _lt

h instance-attribute

h = list(items)

append class-attribute instance-attribute

append = push

push

push(x) -> None
Source code in pydsa/heaps.mu
78
def push(x) -> none: _push(self.h, x, self.before)
102
103
def push(self, x) -> None:
    _push(self.h, x, self.before)

pop

pop()
Source code in pydsa/heaps.mu
79
def pop(): _pop(self.h, self.before)
105
106
def pop(self):
    return _pop(self.h, self.before)

peek

peek()
Source code in pydsa/heaps.mu
80
def peek(): self.h[0]
108
109
def peek(self):
    return self.h[0]

maxheapify

maxheapify(h: list) -> None
Source code in pydsa/heaps.mu
44
45
46
def maxheapify(h: list) -> none
  for i in reversed(0..<len(h) // 2)
    _down(h, i, _gt)
54
55
56
def maxheapify(h: list) -> None:
    for i in reversed(range(0, len(h) // 2)):
        _down(h, i, _gt)

maxheappushpop

maxheappushpop(h: list, item)
Source code in pydsa/heaps.mu
48
49
50
51
52
def maxheappushpop(h: list, item)
  if h and item < h[0]
    item, h[0] = h[0], item
    _down(h, 0, _gt)
  item
59
60
61
62
63
def maxheappushpop(h: list, item):
    if h and item < h[0]:
        item, h[0] = h[0], item
        _down(h, 0, _gt)
    return item

maxheapreplace

maxheapreplace(h: list, item)
Source code in pydsa/heaps.mu
54
55
56
57
def maxheapreplace(h: list, item)
  top, h[0] = h[0], item
  _down(h, 0, _gt)
  top
66
67
68
69
def maxheapreplace(h: list, item):
    top, h[0] = h[0], item
    _down(h, 0, _gt)
    return top

maxheappush

maxheappush(h: list, item) -> None
Source code in pydsa/heaps.mu
59
def maxheappush(h: list, item) -> none: _push(h, item, _gt)
72
73
def maxheappush(h: list, item) -> None:
    _push(h, item, _gt)

maxheappop

maxheappop(h: list)
Source code in pydsa/heaps.mu
60
def maxheappop(h: list): _pop(h, _gt)
76
77
def maxheappop(h: list):
    return _pop(h, _gt)

maxheappeek

maxheappeek(h: list)
Source code in pydsa/heaps.mu
61
def maxheappeek(h: list): h[0]
80
81
def maxheappeek(h: list):
    return h[0]

maxheapitems

maxheapitems(h: list) -> list
Source code in pydsa/heaps.mu
62
def maxheapitems(h: list) -> list: list h
84
85
def maxheapitems(h: list) -> list:
    return list(h)

draw_heap

draw_heap(heap_list: list[int]) -> None

Utility function to draw a heap represented as a list in the terminal. The heap list is assumed to be in level-order (array representation of a complete binary tree).

Source code in pydsa/heaps.mu
85
86
87
88
89
90
91
92
93
def draw_heap(heap_list: [int]) -> none
  """
  Utility function to draw a heap represented as a list in the terminal.
  The heap list is assumed to be in level-order (array representation of a complete binary tree).
  """
  if not heap_list
    print "Empty heap"
    return
  draw_tree(build_tree(list(heap_list)))
120
121
122
123
124
125
126
127
128
def draw_heap(heap_list: list[int]) -> None:
    """
    Utility function to draw a heap represented as a list in the terminal.
    The heap list is assumed to be in level-order (array representation of a complete binary tree).
    """
    if not heap_list:
        print("Empty heap")
        return
    draw_tree(build_tree(list(heap_list)))