next up previous
Next: Up: Previous:

Operations

*
Union(H1, H2)

Union(H1, H2)
$\;\;\;\;\;$H = new heap containing trees of H1 and H2 merged in
$\;\;\;\;\;$ $\;\;\;\;\;$non-decreasing order by degree of root
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; Similar to Merge used in MergeSort
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; O(lg n): at most two roots of each degree,
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; $\;\;\;\;\;$ $\;\;\;\;\;$O(lg n) possible degrees
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; No more than 2 Bi trees in H at this point
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; Could be 3 after linking two Bi-1 trees together
$\;\;\;\;\;$prev-x = NIL $\;\;\;\;\;$; three-tree window
$\;\;\;\;\;$x = head(H) $\;\;\;\;\;$; look for:



\psfig{figure=figures/f13-3.ps}

$\;\;\;\;\;$next-x = sibling(x)
$\;\;\;\;\;$while next-x $\neq$ NIL
$\;\;\;\;\;$ $\;\;\;\;\;$if degree(x) $\neq$ degree(next-x) or
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$degree(x) = degree(next-x) = degree(sibling(next-x))
$\;\;\;\;\;$ $\;\;\;\;\;$then move window right by one
$\;\;\;\;\;$ $\;\;\;\;\;$else if key(x) $\leq$ key(next-x)
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$then:



\psfig{figure=figures/f13-4.ps}

$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$else:



\psfig{figure=figures/f13-5.ps}

$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$advance window



Running time = O(lg n) if n = n1 + n2 nodes in H.


next up previous
Next: Up: Previous: