Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Heap] Improved consistency checking #69

Closed
lorentey opened this issue Aug 7, 2021 · 1 comment · Fixed by #78
Closed

[Heap] Improved consistency checking #69

lorentey opened this issue Aug 7, 2021 · 1 comment · Fixed by #78
Labels
enhancement New feature or request Heap Min-max heap module

Comments

@lorentey
Copy link
Member

lorentey commented Aug 7, 2021

The current Heap._checkInvariants method doesn't fully validate the min-max invariants -- it just checks to see if the property holds for the immediate children of any node.

Instead, we should compare the value stored in every node with a currently expected range of values.

E.g., in the tree below, when checking descendants of 11, each value needs to be within the range 11 ... 41. Each new level narrows this range a bit further (by refining the lower or upper bounds, depending on the polarity of the node).

 level 0:                 8
 level 1:         71              41
 level 2:     31      10      11      16
 level 3:   46  51  31  21  13

Implementing this would mean that we'd have to keep track of a stack of expected ranges in addition to their associated indices. (Or switch to a recursive implementation.)

@lorentey
Copy link
Member Author

Actually, I misread the original implementation -- it did verify the full min-max property, it was just doing it slower than necessary, by iterating over each node's entire subtree, rather than visiting each node exactly once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Heap Min-max heap module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant