-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.kt
39 lines (35 loc) · 857 Bytes
/
Utils.kt
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
package dataStructures.heap
/**
* Changes one element in the [heap] with another
*
* @param i index of the element to change with [j]
* @param j index of the element to change with [i]
* @param heap array to make the change
*/
fun <E> exchange(i: Int, j: Int, heap: Array<Node<E>?>) {
if (i < 0 || j < 0) return
val tmp = heap[i]
heap[i] = heap[j]
heap[j] = tmp
}
/**
* Gets the left index of the [i] element
*
* @param i index of the element
* @return left index of the [i] element
*/
fun left(i: Int) = 2 * i + 1
/**
* Gets the right index of the [i] element
*
* @param i index of the element
* @return right index of the [i] element
*/
fun right(i: Int) = 2 * i + 2
/**
* Gets the parent index of the [i] element
*
* @param i index of the element
* @return parent index of the [i] element
*/
fun parent(i: Int) = (i - 1) / 2