-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.js
More file actions
42 lines (42 loc) · 904 Bytes
/
Copy pathheap.js
File metadata and controls
42 lines (42 loc) · 904 Bytes
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
function heap()
{
this.items = []
}
heap.prototype.swap = function(index1,index2)
{
var temp = this.items[index1];
this.items[index1]=this.items[index2];
this.items[index2]=temp;
}
heap.prototype.parentIndex = function(index)
{
return Math.floor((index-1)/2)
}
heap.prototype.leftChildINdex = function(index)
{
return index * 2 + 1;
}
heap.prototype.rightChildIndex = function(index)
{
return index * 2 + 2;
}
heap.prototype.parent = function(index)
{
return this.items[this.parentIndex(index)];
}
heap.prototype.leftChild = function(index)
{
return this.items[this.leftChildINdex(index)];
}
heap.prototype.rightChild = function(index)
{
return this.items[this.rightChildINdex(index)];
}
heap.prototype.peek = function(item)
{
return this.item[0];
}
heap.prototype.size = function()
{
return this.items.length;
}