检查最小-最大堆 java 的奇偶级别

发布于 2024-11-25 13:49:34 字数 611 浏览 5 评论 0原文

这是我编写的一个方法,用于确定数组的给定索引是否表示堆的最大级别或最小级别,其中最小级别具有偶数深度(包括 0),最大级别具有奇数深度。它工作得很好,但它的运行时间是(我认为)O(log N)。有没有更有效的方法来做到这一点,例如具有恒定运行时间的简单数学计算?请注意,此方法假设数据从数组的索引 1 开始,而不是从索引 0 开始。

    private boolean isMaxLevel(int i)
    {
    int border = 1;
    int prev = 1;
    int count = 1;
    boolean isMax = false;
    // alternates boolean between true and false as each level is checked.
    while (true)
        {
        if (i >= prev && i <= border)
            return isMax;
        isMax = !isMax;
        prev = border + 1;
        count *= 2;
        border += count;
    }
}

So this is a method I wrote to determine if a given index of an array represents a max level or a min level of a heap, where min level has even depth (including 0), max level has odd depth. It works fine, but its run time is (I think) O(log N). Is there a more efficient way of doing this like a simple math calculation that has a constant run time? Note that this method assumes the data starts at index 1 of the array, not index 0.

    private boolean isMaxLevel(int i)
    {
    int border = 1;
    int prev = 1;
    int count = 1;
    boolean isMax = false;
    // alternates boolean between true and false as each level is checked.
    while (true)
        {
        if (i >= prev && i <= border)
            return isMax;
        isMax = !isMax;
        prev = border + 1;
        count *= 2;
        border += count;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经暖 2024-12-02 13:49:34

我不清楚你的要求是什么,但这可能对你有帮助。

public static void main(String... args) {
    for (int i = 1; i <= 256; i *= 2) {
        System.out.println((i - 1) + ": " + isOddHighestBit(i - 1));
        System.out.println(i + ": " + isOddHighestBit(i));
    }
}

public static boolean isOddHighestBit(int i) {
    return (Double.doubleToRawLongBits(i) >> 52) % 2 == 0;
}

印刷

0: true
1: false
1: false
2: true
3: true
4: false
7: false
8: true
15: true
16: false
31: false
32: true
63: true
64: false
127: false
128: true
255: true
256: false

Its not clear to me what your requirement is but this might help you.

public static void main(String... args) {
    for (int i = 1; i <= 256; i *= 2) {
        System.out.println((i - 1) + ": " + isOddHighestBit(i - 1));
        System.out.println(i + ": " + isOddHighestBit(i));
    }
}

public static boolean isOddHighestBit(int i) {
    return (Double.doubleToRawLongBits(i) >> 52) % 2 == 0;
}

prints

0: true
1: false
1: false
2: true
3: true
4: false
7: false
8: true
15: true
16: false
31: false
32: true
63: true
64: false
127: false
128: true
255: true
256: false
汐鸠 2024-12-02 13:49:34

我不确定这是否更快,它可能取决于实现,但是:

private boolean isMaxLevel(int i){
    if(((int)Math.log(i+1)/Math.log(2)))%2 == 1)
        return true;
    return false;
}

返回正确的值。从数学上考虑一下。您想找出以下关于 i 的 n 项是正确的。如果 n 是奇数,则有最高级别,如果 n 是偶数,则有最低级别。

2ⁿ-1 <= i < 2ⁿ⁺ⁱ-1

求解 n 我们得到:

2ⁿ-1 <= i < 2ⁿ⁺ⁱ-1
2ⁿ <= i+1 < 2ⁿ⁺ⁱ
n <= log₂(i+1) < n+1
n = floor(log₂(i+1))

然后我们可以测试 n 是否为奇数 (n%2 == 1)

I'm not sure if this in any faster, it's probably implementation dependent, but:

private boolean isMaxLevel(int i){
    if(((int)Math.log(i+1)/Math.log(2)))%2 == 1)
        return true;
    return false;
}

Returns the correct value. Think of it mathematically. You want to find out for which n the following is true about i. If n is odd you have a max level, if n is even you have a min level.

2ⁿ-1 <= i < 2ⁿ⁺ⁱ-1

Solving this for n we get:

2ⁿ-1 <= i < 2ⁿ⁺ⁱ-1
2ⁿ <= i+1 < 2ⁿ⁺ⁱ
n <= log₂(i+1) < n+1
n = floor(log₂(i+1))

Then we can just test if n is odd (n%2 == 1)

沉睡月亮 2024-12-02 13:49:34

我们必须检测最小级别和最大级别范围

1 => pow(2,0) --> 最低等级

2-3 => pow(2,1) 到 pow(2,2)-1 --> 最高级别

4-7 => pow(2,2) 到 pow(2,3)-1 --> 最低级别

8-15 => pow(2,3) 到 pow(2,4)-1 --> 最大级别

c 实现

#include<math.h>
#define bool int
#define true 1
#define false 0
int isMinLevel(int i,int n)//i is on min level or not
{
  int h=2;
  if(i==1)
    return true;
  while(true)
    {
      if(i>=pow(2,h)&&i<=pow(2,h+1)-1)
    return true;
      else if(i>n||i<pow(2,h))
    return false;
      h+=2;
    }
}

we have to detect the min level and max level ranges

1 => pow(2,0) -->min level

2-3 => pow(2,1) to pow(2,2)-1 -->max level

4-7 => pow(2,2) to pow(2,3)-1 -->min level

8-15 => pow(2,3) to pow(2,4)-1 -->max level

c implementation

#include<math.h>
#define bool int
#define true 1
#define false 0
int isMinLevel(int i,int n)//i is on min level or not
{
  int h=2;
  if(i==1)
    return true;
  while(true)
    {
      if(i>=pow(2,h)&&i<=pow(2,h+1)-1)
    return true;
      else if(i>n||i<pow(2,h))
    return false;
      h+=2;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文