Sunday, 4 December 2011

Queues, Heaps and Priority Queues Cont'd

     So like you said, my blogs are too much like question and answer, so I'm going to switch it up a bit and actually blog to you for the first time. Basically, in the first part of this weeks homework I learned some stuff about the add() and remove() methods of the DynamicHeap, where basically add adds new strings while maintaining the principles of a heal (complete trees). After adding the string in the right place to maintain a complete tree, it reorganizes the tree to maintain the maxHeap be comparing and swapping nodes. Remove() does a similar thing but inverted in a way. It deletes the maximum value and reorganizes the tree after that, the code however was a bit confusing, but it essentially sets the root to the maxValue then deletes it.
      I'm quite confused about coding a method to find the height of a heap because I was researching on it for quite a bit and found that the height of a complete tree (which would be the height of a heap) is log(base 2) of n, which I have no idea how to code. I then looked up logarithm methods but it got way more confusing from there.
      I also really like HeapSort, it's extremely logical. You traverse your list, or array, or whatever, and add all those values into a heap. Then you just start removing values from the heap and adding them to the end of the array, this is sorting it because whenever you remove a value from a heap it's the max value. Boom, sorted array. But it's really inefficient, you traverse it twice meaning it takes O(2n) time.