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

Fixed problem with BinaryHeap::remove()#109

Open
antevir wants to merge 1 commit into
ARMmbed:masterfrom
hms-networks:binheap_remove_fix
Open

Fixed problem with BinaryHeap::remove()#109
antevir wants to merge 1 commit into
ARMmbed:masterfrom
hms-networks:binheap_remove_fix

Conversation

@antevir

@antevir antevir commented Jul 13, 2016

Copy link
Copy Markdown

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 #108

@antevir

antevir commented Jul 13, 2016

Copy link
Copy Markdown
Author

I have tested the modification with the function below:

#include "core-util/BinaryHeap.h"

void testHeap()
{
    UAllocTraits_t traits = { 0 };
    mbed::util::BinaryHeap<int, mbed::util::MinCompare<int>> heap;
    heap.init(40, 40, traits);

    while (true) {
        int values[40];
        int cnt = 10 + (rand() % 30);

        for (int i = 0; i < cnt; i++) {
            int value = rand() % 100;
            values[i] = value;
            heap.insert(values[i]);
        }

        int remCnt = rand() % 5;
        for (int i = 0; i < remCnt; i++) {
            int remValue = values[rand() % cnt];
            heap.remove(remValue);
        }

        int lastValue = -1;
        while (!heap.is_empty()) {
            int value = heap.pop_root();
            if (value < lastValue)
                printf("ERROR\n");
            lastValue = value;
        }
    }
}

@0xc0170

0xc0170 commented Jul 13, 2016

Copy link
Copy Markdown
Contributor

Thanks for providing the fix and the test case

cc @bogdanm

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 antevir force-pushed the binheap_remove_fix branch from f510f04 to 4d63cd4 Compare July 19, 2016 14:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants