Download presentation
Presentation is loading. Please wait.
1
Chapter 8 – Binary Search Tree
8.6 Huffman Trees Chapter 8 – Binary Search Tree
2
Attendance Quiz #29 Trees
3
Tip #31: size() vs empty()
Trees Should size() or empty() be used to test for container empty? bool empty() const { return (size() == 0); // BAD?? } Some implementations of std::list::size take O(n) and not O(1). By using the empty() function: Not very likely to incur the overhead of an extra function call, since the function is probably in-lined. Makes makes code more readable, and means you don't have to worry about implementation details. Your code can more easily be adapted to other types of containers, with other characteristics. Bottom line: The standard guarantees that empty() is a constant-time operation for all standard containers.
4
Case Study: Building a Custom Huffman Tree
8.6 Huffman Trees Case Study: Building a Custom Huffman Tree 8.6, pgs
5
Huffman Trees Huffman coding is a lossless data compression algorithm.
Assign variable-length codes to input characters based on the frequencies of corresponding characters. The most frequent character gets the smallest code and the least frequent character gets the largest code. A straight binary encoding of an alphabet assigns a unique 8- bit binary number to each symbol in the alphabet. ASCII has 256 possible characters, the length of a message would be 8 x n where n is the total number of character. The message “go eagles” requires 9 x 8 or 72 bits in ASCII. The same string encoded using a good Huffman encoding schema would only require 38 bits. A Huffman tree can be implemented using a binary tree and a priority_queue.
6
Huffman Trees Trees In 1973, Donald Knuth published the relative frequencies of the letters in English text. The letter e occurs and average of 103 times every 1000 letters (or 10.3% of the letters are e's.) e = 010
7
A 1. What are the Huffman codes for the characters found in the Huffman binary tree to the right? 1 p 5 s 4 e 2 l n 13 8 char frequency encoding s 5 e 4 l 2 n 1 p 10 110 1110 1111 2. Using the above Huffman codes, what word is represented with the following 27-bit stream? sleeplessness 3. What is the resulting compression ratio of Huffman encoding verses 8-bit ASCII encoding? (13 x 8 = 104 bits) : 27 bits or 4 : 1
8
Steps to build Huffman Tree
Trees Input: array of unique characters with their frequency of occurrences. Create a leaf node for each unique character and build a min- heap of all leaf nodes. Min-heap is used as a priority queue. The value of frequency field is used to compare two nodes in min heap. The least frequent character will be at root (min-heap). Extract two nodes (minimum frequencies) from the min heap and create a new internal node whose frequency is the sum of the two extracted node frequencies. Make the first extracted node the left child and the second node the right child. Add the new internal node (with two children) to the min heap. Repeat steps #2 and #3 until the heap contains only one node. The tree is complete and the remaining node is the root node of a Huffman Binary Tree.
9
An assassin sins 1 3 n a 16 6 s 2 i 10 4 ␣ char frequency encoding a 3 n sp 2 i s 6 encoding 00 01 100 101 11 1. Create a leaf node for each unique character and build a min-heap of all leaf nodes. 2 ␣ i 3 a n 6 s 2. Extract two nodes, create new node w/frequency equal to the sum of the two node frequencies, and add back to min-heap. Repeat until 1 node. 1 2 3 4 ␣ i a n 6 s 1 2 3 a n 6 s 4 $ 1 2 4 $ 6 s 1 6 $ 10 16 $ 2 ␣ i 2 ␣ i 3 a n 3 a n 4 $ 6 s 6 $ 10 2 ␣ i 3 a n 4 $ 6 s 2 ␣ i 3. What is the bit stream for "An assassin sins"?
10
B aaabbc 1100 4:24 char frequency encoding a b c 3 2 1 11 10
11 10 1. Using the above character stream, create a leaf node for each unique character and build a min-heap of all leaf nodes. 1 2 1 c 3 a 2 b 2. Extract two nodes, create new node w/frequency equal to the sum of the two node frequencies, and add back to min-heap. Repeat until 1 node. 1 2 c 3 a b 1 3 a $ 6 $ 3 a 1 2 b c 6 1 c 2 b 3 a $ 1 c 2 b 3. What is the bit stream for "baa"? 1100 4:24 4. What is the resulting compression ratio of Huffman to ASCII?
11
11.1 Self Balancing Trees 11.1, pgs
12
Balanced Binary Search Trees
The performance of a binary search tree is proportional to the height of the tree or the maximum number of nodes along a path from the root to a leaf. A full binary tree of height h (assuming an empty tree is of height 0,) can hold 2h -1 items. If a binary search tree is full and contains n items, the expected performance is O(log n). However, if a binary tree is not full, the actual performance is worse than expected, possibly up to O(n). Self-balancing trees require the heights of the right and left subtrees to be equal or nearly equal. We'll examine these trees later as well as non-binary search trees: the B-tree and its specializations, the 2-3 and trees, and the B+ tree.
13
Balanced Binary Search Trees
Balanced tree is a tree whose height is of order of log(number of elements in the tree). Balancing a tree recursively applies to every subtree. That is, the tree is balanced if and only if: The left and right subtrees' heights differ by at most one, AND The left subtree is balanced, The right subtree is balanced. 8 4 12 2 10 9 13 Balanced 8 4 12 2 10 9 Not Balanced
14
Why Balance is Important
Trees The binary tree to the right has search performance of O(n), not O(log n). Balancing a tree recursively applies to every subtree. That is, the tree is balanced if and only if: The left and right subtrees' heights differ by at most one, AND The left subtree is balanced, The right subtree is balanced. 1 2 3 4 5 6 7 8 9 10 8 4 12 2 10 9 13 Balanced 8 4 12 2 10 9 Not Balanced
15
Rotation Trees We need an operation on a binary tree that changes the relative heights of left and right subtrees, but preserves the binary search tree property. Tree rotation is an operation on a binary tree that changes the structure without interfering with the order of the elements. A tree rotation moves one node up in the tree and one node down. Rotation is used to change the shape of the tree, and in particular to decrease its height by moving smaller subtrees down and larger subtrees up, resulting in improved performance of many tree operations.
17
Trees
18
Algorithm for Rotation
Trees = left right = data = 20 BTNode root = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode = left right = data = 7 NULL BTNode
19
Algorithm for Rotation
Trees = left right = data = 20 BTNode root temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) = left right = data = 7 NULL BTNode
20
Algorithm for Rotation
Trees root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right = left right = data = 7 NULL BTNode
21
Algorithm for Rotation
Trees root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root = left right = data = 7 NULL BTNode
22
Algorithm for Rotation
Trees root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root Set root to temp = left right = data = 7 NULL BTNode
23
Algorithm for Rotation
Trees root = left right = data = 10 BTNode = left right = data = 5 NULL BTNode = left right = data = 20 BTNode = left right = data = 7 NULL BTNode = left right = data = 15 NULL BTNode = left right = data = 40 NULL BTNode
24
Implementing Rotation
Trees
25
Implementing Rotation
Trees
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.