 		return root
 	}
 	
-	// Inserts a new value to heap and supports heap property.
+	// Inserts a new element to heap and supports heap property.
 	// Complexity: O(log n)
-	public mutating func insert(_ value: T) {
-		a.append(value)
+	public mutating func insert(_ element: Element) {
+		a.append(element)
 		siftUp()
 	}
 	
 	// Build heap with array.
 	// Complexity: O(n)
-	private mutating func build(_ a: [T]) {
+	private mutating func build(_ a: [Element]) {
 		self.a = a
 		for i in stride(from: a.count / 2 - 1, through: 0, by: -1) {
 			siftDown(i)


import Foundation

// Min-heap.
public struct Heap<T>: CustomDebugStringConvertible where T: Comparable {
	
	// Return min value from heap.
	// Complexity: O(1)
	public var min: T? { return a.first }
	
	// Returns true if heap is empty.
	// Complexity: O(1)
	public var isEmpty: Bool { return a.isEmpty }
	
	// Returns the count of values.
	// Complexity: O(1)
	public var count: Int { return a.count }
	
	// Build heap with array.
	// Complexity: O(n)
	public init(_ a: [T] = []) {
		build(a)
	}
	
	// Deletes and returns a minimum value from heap.
	// Complexity: O(log n)
	public mutating func extractMin() -> T? {
		guard !isEmpty else { return nil }
		guard count > 1 else { return a.removeLast() }
		swap(0, a.count - 1)

//
//  Heap.swift
//  Binary min-heap.
//
//  Created by Vyacheslav Khorkov on 18/08/2017.
//  Copyright © 2017 Vyacheslav Khorkov. All rights reserved.
