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

max value decisions are different between Heap.max and Heap.replaceMax #439

Open
iamhands0me opened this issue Dec 15, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@iamhands0me
Copy link

In the Heap module, if the left max value is same as the right max value, the max variable will return the right one but the replaceMax function will replace the left one

struct Fruit: Comparable {
    let name: String
    var price: Int
    
    static func <(lhs: Fruit, rhs: Fruit) -> Bool {
        lhs.price < rhs.price
    }
}

var heap = Heap([
    Fruit(name: "🍎", price: 1),
    Fruit(name: "🍌", price: 10),
    Fruit(name: "🍊", price: 10)
])

if var maxPriceFruit = heap.max {
    maxPriceFruit.price -= 1
    
    heap.replaceMax(with: maxPriceFruit)
}

print(heap.unordered) // [Fruit(name: "🍎", price: 1), Fruit(name: "🍊", price: 9), Fruit(name: "🍊", price: 10)]

I think just changing

let maxNode = handle.maxValue(.leftMax, .rightMax)

to

let maxNode = handle.maxValue(.rightMax, .leftMax)

(same as popMax does) can fix the problem

Information

  • Package version: main branch
@iamhands0me iamhands0me added the bug Something isn't working label Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant