-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedHeap.java
More file actions
149 lines (135 loc) · 3.88 KB
/
LinkedHeap.java
File metadata and controls
149 lines (135 loc) · 3.88 KB
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
import java.util.List;
public class LinkedHeap<K extends Comparable<K>, V> extends LinkedBinaryTree<Entry<K, V>> implements Heap<K, V> {
private static final Heap.Type MAX_HEAP = null;
// private Position<Entry<K, V>> root = null;
private Heap.Type type;
LinkedHeap() {
}
LinkedHeap(Heap.Type type) {
this.type = type;
}
/**
* Inserts a key-value pair and return the entry created
*
*/
@Override
public Entry<K, V> insert(K key, V value) { //root³ëµåºÎÅÍ ÀúÀåÀÌ ¾ÈµÊ
Position<Entry<K, V>> newEntry = new INode<>(new Entry<>(key, value));
List<Position<Entry<K, V>>> heap = levelOrder();
Position<Entry<K, V>> tempParent;
//Position<Entry<K, V>> prevLastNode = heap.get(heap.size()-1);
if(heap.size() % 2 == 0){
tempParent = heap.get(heap.size() / 2 - 1);
tempParent.setRight(newEntry);
}
else{
tempParent = heap.get(heap.size() / 2);
tempParent.setLeft(newEntry);
}
upHeap(newEntry);
size += 1;
return newEntry.element();
}
public void upHeap(Position<Entry<K, V>> newEntry){
Position<Entry<K, V>> curNode = newEntry;
Entry<K, V> temp;
if(type.toString() == "MAX_HEAP"){
while(curNode != root){
if((int)curNode.element().key > (int)curNode.parent().element().key){
//System.out.println(curNode.parent() + " --- " + curNode);
temp = curNode.parent().element();
curNode.parent().replace(curNode.element());
curNode.replace(temp);
//System.out.println(curNode.parent() + " --- " + curNode);
}
curNode = curNode.parent();
}
}
else{
while(curNode != root){
if((int)curNode.element().key < (int)curNode.parent().element().key){
temp = curNode.parent().element();
curNode.parent().replace(curNode.element());
curNode.replace(temp);
}
curNode = curNode.parent();
}
}
}
public void downHeap(){
Position<Entry<K, V>> curNode = root;
Entry<K, V> temp;
if(type.toString() == "MAX_HEAP"){
while(curNode.hasLeft() != false && curNode.hasRight() != false){
if((int)curNode.left().element().key > (int)curNode.right().element().key){
temp = curNode.element();
curNode.replace(curNode.left().element());
curNode.left().replace(temp);
curNode = curNode.left();
}
else{
temp = curNode.element();
curNode.replace(curNode.right().element());
curNode.right().replace(temp);
curNode = curNode.right();
}
}
}
else{
while(curNode.hasLeft() != false && curNode.hasRight() != false){
//System.out.println( " "+ curNode);
//System.out.println(curNode.left() + " --- " + curNode.right());
if((int)curNode.left().element().key < (int)curNode.right().element().key){
temp = curNode.element();
curNode.replace(curNode.left().element());
curNode.left().replace(temp);
curNode = curNode.left();
}
else{
temp = curNode.element();
curNode.replace(curNode.right().element());
curNode.right().replace(temp);
curNode = curNode.right();
}
//System.out.println(curNode.isExternal()); //Debug
}
}
}
@Override
public Entry<K, V> peek() {
List<Position<Entry<K, V>>> heap = levelOrder();
if(size == 0){
return null;
}
System.out.println(heap.get(0));
return heap.get(0).element();
}
@Override
public void printHeap() {
Position<Entry<K, V>> curNode = root;
List<Position<Entry<K, V>>> heap = levelOrder();
int count = 1;
for(Position<Entry<K, V>> x : heap){
System.out.print(curNode.toString() + " ");
}
}
@Override
public Entry<K, V> remove() {
Entry<K, V> rootElemet = root.element();
List<Position<Entry<K, V>>> heap = levelOrder();
Position<Entry<K, V>> lastNode = heap.get(size()-1);
root.replace(lastNode.element());
if(size() % 2 == 0){
lastNode.parent().setLeft(new ENode<>());
}
else{
lastNode.parent().setRight(new ENode<>());
}
downHeap();
return rootElemet;
}
@Override
public int size() {
return size;
}
}