File tree 2 files changed +26
-4
lines changed
2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -79,8 +79,7 @@ def sift_down(idx = 0)
79
79
return self if idx >= @tree . size
80
80
cidxs = @tree . class . children_idx ( idx , @tree . child_slots )
81
81
# promote the heapiest child
82
- cidx = self . heapiest ( cidxs )
83
- if !self . heapish? ( idx , cidx )
82
+ if ( cidx = self . heapiest ( cidxs ) ) and !self . heapish? ( idx , cidx )
84
83
@tree . swap ( idx , cidx )
85
84
self . sift_down ( cidx )
86
85
end
@@ -96,7 +95,7 @@ def heapish?(pidx, cidx)
96
95
# return the heapiest idx in cidxs
97
96
#
98
97
def heapiest ( cidxs )
99
- idx = cidxs . first
98
+ idx = cidxs . first or return nil
100
99
cidxs . each { |cidx |
101
100
idx = cidx if cidx < @tree . size and heapish? ( cidx , idx )
102
101
}
@@ -108,7 +107,7 @@ def heapiest(cidxs)
108
107
# * calls heapish? on idx's children and then heap? on them recursively
109
108
#
110
109
def heap? ( idx : 0 )
111
- check_children = [ ]
110
+ check_children = Array . new
112
111
@tree . class . children_idx ( idx , @tree . child_slots ) . each { |cidx |
113
112
# cidx may not be valid
114
113
if cidx < @tree . size
Original file line number Diff line number Diff line change
1
+ module CompSci
2
+ class Heap
3
+ @tree: CompleteTree
4
+ @cmp_val: Integer
5
+
6
+ attr_reader tree: CompleteTree
7
+ attr_reader cmp_val: Integer
8
+
9
+ def initialize : (?cmp_val: Integer,
10
+ ?minheap: bool ,
11
+ ?child_slots: Integer) -> void
12
+
13
+ def push : (untyped node) -> self
14
+ def pop : () -> untyped
15
+ def sift_up : (?Integer idx) -> self
16
+ def sift_down : (?Integer idx) -> self
17
+ def heapish? : (Integer pidx, Integer cidx) -> bool
18
+ def heapiest : (Array[Integer] cidxs) -> Integer?
19
+ def heap? : (?idx: ::Integer) -> bool
20
+ def size : () -> Integer
21
+ alias count size
22
+ end
23
+ end
You can’t perform that action at this time.
0 commit comments