我试过这个问题但是在解决这个算法时会出现一些错误。 [英] I tried this question but there is some error occur while solving this algorithm.

查看:62
本文介绍了我试过这个问题但是在解决这个算法时会出现一些错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个新的B树操作COMBINE(T1,T2,k)。该操作将具有相同最小度数参数t的两个B树T1和T2作为输入,加上未出现在T1或T2中的新密钥k。我们假设T1中的所有密钥都严格小于k,T2中的所有密钥都严格大于k。 COMBINE操作产生一个新的B树T,具有相同的最小度t,其密钥是T1中的那些,T2中的密钥加上k。在此过程中,它会破坏原始树T1和T2。在这个问题中,您将设计一个算法来实现COMBINE操作。您的算法应该在时间O(| h1 - h2 | + 1)中运行,其中h1和h2分别是树T1和T2的高度。在分析成本时,您应该将t视为常数。首先考虑问题的特殊情况,其中假设h1等于h2。给出一个算法来组合在恒定时间内运行的树。



我尝试过:



数组a [0..n-1];

键k开始l←0;

r←n-1

而l≤r

do m←n

(l + r)/ 2

如果

a [m]< k

然后

l←m + 1

否则如果

a [m]> ; k

然后

r←m-1

否则返回

m

end如果

结束时

返回

未找到

Consider a new B-tree operation COMBINE (T1,T2,k). This operation takes as input two B-trees T1 and T2 with the same minimum degree parameter t, plus a new key k that does not appear in either T1 or T2. We assume that all the keys in T1 are strictly smaller than k and all the keys in T2 are strictly larger than k. The COMBINE operation produces a new B-tree T , with the same minimum degree t, whose keys are those in T1, those in T2, plus k. In the process, it destroys the original trees T1 and T2. In this problem, you will design an algorithm to implement the COMBINE operation. Your algorithm should run in time O(|h1 − h2| + 1), where h1 and h2 are the heights of trees T1 and T2 respectively. In analyzing the costs, you should regard t as a constant. First consider the special case of the problem in which h1 is assumed to be equal to h2. Give an algorithm to combine the trees that runs in constant time.

What I have tried:

array a[0..n−1];
key k begin l ← 0;
r ← n−1
while l ≤ r
do m ← n
(l+r)/2
if
a[m]<k
then
l←m+1
else if
a[m]>k
then
r←m−1
else return
m
end if
end while
return
not found

推荐答案

看起来像你有一个高级的功课要做,但真的没有头绪。



在这里你找到一些关于 C ++中的二进制树



在您编写解决方案代码之前,您必须通过 Learn C ++ 或您的语言来提高您的技能正在编码。
Looks like you have an advanced homework to do, but really no clue.

Here you find some tutorial about binary tree in C++.

Before you can code your solution you must enhance your skills by Learn C++ or the language you are coding.


这篇关于我试过这个问题但是在解决这个算法时会出现一些错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆