/
pseudoKodRemoveReplace
60 lines (55 loc) · 1.35 KB
/
pseudoKodRemoveReplace
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
remove(e)
k <-- heapArray(size-1)
size--
position <-- e.position // Ta bort e ur #-tabellen
heapArray(position) <-- k // Uppdatera k i#-tabellen
BubbleSort(position, e.getKey)
replaceKey(e, k)
position <-- e.position
oldKey <-- e.key
e.key <-- k
BubbleSort(position, oldKey)
BubbleSort(position, oldKey) // För minsta elementet överst
e <-- heapArray(position)
key <-- e.getKey
if key < oldKey
BubbleSortUp(position, key)
else if key > oldKey
BubbleSortDown(position, key)
fi
return
BubbleSortUp(position, key)
if position == 0
return
fi
if "position.parent".getKey =< key
return
fi
changePlace(position, "position.parent")
BubbleSortUp("position.parent", key)
// är osäker på vad jag ska ange för position
return
BubbleSortDown(position, key)
if position == 0
return
fi
if "position.leftChild".getKey =< "position.rightChild".getKey
if "position.leftChild".getKey > key
return
fi
changePlace(position, "position.leftChild")
BubbleSortDown("posistion.leftChild", key)
// återigen, osäker på argumenten
return
fi
if "position.rightChild".getKey >= key
return
fi
changePlace(position, "position.rightChild")
BubbleSortDown("position.rightChild", key)
return
changePlace(firstP, secondP)
// jag tror det borde vara något i stil med det här
temp = tree[secondP]
tree[secondP] = tree[firstP]
tree[firstP] = temp