seek(u、v)関数を定義する必要があります。ここで、uはツリー内の新しいノード(検索を開始するノード)であり、vは新しいノードの下の子孫の数であり、この関数はインデックスを返します。最高のキー値の。ツリーにはBSTがなく、多くの子を持つノードが存在する可能性があります。例:
input:
5 // tree with 5 nodes
1 3 5 2 7 // the nodes' keys
1 2 // 1 and 2 are linked
2 3 // 2 and 3 are linked
1 4 // 1 and 4 are linked
3 5 // 3 and 5 are linked
4 // # of seek() requests
2 3 // index 2 of tree, which would be key 5, 3 descendants from index 3
4 1 // index 4 of tree, but only return highest from index 4 to 4 (it would
// return itself)
3 2 // index 3, next 2 descendants
3 2 // same
output:
5 // Returned index 5 because the 7 is the highest key from array[3 'til 5]
4 // Returned index 4 because the range is one, plus 4's children are null
5 // Returned index 5 because the 7 is the highest key from array[4 'til 5]
5 // Same as prior line
新しいルートを新しい赤黒木に入れることを考えていましたが、各ノードの後続または先行情報を効率的に保存する方法が見つかりません。配列に入れることも考えていますが、不均衡でソートされていないツリーの性質上、ツリーがソートされることを保証するものではありません。さらに、BSTではないため、順序どおりのツリーウォークを実行できません。特定の範囲から最高のキーを取得する方法について何か提案はありますか?