This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FibbonacciNode.cs
192 lines (179 loc) · 6 KB
/
FibbonacciNode.cs
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
using System;
using System.Collections.Generic;
using System.Text;
namespace SolucionAlumno
{
/// <summary>
/// Implements a node of the Fibonacci heap. It holds the information
/// necessary for maintaining the structure of the heap. It acts as
/// an opaque handle for the data element, and serves as the Key to
/// retrieving the data from the heap.
/// @author Nathan Fiedler
/// </summary>
/// <typeparam name="T"></typeparam>
class FibbonacciNode<T>
{
/** Data object for this node, holds the Key Value. */
private T value;
public T Value
{
get { return this.value; }
set { this.value = value; }
}
/** Key Value for this node. */
private long key;
public long Key
{
get { return key; }
set { key = value; }
}
/** Parent node. */
private FibbonacciNode<T> parent;
public FibbonacciNode<T> Parent
{
get { return parent; }
set { parent = value; }
}
/** First Child node. */
private FibbonacciNode<T> child;
public FibbonacciNode<T> Child
{
get { return child; }
set { child = value; }
}
/** Right sibling node. */
private FibbonacciNode<T> right;
public FibbonacciNode<T> Right
{
get { return right; }
set { right = value; }
}
/** Left sibling node. */
private FibbonacciNode<T> left;
public FibbonacciNode<T> Left
{
get { return left; }
set { left = value; }
}
/** Number of children of this node. */
private int degree;
public int Degree
{
get { return degree; }
set { degree = value; }
}
/** True if this node has had a Child removed since this node was
* added to its Parent. */
private bool mark;
public bool Mark
{
get { return mark; }
set { mark = value; }
}
/// <summary>
/// Constructor which sets the data field to the
/// passed arguments. It also initializes the Right and Left pointers,
/// making this a circular doubly-linked list.
/// </summary>
/// <param name="Value">data object to associate with this node</param>
public FibbonacciNode(T value, long key)
{
this.value = value;
this.key = key;
right = this;
left = this;
}
/// <summary>
/// Performs a cascading cut operation. Cuts this from its Parent
/// and then does the same for its Parent, and so on up the tree.
/// <p><em>Running time: O(log n)</em></p>
/// </summary>
/// <param name="min">the minimum heap node, to which nodes will be added.</param>
public void cascadingCut(FibbonacciNode<T> min)
{
FibbonacciNode<T> z = parent;
// if there's a Parent...
if (z != null)
{
if (mark)
{
// it's marked, cut it from Parent
z.cut(this, min);
// cut its Parent as well
z.cascadingCut(min);
}
else
{
// if y is unmarked, set it marked
mark = true;
}
}
}
/// <summary>
/// The reverse of the link operation: removes x from the Child
/// list of this node.
/// <p><em>Running time: O(1)</em></p>
/// </summary>
/// <param name="x">Child to be removed from this node's Child list</param>
/// <param name="min">the minimum heap node, to which x is added.</param>
public void cut(FibbonacciNode<T> x, FibbonacciNode<T> min)
{
// remove x from childlist and decrement Degree
x.left.right = x.right;
x.right.left = x.left;
degree--;
// reset Child if necessary
if (degree == 0)
{
child = null;
}
else if (child == x)
{
child = x.right;
}
// add x to root list of heap
x.right = min;
x.left = min.left;
min.left = x;
x.left.right = x;
// set Parent[x] to nil
x.parent = null;
// set Mark[x] to false
x.mark = false;
}
/// <summary>
/// Make this node a Child of the given Parent node. All linkages
/// are updated, the Degree of the Parent is incremented, and
/// Mark is set to false.
/// </summary>
/// <param name="Parent">the new Parent node.</param>
public void link(FibbonacciNode<T> parent)
{
// Note: putting this code here in FibbonacciNode<T>makes it 7x faster
// because it doesn't have to use generated accessor methods,
// which add a lot of time when called millions of times.
// remove this from its circular list
left.right = right;
right.left = left;
// make this a Child of x
this.parent = parent;
if (parent.child == null)
{
parent.child = this;
right = this;
left = this;
}
else
{
left = parent.child;
right = parent.child.right;
parent.child.right = this;
right.left = this;
}
// increase Degree[x]
parent.degree++;
// set Mark false
mark = false;
}
}
}