Skip to content
Permalink
Browse files
heap node initialization in the node itself (dropped the ugly 'reset …
…degree' method)

still working on the CONSOLIDATE method
  • Loading branch information
simonetripodi committed Jun 28, 2012
1 parent d8f3ac6 commit 05b47876e4ba95a616f57c25aae6ebbee1bff64e
Showing 2 changed files with 60 additions and 73 deletions.
@@ -105,6 +105,18 @@ public FibonacciHeap( /* @Nullable */Comparator<? super E> comparator )

/**
* {@inheritDoc}
*
* <pre>FIB-HEAP-INSERT(H, x)
* 1 degree[x] &larr; 0
* 2 p[x] &larr; NIL
* 3 child[x] &larr; NIL
* 4 left[x] &larr; x
* 5 right[x] &larr; x
* 6 mark[x] &larr; FALSE
* 7 concatenate the root list containing x with root list H
* 8 if min[H] = NIL or key[x] &lt; key[min[H]]
* 9 then min[H] &larr; x
* 10 n[H] &larr; n[H] + 1</pre>
*/
public boolean add( E e )
{
@@ -113,7 +125,32 @@ public boolean add( E e )
throw new NullPointerException();
}

insert( new FibonacciHeapNode<E>( e ) );
// 1-6 performed in the node initialization
FibonacciHeapNode<E> node = new FibonacciHeapNode<E>( e );
// 8' if min[H] = NIL
if ( isEmpty() )
{
// then min[H] <- x
minimumNode = node;
}
else
{
// 7 concatenate the root list containing x with root list H
minimumNode.getLeft().setRight( node );
node.setLeft( minimumNode.getLeft() );
node.setRight( minimumNode );
minimumNode.setLeft( node );

// 8'' if key[x] < key[min[H]]
if ( compare( node, minimumNode ) < 0 )
{
// 9 then min[H] <- x
minimumNode = node;
}
}

// 10 n[H] <- n[H] + 1
size++;

elementsIndex.add( e );

@@ -454,8 +491,12 @@ private void consolidate()
// 16 if A[i] != NIL
if ( pointer != null )
{
// FIXME this is totally wrong!!!
insert( pointer );
// TODO 17 then add A[i] to the root list of H
// TODO 18 if min[H] = NIL or key[A[i]] &le; key[min[H]]
// TODO 19 then min[H] &larr; A[i]</pre>

// FIXME this should be wrong
add( pointer.getElement() );
}
}
}
@@ -563,66 +604,6 @@ public int potential()
return trees + 2 * markedNodes;
}

/**
* Adds a node in the current structure.
*
* <pre>FIB-HEAP-INSERT(H, x)
* 1 degree[x] &larr; 0
* 2 p[x] &larr; NIL
* 3 child[x] &larr; NIL
* 4 left[x] &larr; x
* 5 right[x] &larr; x
* 6 mark[x] &larr; FALSE
* 7 concatenate the root list containing x with root list H
* 8 if min[H] = NIL or key[x] &lt; key[min[H]]
* 9 then min[H] &larr; x
* 10 n[H] &larr; n[H] + 1</pre>
*
* @param node the node has to be added.
* @see #offer(Object)
* @see #add(Object)
*/
private void insert( FibonacciHeapNode<E> node )
{
// 1 degree[x] &larr; 0
node.resetDegree();
// 2 p[x] <- NIL
node.setParent( null );
// 3 child[x] <- NIL
node.setChild( null );
// 4 left[x] <- x
node.setLeft( node );
// 5 right[x] <- x
node.setRight( node );
// 6 mark[x] <- FALSE
node.setMarked( false );

// 8' if min[H] = NIL
if ( isEmpty() )
{
// then min[H] <- x
minimumNode = node;
}
else
{
// 7 concatenate the root list containing x with root list H
minimumNode.getLeft().setRight( node );
node.setLeft( minimumNode.getLeft() );
node.setRight( minimumNode );
minimumNode.setLeft( node );

// 8'' if key[x] < key[min[H]]
if ( compare( node, minimumNode ) < 0 )
{
// 9 then min[H] <- x
minimumNode = node;
}
}

// 10 n[H] <- n[H] + 1
size++;
}

/**
* Compare the given objects according to to the specified comparator if not null,
* according to their natural ordering otherwise.
@@ -55,13 +55,13 @@
/**
* The number of children in the child list of node {@code x} is stored in {@code degree[x]}.
*/
private int degree = 0;
private int degree;

/**
* {@code mark[x]} indicates whether node {@code x} has lost a child since
* the last time {@code x} was made the child of another node.
*/
private boolean marked = false;
private boolean marked;

/**
* Build a new {@link FibonacciHeap} node with the given value.
@@ -70,6 +70,20 @@
*/
public FibonacciHeapNode( E element )
{
// 1 degree[x] &larr; 0
degree = 0;
// 2 p[x] <- NIL
setParent( null );
// 3 child[x] <- NIL
setChild( null );
// 4 left[x] <- x
setLeft( this );
// 5 right[x] <- x
setRight( this );
// 6 mark[x] <- FALSE
setMarked( false );

// set the adapted element
this.element = element;
}

@@ -163,14 +177,6 @@ public int getDegree()
return degree;
}

/**
* Sets the number of children in the child list to {@code 0}.
*/
public void resetDegree()
{
degree = 0;
}

/**
* Increases the degree of current node.
*

0 comments on commit 05b4787

Please sign in to comment.