Skip to main content
added 165 characters in body
Source Link
Will Ness
  • 1k
  • 1
  • 8
  • 25

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will now assume it's operating on heaps only, not on arbitrary trees. This will make replaceTop logarithmic. Leaves all the other problems unaddressed.

According to master theoremmaster theorem for 2T(n/2) + O(log(n)) case, heapify will be O(n) then, assuming a balanced tree. A balanced tree can be built from a list in remveTopO(n) time.

removeTop will most probably be O(log(n)).

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will make replaceTop logarithmic. Leaves all the other problems unaddressed.

According to master theorem, heapify will be O(n) then. remveTop will most probably be O(log(n)).

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will now assume it's operating on heaps only, not on arbitrary trees. This will make replaceTop logarithmic. Leaves all the other problems unaddressed.

According to master theorem for 2T(n/2) + O(log(n)) case, heapify will be O(n) then, assuming a balanced tree. A balanced tree can be built from a list in O(n) time.

removeTop will most probably be O(log(n)).

added 389 characters in body
Source Link
Will Ness
  • 1k
  • 1
  • 8
  • 25

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will make replaceTop logarithmic. Leaves all the other problems unaddressed still.

replaceTop (Leaf _)       a = Leaf a      -- operates on heaps!
replaceTop (Node lh _ rh) a               -- lh, rh are heaps!
   | a >= top       = Node lh a rh
   | ltop > rtop    = Node (replaceTop lh a) ltop rh               
   | otherwise      = Node lh                rtop (replaceTop rh a)
  where
    ltop = atTop lh
    rtop = atTop rh
    top  = max ltop rtop

According to master theorem, heapify will be O(n) then. remveTop will most probably be O(log(n)).

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will make replaceTop logarithmic. Leaves all the other problems unaddressed still.

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will make replaceTop logarithmic. Leaves all the other problems unaddressed.

replaceTop (Leaf _)       a = Leaf a      -- operates on heaps!
replaceTop (Node lh _ rh) a               -- lh, rh are heaps!
   | a >= top       = Node lh a rh
   | ltop > rtop    = Node (replaceTop lh a) ltop rh               
   | otherwise      = Node lh                rtop (replaceTop rh a)
  where
    ltop = atTop lh
    rtop = atTop rh
    top  = max ltop rtop

According to master theorem, heapify will be O(n) then. remveTop will most probably be O(log(n)).

deleted 37 characters in body
Source Link
Will Ness
  • 1k
  • 1
  • 8
  • 25

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will make replaceTop logarithmic. Leaves all the other problems unaddressed still, but fixes this function, immediately.

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. Leaves all the other problems unaddressed still, but fixes this function, immediately.

It's probably simpler to re-write replaceTop to essentially duplicate the above code, without the calls to heapify. It will make replaceTop logarithmic. Leaves all the other problems unaddressed still.

Source Link
Will Ness
  • 1k
  • 1
  • 8
  • 25
Loading