Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

BinaryHeap.remove() breaks consistency #108

Open
antevir opened this issue Jul 13, 2016 · 1 comment
Open

BinaryHeap.remove() breaks consistency #108

antevir opened this issue Jul 13, 2016 · 1 comment
Labels

Comments

@antevir
Copy link

antevir commented Jul 13, 2016

The BinaryHeap consistency can break if an item is removed under specific conditions. The bug can be reproduced with the code below:

#include "core-util/BinaryHeap.h"

using mbed::util::BinaryHeap;
using mbed::util::MinCompare;

BinaryHeap<int, MinCompare<int>> heap;

void testHeap()
{
    UAllocTraits_t traits = { 0 };
    int values[] = { 59, 46, 79, 7, 62, 6, 8, 10, 36, 11, 14, 70, 61, 1 };

    heap.init(20, 20, traits);

    for (int i = 0; i < sizeof(values) / sizeof(int); i++) {
        heap.insert(values[i]);
    }

    heap.remove(11);

    while (!heap.is_empty()) {
        printf("%d ", heap.pop_root());
    }
}

The above code should print out all the values in rising order, i.e. the expected output is:

1 6 7 8 10 14 36 46 59 61 62 70 79

However, the actual output is:

1 6 7 10 8 14 36 46 59 61 62 70 79

It looks like the algorithm in remove() is not correct

@ciarmcom
Copy link
Member

ARM Internal Ref: IOTSFW-2788

antevir pushed a commit to hms-networks/core-util that referenced this issue Jul 13, 2016
When last element was swapped with the removed element it was only propagated downwards.
Depending if the swapped value is greater or smaller than its parent it may instead
need to propagate upwards.

Fixes ARMmbed#108

Change-Id: I9583a3412ccb88166d4e3091ed31a4c8a3aa4486
antevir pushed a commit to hms-networks/core-util that referenced this issue Jul 19, 2016
When last element was swapped with the removed element it was only propagated downwards.
Depending if the swapped value is greater or smaller than its parent it may instead
need to propagate upwards.

Fixes ARMmbed#108

Change-Id: I9583a3412ccb88166d4e3091ed31a4c8a3aa4486
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants