-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.h
197 lines (160 loc) · 3.89 KB
/
Node.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef ADS_NODE_H
#define ADS_NODE_H
#include <stddef.h>
namespace ADS
{
enum NodeType {
Simple,
BinomialHeap,
FibonacciHeap
};
/* Simple node that wraps an element */
template<class T>
struct Node
{
Node (T element);
virtual ~Node (void);
NodeType getType (void);
T el;
Node<T> *pRightSibling;
protected:
Node (NodeType nodeType, T element);
private:
NodeType type;
};
/* node for binomial heap */
template<class T>
struct BNode : public Node<T>
{
/* Creates a Node which is sibling of itself */
BNode (T element);
virtual ~BNode (void);
/* Returns the next sibling of the element */
unsigned int getChildCount (void);
BNode<T> * getSibling (void);
unsigned int getSiblingCount (void);
void setSibling (BNode<T> *pSibling);
/* Resets all the pointers to NULL */
void reset (void);
void resetSibling (void);
BNode<T> *pParent;
BNode<T> *pFirstChild;
unsigned int uiDegree;
protected:
BNode (NodeType nodeType, T element);
};
/* node for Fibonacci heap */
template<class T>
struct FNode : public BNode<T>
{
/* Creates a Node which is sibling of itself */
FNode (T element);
~FNode (void);
/*
* Reset all the pointers to NULL
*/
void reset (void);
bool bCut;
FNode<T> *pLeftSibling;
};
/*************************************************************************/
template<class T>
Node<T>::Node (T element)
{
type = Simple;
el = element;
pRightSibling = NULL;
}
template<class T>
Node<T>::Node (NodeType nodeType, T element)
{
type = nodeType;
el = element;
pRightSibling = NULL;
}
template<class T>
Node<T>::~Node()
{
}
template<class T>
NodeType Node<T>::getType()
{
return type;
}
/*************************************************************************/
template<class T>
BNode<T>::BNode (T element)
: Node<T> (BinomialHeap, element)
{
reset();
}
template<class T>
BNode<T>::BNode (NodeType nodeType, T element)
: Node<T> (nodeType, element)
{
reset();
}
template<class T>
BNode<T>::~BNode()
{
Node<T>::pRightSibling = NULL;
}
template<class T>
unsigned int BNode<T>::getChildCount()
{
if (pFirstChild == NULL)
return 0;
return (1 + pFirstChild->getSiblingCount());
}
template<class T>
BNode<T> * BNode<T>::getSibling()
{
return (BNode<T> *) Node<T>::pRightSibling;
}
template<class T>
unsigned int BNode<T>::getSiblingCount()
{
unsigned int uiCount = 0;
for (BNode<T> *pCurrNode = getSibling(); pCurrNode != this;
pCurrNode = pCurrNode->getSibling())
uiCount++;
return uiCount;
}
template<class T>
void BNode<T>::setSibling (BNode<T> *pSibling)
{
Node<T>::pRightSibling = pSibling;
}
template<class T>
void BNode<T>::reset()
{
pParent = pFirstChild = NULL;
resetSibling();
uiDegree = 0;
}
template<class T>
void BNode<T>::resetSibling()
{
Node<T>::pRightSibling = this;
}
/*************************************************************************/
template<class T>
FNode<T>::FNode (T element)
: BNode<T> (FibonacciHeap, element)
{
reset();
}
template<class T>
FNode<T>::~FNode()
{
pLeftSibling = NULL;
}
template<class T>
void FNode<T>::reset()
{
BNode<T>::reset();
pLeftSibling = this;
bCut = false;
}
}
#endif /* ADS_NODE_H */