标签: heap

使用链表实现二叉堆

可能的重复:
二进制最小堆的链表实现(操作遇到问题......)

问候,

我无法找出一种算法来为我提供二叉堆的链表实现中树节点的位置。我已经使用数组实现了堆,现在我想尝试使用链表;如果我使用数组来表示堆,有没有办法找到其数组索引为 i 的树节点?

java algorithm heap binary-tree

0
推荐指数
1
解决办法
9641
查看次数

如何对堆对象的更改进行引理?

我正在尝试根据 Intro 中的代码使用 Dafny 实现 MaxHeap。参见《算法》,CLRS 第 3 版,第 6.1 节,第 153 页或此处的Max-Heapify 函数。我从使用递归切换到 while 循环,因为这在 Dafny 中似乎更容易处理。

调用 heapify 函数后尝试证明数组的堆属性。特别是,我希望能够使用 heapify 上的 require 语句来断言堆中未更改且在更新之前满足堆属性的三元组在更新后也满足堆属性。

然而,对数组进行任何更改后,它似乎忘记了所有有关 require/invariant 语句的信息。即使我表明更新前后的值相同,它仍然不再通过断言。我将更新提取到交换方法中。

我希望我可以写一个引理来断言这个事实,但似乎引理不允许修改堆或使用 old() 或调用方法。有没有办法为此写一个引理?

function method parent(i: int): int 
{
    i/2
}

function method left(i: int): int
{
    2*i
}

function method right(i: int): int
{
    2*i+1
}


class MaxHeap {
    var data: array<int>
    ghost var repr: set<object>

    constructor(data: array<int>) 
        ensures this.data == data
        ensures this in repr
    {
        this.data := …
Run Code Online (Sandbox Code Playgroud)

heap dafny

0
推荐指数
1
解决办法
220
查看次数

调整堆大小时 C 中的 realloc 错误

我想在堆的插入函数中使用 C 中的 realloc。这是代码:

\n
typedef struct MaxHeap {\n    int size;\n    int* heap;\n} MaxHeap;\n\nvoid max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n    max_heap->heap = realloc((max_heap->size+1), sizeof * max_heap->heap);\n    max_heap[max_heap->size] = N_INF;\n    max_heap->size += 1;\n    increase_key(max_heap, key_index, max_heap->size, key)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我收到这个警告:

\n

warning: passing argument 1 of \xe2\x80\x98realloc\xe2\x80\x99 makes pointer from integer without a cast [-Wint-conversion]我尝试了这个修复:

\n
max_heap->heap = realloc((max_heap->heap), (max_heap->size+1) * sizeof(*(max_heap->heap)));\n\n
Run Code Online (Sandbox Code Playgroud)\n

更新

\n

我这样做了:

\n
void max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n    int* …
Run Code Online (Sandbox Code Playgroud)

c heap

0
推荐指数
1
解决办法
86
查看次数

检测到堆损坏:正常阻塞后(#62)

我代码

#include<stdio.h>
#include<conio.h>

void Nhap(int *x, int y)
{
    for(int i=0; i<y; i++)
        {
            printf("x[%d] = ",i);
            scanf("%d",&x[i]);
        }
}

void Chen(int *a, int *b, int n, int m, int k)
{
    int c[100];
    int x=0;
    for(int i=k; i<n; i++)
    {
        c[x]=a[i];
        x++;
    }
    c[x]='\0';
    x=0;
    for(int i=k; i<m+k; i++)
        {
    a[i]=b[x];
    x++;
        }
    x=0;
    for(int i=k+m; i<m+n; i++)
    {
        a[i]=c[x];
        x++;
    }
    for(int i=0; i<m+n; i++)
        printf("%2d",a[i]);
}


void main()
{
    int m, n, k=0;
    printf("Enter element of b: …
Run Code Online (Sandbox Code Playgroud)

c++ heap

-1
推荐指数
1
解决办法
399
查看次数

字符串不可变?

有人能回答我吗?

public class ReplaceString{

    public static void main(String arg[]) {
        String a = "Hariom";
        a = a.replace('H', 'b');
        System.out.println(a);
    }
}
Run Code Online (Sandbox Code Playgroud)

java string heap stack immutability

-1
推荐指数
2
解决办法
237
查看次数

内存是分配:堆还是堆栈?

#include <iostream>
using namespace std;

struct number
{
    int value;
    int pos;
public:
    number(int a,int b)
    {
        value=a;
        pos=b;
    }
};
int main() {
    // your code goes here
    number(1,2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,在哪里分配内存number(1,2)?在堆或堆栈上?我们如何验证它?

c++ memory heap stack

-1
推荐指数
1
解决办法
87
查看次数

将值分配给太多动态分配的数组时程序崩溃 - C++

我的程序中有大约+30个动态分配的数组,每个区域的定义如下:

int Nx = 240;
int Ny = 240;
double* array = new double(Nx*Ny);
Run Code Online (Sandbox Code Playgroud)

我可以为其中的16个分配值,但是一旦我到达第17个,它就会抛出seg错误!

这是抛出它的代码,完全没问题!

for (int i = 0; i < Nx*Ny; i++) {
    array[i] = 0;
}
Run Code Online (Sandbox Code Playgroud)

我真的不知道为什么,我想要用完堆,但因为我有4GB的RAM应该是不可能的!我正在使用MSVS15并在Windows 10上运行该程序!

知道为什么会这样吗?确切的错误:

在ecostress.exe中0x00298389处抛出异常:0xC0000005:访问冲突写入位置0x01D2B000.如果存在此异常的处理程序,则可以安全地继续该程序.

c++ heap visual-studio

-1
推荐指数
1
解决办法
42
查看次数

"=="如何对原始类型起作用

我想知道"=="运算符如何处理原始值.我理解"=="检查两个引用是否引用堆上的同一个对象.但它如何在原始值的上下文中工作,它们是否存储在堆栈中?例如

int a = 5; int b = 5;

我假设这些值不存储在内存中的相同位置,但是== b返回"true".

我的想法是,JVM将存储在堆栈中的所有值视为存储在内存中的一个位置,并且在"=="的情况下返回true.你能用更详细的方式向我解释一下这件事吗?

问候

java heap stack jvm primitive-types

-1
推荐指数
1
解决办法
463
查看次数

malloc/free(),错误信号6

这是一个基本的堆栈实现代码.但是,它会产生信号中止.

int *arr;
int size = 2;
int top = 0;

int pop() {

    int i;
    if (top <= size / 4) {
        int *arr2 = (int*)malloc(sizeof(int) * size / 2);
        for ( i = 0; i < size; i++)
            arr2[i] = arr[i];
        free(arr);
        arr = arr2;
        size /= 2;
    }
    return arr[--top];
}

void push( int a) {

    int i;
    if (top >= size) {
        int *arr2 = (int*)malloc(sizeof(int)*size * 2);
        for ( i = 0; i < size; …
Run Code Online (Sandbox Code Playgroud)

c memory heap malloc free

-1
推荐指数
1
解决办法
511
查看次数

C++为什么非const数组声明不好?

如果我有以下两个陈述:

// OK
const int ARRAYSIZE = 5;
int x[ARRAYSIZE];

// NOT OK
int ARRAYSIZEBAD = 5;
int y[ARRAYSIZEBAD];
Run Code Online (Sandbox Code Playgroud)

我不用-pedantic-errors标志编译...为什么第二个例子是坏事?在什么情况下,最好使用new运算符进行动态分配?

c++ heap stack

-1
推荐指数
1
解决办法
187
查看次数