Binary Heaps
Heapsort in phases
1. Rearrange the array elements into max heap order 2. Repeatedly move the maximum element to its final sorted place toward the end of the array, and heapify the remaining elements
VLC from Huffman's algorithm
With prefix property such that there is no other encoding with a smaller expected code length; left branch - 0; right branch - 1;
Priority Queue
chooses the next element to delete based on priority
Binary Heap Deletion
delete and return the element with the extreme (max/min) priority; maintain the complete shape by replacing the root value with the value in the lowest, right-most leaf, then delete that leaf; swap values as necessary on a root-to-leaf path to maintain the partial order
Binary Heap Insertion
insert the new element in the one and only one location that will maintain the complete shape; swap values as necessary on a leaf-to-root path to maintain the partial order
Priority
some value associated with the element that could represent importance, cost, or some other problem-specific concept
Text compression
stores the same information in fewer bytes
Expected Code Length
want code tree with minimum; weighted average of all possible character code lengths
Transform into total order
Repeatedly move the maximum element to its final sorted place toward the end of the array, and heapify the remaining elements
Way we map a hierarch onto a linear array
Store the root at index 0; For a node stored at index i: Left child: 2i +1 Right child: 2i + 2 Parent: (i-1)/2
Extreme priority
The element with the most is returned by the remove operation (whether max or min depends on queue set up)
Implementing a priority queue
Binary heap-- usually array; Add- O(logN), remove- O(logN),peek- O(1); things are lined up in priority order vs time order; heap implies array
Code trees
Binary trees in which the leaves contain the characters to be coded, interior nodes are just place-holders; root of every subtree is annotated with the cumulative frequency of all its descendent leaves
Binary heap
Complete binary tree; obeys a partial order property (max or min heap)
Character codes from code trees
Generated by root to leaf traversals-- no code is the prefix to another because the path for each letter is different
Huffman's algorithm code tree
Generated code tree with an expected code length that is at least as small as any other code tree that could be generated
Huffman's algorithm
Generates a variable-length encoding for a given alphabet of the purposes of data compression (rather then using defaults)
Heapsort
In-place comparison sort with O(N log N) time complexity; Works in two phases: the initial arbitrary order of the array is transformed into a partial order, then the partial order is transformed into a total order
Generating a variable-length code
Must have a prefix property- code for one character can't be a prefix of another character's code
Variable-length codes
Number of bits per character determined by the char's relative frequency of occurrence (determined by first pass); Most frequent characters use fewest bits
Array-based implementation
Acceptable space efficient, easy traversal (parent and children determined from multiplication and division);
ASCII
American Standard Code for Information Interchange; includes English alphabet, punctuation, digits, and "control" characters; fixed length code
Arrange into max heap order
Beginning with the lowest, right-most parent and continuing to the root, heapify each subtree