-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbinary_heap.ml
170 lines (148 loc) · 4.89 KB
/
binary_heap.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module type S = sig
include Binary_search_tree.BST
val peek : t -> comparable option
val extract : t -> (comparable option) * t
end
module BinaryHeap (Ord : Ord.S)
: (S with type comparable := Ord.t) =
struct
type heap = { size: int;
items: Ord.t option array;
}
(* Our type t is a heap with an index *)
type t = { heap: heap;
index: int;
}
let parent_index n = n / 2
let left_child_index n = 2 * n + 1
let right_child_index n = 2 * n + 2
let swap root a b =
let items = Array.copy root.heap.items in
{ root with
heap = { root.heap
with items = begin
let tmp = items.(a) in
items.(a) <- items.(b);
items.(b) <- tmp;
items
end
}
}
let rec bubble_up index root =
let items = root.heap.items in
match (items.(index), items.(parent_index index)) with
| (Some value, Some parent_value) ->
if Ord.compare value parent_value < 0
then
swap root (parent_index index) index
|> bubble_up (parent_index index)
else
root
| _ -> raise (Invalid_argument "index out of bounds")
let rec bubble_down index root =
match root.heap.items.(index) with
| None -> root
| Some value -> (
let safe_index i =
if i >= root.heap.size then None
else root.heap.items.(i) in
let left = left_child_index index |> safe_index in
let right = right_child_index index |> safe_index in
match (left, right) with
| (None, None) -> root
| (Some v, None) ->
(* Should we swap with our left child? *)
if Ord.compare v value < 0 then
swap root index (left_child_index index)
|> bubble_down (left_child_index index)
else root
| (None, Some v) ->
(* Should we swap with our right child? *)
if Ord.compare v value < 0 then
swap root index (right_child_index index)
|> bubble_down (right_child_index index)
else root
| (Some a, Some b) ->
(* Find the `min` of a and b *)
let (min_value, min_index) =
if Ord.compare a b < 0 then (a, left_child_index index)
else (b, right_child_index index) in
(* Should we swap with the min of a and b? *)
if Ord.compare min_value value < 0 then
swap root index min_index
|> bubble_down min_index
else root)
(* Heaps aren't Binary search trees, so `find` will be useless,
but we'll get `height` and `string_of_tree` for free.
TODO: Make another Binary_tree.Make maybe?
*)
include (
Binary_search_tree.Make (Ord) (struct
type nonrec t = t
let empty_tree = {
heap = { items = [| None |];
size = 0;
};
index = 0;
}
(* Update a node with an index, but return empty_tree
if that index is out of bounds.
This allows us to safely call `value` of a node's left
or right child.
*)
let maybe_emptify node index =
if index >= node.heap.size
then empty_tree
else { node with index = index }
let left node = maybe_emptify node (left_child_index node.index)
let right node = maybe_emptify node (right_child_index node.index)
let value node = match node.heap.items.(node.index) with
| Some v -> v
| None -> raise (Invalid_argument "index out of bounds")
let maybe_increase_capacity root =
if root.heap.size = Array.length root.heap.items then
let double_capacity_items =
Array.append
root.heap.items
(Array.make root.heap.size None) in
{ root with
heap = { root.heap with
items = double_capacity_items
}
}
else root
let insert root value =
{ root with
heap =
let items = Array.copy root.heap.items in
let size = root.heap.size in
{ items = (
items.(size) <- Some value; items
);
size = root.heap.size + 1;
}
}
|> bubble_up root.heap.size
|> maybe_increase_capacity
end
) : (Binary_search_tree.BST
with type t := t
and type comparable := Ord.t))
let peek root = root.heap.items.(0)
let extract root = match peek root with
| None -> (None, root)
| Some v -> (
Some v,
let items = Array.copy root.heap.items in
let {size} = root.heap in
{ root with
heap = {
items = begin
items.(0) <- items.(size - 1);
items.(size - 1) <- None;
items
end;
size = size - 1;
}
} |> bubble_down 0)
end