90 likes | 198 Vues
Learn about the ordering of nodes in Dynamic Huffman Trees and how to maintain it efficiently using array representation and IncrementLetter procedure. Practice with an exercise to encode text.
E N D
Sibling property: there is an ordering of nodes such that C(ni)<=C(ni+1) for all i and for each adjacent pair of nodes n2k-1, n2k are siblings in the Tree. Dynamic Huffman Trees
Example • Ordering is
Keeping track of Ordering • (one idea) Use Array representation of tree. • Store child/parent offsets (can’t use implicit representation, since Huffman trees aren’t necessarily “complete”) • (more clever) See if you can use pointers to require only 1 lookup to find the swap-node
Increment Letter • Procedure IncrementLetter (letter) • letterPos = position of letter in Array representation of tree • suspectPos = letterPos • While suspectPos is not Root • Increment Count(letterPos) • If Count(suspectPos) > Count(suspectPos - 1) Then • { it got out of order } • lastOfCount = lowest #’d array index whose count is • the same as Count(suspectPos) used to be • Swap sub-Trees at lastOfCount, suspectPos • Else • { everything’s fine now } • Exit While • End If • suspectPos = Parent(lastOfCount) • End While
Put new letter into tree Procedure InsertNewLetter (newLetter) { the NULL character is the special signal for a new character } Replace NULL node with newParentNode Make newParentNode a Parent of (NULL, newLetter) Run IncrementLetter algorithm on newLetter
Exercise • Process 11-character text using dynamic Huffman tree. Write the encoding. • Periodically check if static tree would be the same • Use Array (used for Heap) or draw by hand. Text: The rather rash hero rode a horse that dashed with a wet head and dared to rise. It hid with tethered teeth that saw a sorrow. Oh, are these withered arrows of teeth so arid in war? Or has the horse the ride of heroes ridden in its hidden dose of worth? Letters: adefh iorst w (11 letters)