reset password
Author Message
mnava18
Posts: 86
Posted 04:31 Dec 07, 2014 |

anyone rememeber what question 1 and 2 were? totally forgot how how that worked

Last edited by mnava18 at 04:33 Dec 07, 2014.
vsluong4
Posts: 87
Posted 23:19 Dec 07, 2014 |
  1. Assume one is using two heaps to keep track of the median of a sequence of numbers. Assume the "low" heap contains 11 numbers with a maximum of 20 and the "high" heap contains 10 numbers with a minimum of 25. Assume the next input is 15. Which of the following statements will be guaranteed to be true after 15 is processed?

     The maximum of the low heap will be 15
     The maximum of the low heap will be 25
     The minimum of the high heap will be 15
     The minimum of the high heap will be 20
  2. For computer execution, 

    What's the running time for finding the maximum element in a max-heap?

    what's the running time for finding the maximum element in a balanced Binary Search Tree (BST) ?

     heap : O(log n), BST : O(log n)
     heap : O(n * log n), BST : O (log n) 
     heap : O(1), BST : O(log n)
     heap : O(log n), BST : O(1)

It would probably help the most if you rewatch the video about these topics.

1) After inserting 15, the two heaps differ by 2 and they must be balanced for the algorithm to work.  So the one with more elements has 1 taken off and given to the one with less.

2) The root, or first element, of a MAX heap is the max element.  Looking up the first element is the same whether you have 1 element or 1000000000.  This is different from extracting the max element because extra work must be done(O(logn)) in order to maintain the heap structure.  

Balanced BST support fast look-up because there are log(n) levels and the max element is in the rightmost(or leftmost if you reverse a tree) node.  Then you just jump from one node to its right one until you get to the end.  Each time you go to another node, you're going down a level and there are only log(n) levels to explore because the tree is balanced.