免責事項:「優先ツリー」のあなたの定義は、単純な分、ヒープです。ですから、基本的には、2つのヒープを1つにマージするだけです。
単純なアプローチは次のとおりです。小さなルートでヒープを取り出し、もう一方のヒープをその中にマージします。挿入されるヒープのルートから開始し、ノードが挿入するヒープのルートよりも大きな値を持つまで、任意のパスに従います。そのノードを挿入するヒープに置き換えます。今度は新しいヒープ(ヒープから削除したヒープ)を挿入します。挿入するヒープが最後に空になるまで、ちょうど挿入したノードから上記の作業を進めてください。 N
ヒープの根及びL
でありR
それぞれヒープがaswellであり、左右の子供である場合
//if at least one heap is empty, the job is already done
if H1.isEmpty()
return H2
if H2.isEmpty()
return H1
//select heap to insert into and heap to be inserted
node insInto, toIns, prntInsInto
if H1.getRoot() > H2.getRoot()
insInto = H2.getRoot()
toIns = H1.getRout()
else
insInto = H1.getRoot()
toIns = H2.getRoot()
heap result = insInto
//merge
while toIns != null
if insInto == null
//we've run to the lower end of the heap to insert into
//just append the heap to insert and youre done
prntInsInto.setLeftChild(toIns)
return result
if insInto > toIns
//replace node in heap into which is insert with the root of the heap to insert
prntInsInto.replace(insInto , toIns)
//removed subtree becomes new heap to insert
//and inserted subtree becomes new heap into which is inserted
node tmp = toIns
toIns = insInto
insInto = tmp
//I selected the left child for the search, this is
//completely random, you could even replace it with the
//larger of the two children for better efficiency
prntInsInto = insInto
insInto = insInto.leftChild()
return result
このアルゴリズムは、その分、ヒープをH = (N , L , R)
のように再帰的に定義することができる、という事実を利用します。
これは最悪のキャストO(log |H1| + log |H2|)
で実行されます。もっと早くすることはできません。
編集:
ヒープが配列として格納されているとのコメントに気付きました。この場合、ランタイムを達成するためにはすべての要素をトラバースする必要があるため、対数ランタイムは単純に不可能です。したがって、ですでに存在するデータ構造を操作できるという事実に依存しているため、O(|H1| + |H2|)
はその場合に可能な限り最善の方法です。
どのようにデータが保存されていますか? –
アレイに格納されています –