Skip to content
Permalink
Browse files
added the FibonacciHeap#moveToRoot( FibonacciHeapNode<E> node ) metho…
…d and fixed wrong assumptions in add(), consolidate() and cut() methods
  • Loading branch information
simonetripodi committed Jul 4, 2012
1 parent a500965 commit 1b35dd4732120e9402c3f7636c3406c18ae5941f
Showing 1 changed file with 37 additions and 27 deletions.
@@ -104,29 +104,15 @@ public FibonacciHeap( /* @Nullable */Comparator<? super E> comparator )
}

/**
* {@inheritDoc}
* Moves the target node in the {@code H} root nodes.
*
* <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 moved in the {@code H} root nodes
* @see #add(Object)
* @see #consolidate()
* @see #cut(FibonacciHeapNode, FibonacciHeapNode)
*/
public boolean add( E e )
private void moveToRoot( FibonacciHeapNode<E> node )
{
if ( e == null )
{
throw new NullPointerException();
}

// 1-6 performed in the node initialization
FibonacciHeapNode<E> node = new FibonacciHeapNode<E>( e );
// 8' if min[H] = NIL
if ( isEmpty() )
{
@@ -148,6 +134,35 @@ public boolean add( E e )
minimumNode = node;
}
}
}

/**
* {@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 )
{
if ( e == null )
{
throw new NullPointerException();
}

// 1-6 performed in the node initialization
FibonacciHeapNode<E> node = new FibonacciHeapNode<E>( e );

// 7-9 performed in the #moveToRoot( FibonacciHeapNode<E> ) method
moveToRoot( node );

// 10 n[H] <- n[H] + 1
size++;
@@ -504,12 +519,7 @@ private void consolidate()
// 16 if A[i] != NIL
if ( pointer != null )
{
// 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() );
moveToRoot( pointer );
}
}
}
@@ -560,7 +570,7 @@ private void cut( FibonacciHeapNode<E> x, FibonacciHeapNode<E> y )
y.decraeseDegree();

// add x to the root list of H
// TODO!!!
moveToRoot( x );

// p[x] <- NIL
x.setParent( null );

0 comments on commit 1b35dd4

Please sign in to comment.