Skip to main content
mathjax to replace backticks for math
Source Link
Snowbody
  • 8.7k
  • 25
  • 50

Heap.ctor(T[], HeapType) modifies the provided array, and uses it for internal representation! This is a disaster of epic proportions! You've ruined the user's data, and now they can return the favour by corrupting your heap! Even more fun, because RemoveAt<T>(int) creates a new array (which is an O(n)\$O(n)\$ operation, and exactly something that Heaps are meant to avoid) this behaviour will change when Add is called. You should be using a List<T>, not an array, and you should be creating it yourself, not accepting it from in a constructor.

Heap.ctor(T[], HeapType) modifies the provided array, and uses it for internal representation! This is a disaster of epic proportions! You've ruined the user's data, and now they can return the favour by corrupting your heap! Even more fun, because RemoveAt<T>(int) creates a new array (which is an O(n) operation, and exactly something that Heaps are meant to avoid) this behaviour will change when Add is called. You should be using a List<T>, not an array, and you should be creating it yourself, not accepting it from in a constructor.

Heap.ctor(T[], HeapType) modifies the provided array, and uses it for internal representation! This is a disaster of epic proportions! You've ruined the user's data, and now they can return the favour by corrupting your heap! Even more fun, because RemoveAt<T>(int) creates a new array (which is an \$O(n)\$ operation, and exactly something that Heaps are meant to avoid) this behaviour will change when Add is called. You should be using a List<T>, not an array, and you should be creating it yourself, not accepting it from in a constructor.

Fix Build complexity
Source Link
VisualMelon
  • 7.6k
  • 23
  • 46

Again, been a while since I had to understand Binary Heaps, so I couldn't say if this is right or not (cursory testing suggests it works), butbut it is not O(n), this is O(n*log(n)) it is notis O(n), this is O(n*log(n))my memory of algorithms betrays me again.

Again, been a while since I had to understand Binary Heaps, so I couldn't say if this is right or not (cursory testing suggests it works), but it is not O(n), this is O(n*log(n)).

Again, been a while since I had to understand Binary Heaps, so I couldn't say if this is right or not (cursory testing suggests it works), but it is not O(n), this is O(n*log(n)) it is O(n), my memory of algorithms betrays me again.

added 78 characters in body
Source Link
VisualMelon
  • 7.6k
  • 23
  • 46
  • Do as Snowbody says, and encapsulate your own representation: don't expose this to anyone.

  • Make that representation a List<T>, and call List<T>.Add(T) to add to it, and List<T>.RemoveAt(int) to remove from it.

  • Add a Count member to the IHeap<T> interface

  • Fix Add

  • More comprehensive argument checks, and leave them in the release build

  • TEST THE CODE

  • Do as Snowbody says, and encapsulate your own representation: don't expose this to anyone.

  • Make that representation a List<T>, and call List<T>.Add(T) to add to it, and List<T>.RemoveAt(int) to remove from it.

  • Add a Count member to the IHeap<T> interface

  • Fix Add

  • TEST THE CODE

  • Do as Snowbody says, and encapsulate your own representation: don't expose this to anyone.

  • Make that representation a List<T>, and call List<T>.Add(T) to add to it, and List<T>.RemoveAt(int) to remove from it.

  • Add a Count member to the IHeap<T> interface

  • Fix Add

  • More comprehensive argument checks, and leave them in the release build

  • TEST THE CODE

Source Link
VisualMelon
  • 7.6k
  • 23
  • 46
Loading