Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac32f99

Browse files
Max binary heap and priority queue
1 parent 9639793 commit ac32f99

File tree

6 files changed

+270
-1
lines changed

6 files changed

+270
-1
lines changed

‎src/main/kotlin/pl/dmichalski/algorithms/data_structures/_4_queue/Node.kt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ class Node(private var value: String) {
2828
return "Node(value='$value', next=$next)"
2929
}
3030

31-
3231
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package pl.dmichalski.algorithms.data_structures._7_max_binary_heap
2+
3+
import java.lang.IllegalStateException
4+
5+
class MaxBinaryHeap {
6+
7+
private var values: MutableList<Int> = mutableListOf()
8+
9+
fun insert(value: Int) {
10+
this.values.add(value)
11+
bubbleUp()
12+
}
13+
14+
fun extractMax(): Int {
15+
if (values.size == 0) {
16+
throw IllegalStateException("Cannot extract max from an empty list")
17+
}
18+
19+
val max = values[0]
20+
val end = values.removeLast()
21+
if(values.size > 1) {
22+
values[0] = end
23+
sinkDown()
24+
}
25+
26+
return max
27+
}
28+
29+
override fun toString(): String {
30+
return "Max binary heap values: $values\n"
31+
}
32+
33+
private fun bubbleUp() {
34+
var idx = this.values.size - 1
35+
val element = this.values[idx]
36+
while (idx > 0) {
37+
val parentIdx = (idx - 1) / 2
38+
val parent = this.values[parentIdx]
39+
if (element > parent) {
40+
this.values[parentIdx] = element
41+
this.values[idx] = parent
42+
idx = parentIdx
43+
} else {
44+
break
45+
}
46+
}
47+
}
48+
49+
private fun sinkDown() {
50+
var parentIndex = 0
51+
val parent = values[0]
52+
val length = values.size
53+
var leftChild: Int? = null
54+
var rightChild: Int?
55+
56+
while(true) {
57+
var swapIdx: Int? = null
58+
val leftChildIdx = 2 * parentIndex + 1
59+
val rightChildIdx = 2 * parentIndex + 2
60+
61+
if(leftChildIdx < length) {
62+
leftChild = values[leftChildIdx]
63+
if (leftChild > parent) {
64+
swapIdx = leftChildIdx
65+
}
66+
}
67+
if(rightChildIdx < length) {
68+
rightChild = values[rightChildIdx]
69+
if ((swapIdx == null && rightChild > parent) || (swapIdx != null && rightChild > leftChild!!)) {
70+
swapIdx = rightChildIdx
71+
}
72+
}
73+
74+
if (swapIdx == null) {
75+
break
76+
}
77+
78+
this.values[parentIndex] = this.values[swapIdx]
79+
this.values[swapIdx] = parent
80+
parentIndex = swapIdx
81+
}
82+
}
83+
84+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package pl.dmichalski.algorithms.data_structures._7_max_binary_heap
2+
3+
object Runner {
4+
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("------------------ Initial max binary heap ------------------ ")
8+
var maxBinaryHeap = getMaxBinaryHeap()
9+
printMaxBinaryHeap(maxBinaryHeap)
10+
11+
println("------------------ After extracting max value from max binary heap ------------------ ")
12+
maxBinaryHeap = getMaxBinaryHeap()
13+
val extractMax = maxBinaryHeap.extractMax()
14+
println("Removed element: $extractMax")
15+
printMaxBinaryHeap(maxBinaryHeap)
16+
}
17+
18+
/**
19+
* It returns max binary heap like this:
20+
* <pre>
21+
* 55
22+
* / \
23+
* 18 45
24+
* / \ / \
25+
* 12 11 14 44
26+
*
27+
* </pre>
28+
*/
29+
private fun getMaxBinaryHeap(): MaxBinaryHeap {
30+
val maxBinaryHeap = MaxBinaryHeap()
31+
32+
maxBinaryHeap.insert(12)
33+
maxBinaryHeap.insert(44)
34+
maxBinaryHeap.insert(14)
35+
maxBinaryHeap.insert(18)
36+
maxBinaryHeap.insert(11)
37+
maxBinaryHeap.insert(45)
38+
maxBinaryHeap.insert(58)
39+
40+
return maxBinaryHeap
41+
}
42+
43+
private fun printMaxBinaryHeap(maxBinaryHeap: MaxBinaryHeap) {
44+
println(maxBinaryHeap)
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pl.dmichalski.algorithms.data_structures._8_priority_queue
2+
3+
class Node(private var value: String, private var priority: Int) {
4+
5+
fun getValue(): String {
6+
return value
7+
}
8+
9+
fun getPriority(): Int {
10+
return priority
11+
}
12+
13+
override fun toString(): String {
14+
return "Node(value='$value', priority=$priority)"
15+
}
16+
17+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package pl.dmichalski.algorithms.data_structures._8_priority_queue
2+
3+
import java.lang.IllegalStateException
4+
5+
class PriorityQueue {
6+
7+
private var values: MutableList<Node> = mutableListOf()
8+
9+
/**
10+
* O(log n) time complexity
11+
*/
12+
fun enqueue(value: String, priority: Int) {
13+
val node = Node(value, priority)
14+
this.values.add(node)
15+
bubbleUp()
16+
}
17+
18+
/**
19+
* O(log n) time complexity
20+
*/
21+
fun dequeue(): Node {
22+
if (values.size == 0) {
23+
throw IllegalStateException("Cannot extract max from an empty list")
24+
}
25+
26+
val min = values[0]
27+
val end = values.removeLast()
28+
if(values.size > 1) {
29+
values[0] = end
30+
sinkDown()
31+
}
32+
33+
return min
34+
}
35+
36+
override fun toString(): String {
37+
return "Priority queue values: $values\n"
38+
}
39+
40+
private fun bubbleUp() {
41+
var idx = this.values.size - 1
42+
val element = this.values[idx]
43+
while (idx > 0) {
44+
val parentIdx = (idx - 1) / 2
45+
val parent = this.values[parentIdx]
46+
if (element.getPriority() >= parent.getPriority()) {
47+
break
48+
} else {
49+
this.values[parentIdx] = element
50+
this.values[idx] = parent
51+
idx = parentIdx
52+
}
53+
}
54+
}
55+
56+
private fun sinkDown() {
57+
var parentIndex = 0
58+
val parent = values[0]
59+
val length = values.size
60+
var leftChild: Node? = null
61+
var rightChild: Node?
62+
63+
while(true) {
64+
var swapIdx: Int? = null
65+
val leftChildIdx = 2 * parentIndex + 1
66+
val rightChildIdx = 2 * parentIndex + 2
67+
68+
if(leftChildIdx < length) {
69+
leftChild = values[leftChildIdx]
70+
if (leftChild.getPriority() < parent.getPriority()) {
71+
swapIdx = leftChildIdx
72+
}
73+
}
74+
if(rightChildIdx < length) {
75+
rightChild = values[rightChildIdx]
76+
if ((swapIdx == null && rightChild.getPriority() < parent.getPriority()) || (swapIdx != null && rightChild.getPriority() < leftChild!!.getPriority())) {
77+
swapIdx = rightChildIdx
78+
}
79+
}
80+
81+
if (swapIdx == null) {
82+
break
83+
}
84+
85+
this.values[parentIndex] = this.values[swapIdx]
86+
this.values[swapIdx] = parent
87+
parentIndex = swapIdx
88+
}
89+
}
90+
91+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package pl.dmichalski.algorithms.data_structures._8_priority_queue
2+
3+
object Runner {
4+
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("------------------ Initial priority queue ------------------ ")
8+
var priorityQueue = getPriorityQueue()
9+
printPriorityQueue(priorityQueue)
10+
11+
println("------------------ Dequeue priority queue elements ------------------ ")
12+
priorityQueue = getPriorityQueue()
13+
val element = priorityQueue.dequeue()
14+
println("Dequeued element: $element")
15+
printPriorityQueue(priorityQueue)
16+
}
17+
18+
private fun getPriorityQueue(): PriorityQueue {
19+
val priorityQueue = PriorityQueue()
20+
21+
priorityQueue.enqueue("Medium priority", 5)
22+
priorityQueue.enqueue("Low priority", 10)
23+
priorityQueue.enqueue("Max priority", 1)
24+
priorityQueue.enqueue("Big priority", 3)
25+
26+
return priorityQueue
27+
}
28+
29+
private fun printPriorityQueue(maxBinaryHeap: PriorityQueue) {
30+
println(maxBinaryHeap)
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /