-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpp
81 lines (68 loc) · 1.84 KB
/
cpp
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
#include "pairingHeap.h"
#include "error.h"
using namespace::std;
PairingHeap::PairingHeap(){
logicalSize = 0;
origin = NULL;
}
PairingHeap::~PairingHeap(){
deleteAll(origin);
}
void PairingHeap::deleteAll(PairNode *node){
if (node->leftChild != NULL) deleteAll(node->leftChild);
if (node->sibling != NULL) deleteAll(node->sibling);
delete node;
}
void PairingHeap::insert(string name, int value){
PairNode *node = new PairNode(name, value, NULL, NULL);
logicalSize++;
if (origin == NULL){
origin = node;
} else {
origin = mergeTrees(origin, node);
}
}
bool PairingHeap::isEmpty() const{
return logicalSize == 0;
}
PairNode *PairingHeap::mergeTrees(PairNode *first, PairNode *second){
// if (first->value == NULL) return second;
// if (second->value == NULL) return first;
if (first->value <= second->value){
first->addChild(second);
return first;
} else {
second->addChild(first);
return second;
}
}
PairNode PairingHeap::peek() const{
if (origin == NULL){
error("Tree is empty");
} else {
return *origin;
}
}
void PairingHeap::printOut() const{
for (PairNode *node = origin; node != NULL;node = node->leftChild){
cout << node->name << ":" << node->value << endl;
}
}
PairNode *PairingHeap::remove(){
logicalSize--;
PairNode *topValue = origin;
origin = mergeSiblings(origin->leftChild);
return topValue;
}
// use after deleting origin
PairNode *PairingHeap::mergeSiblings(PairNode *node){
if (node == NULL || node->sibling == NULL) {
return node;
} else {
PairNode *first, *second, *newNode;
first = node;
second = node->leftChild;
newNode = node->leftChild->leftChild;
return mergeTrees(first, second), mergeSiblings(newNode);
}
}