-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.cpp
More file actions
155 lines (129 loc) · 3.76 KB
/
Copy pathsupport.cpp
File metadata and controls
155 lines (129 loc) · 3.76 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
150
151
152
153
154
155
/*
* support.cpp
*
* Created on: Oct 7, 2021
* Author: d-w-h
*/
#include <iostream>
#include <vector>
#include "memory.hpp"
#include "usertypes.hpp"
void printRootList(Node* z) {
Node* xt = z;
if(xt != NULL) {
do {
std::cout << "xt->key: " << xt->key;
std::cout << ", xt->degree: " << xt->degree << std::endl;
xt = xt->right;
} while(xt != z);
}
}
void printChildList(Node* child) {
Node* xt = child;
if(xt != NULL) {
do {
std::cout << "xt->child->key: " << xt->key;
std::cout << ", xt->child->degree: " << xt->degree << std::endl;
if(xt->child != NULL) {
std::cout << "xt->child->child->key: " << xt->child->key << std::endl;
}
xt = xt->right;
} while(xt != child);
}
}
void printList(Node* z) {
Node* xt = z;
if(xt != NULL) {
do {
std::cout << "xt->key: " << xt->key;
std::cout << ", xt->degree: " << xt->degree << std::endl;
if(xt->child != NULL) {
printChildList(xt->child);
}
xt = xt->right;
} while(xt != z);
}
}
bool numbersChildrenMatch(Node* z, int& numNodes1) {
bool numsMatch = true;
int numNodes = 0;
Node* xt = z->child;
if(xt != NULL) {
do {
numNodes++;
if(xt->child != NULL) {
numsMatch = numbersChildrenMatch(xt, numNodes1);
if(!numsMatch) { return false; }
}
xt = xt->right;
} while(xt != z->child);
numNodes1 = numNodes1 + numNodes;
if(numNodes == z->degree) { numsMatch = true; }
else { numsMatch = false; }
}
return numsMatch;
}
FibHeapProperties numbersMatch(Node* z) {
bool numsMatch = true;
int numNodes = 0;
FibHeapProperties props = { numsMatch, numNodes };
Node* xt = z;
if(xt != NULL) {
do {
numNodes++;
numsMatch = numbersChildrenMatch(xt, numNodes);
props.degreeIsEqualToNumChildren = numsMatch;
props.numNodes = numNodes;
if(!numsMatch) { return props; }
xt = xt->right;
} while(xt != z);
}
props.degreeIsEqualToNumChildren = numsMatch;
props.numNodes = numNodes;
return props;
}
bool childrenAreFibHeap(Node * z) {
bool isFibHeap = true;
Node* xt = z->child;
if(xt != NULL) {
do {
if(xt->p->key > xt->key) {
return isFibHeap = false;
}
if(xt->child != NULL) {
isFibHeap = childrenAreFibHeap(xt);
if(!isFibHeap) { return false; }
}
xt = xt->right;
} while(xt != z->child);
}
return isFibHeap;
}
bool isFibonacciHeap(Node * z) {
bool isFibHeap = true;
Node* xt = z;
if(xt != NULL) {
do {
isFibHeap = childrenAreFibHeap(xt);
if(!isFibHeap) { return false; }
xt = xt->right;
} while(xt != z);
}
return isFibHeap;
}
bool checkFibHeap(FibHeap * H) {
//This is the general test for the Fibonacci heap.
//The function returns true if the heap satisfies
//the Fibonacci heap properties
//Compute heap properties
FibHeapProperties props = numbersMatch(H->min);
bool isFibHeap = isFibonacciHeap(H->min);
//Check if number of children equal Node degrees
bool degreeIsEqualToNumChildren = props.degreeIsEqualToNumChildren;
//Check if number of Nodes counted in heap equals H.n
int numNodes = props.numNodes;
bool numNodesMatch = (numNodes == H->n);
//Check to see if heap is properly structured
bool heapIsOK = numNodesMatch && degreeIsEqualToNumChildren && isFibHeap;
return heapIsOK;
}