BinaryTree_2(1)

# BinaryTree_2(1) - Representation of arithmetic expression...

• Notes
• 15

This preview shows pages 1–6. Sign up to view the full content.

17 Representation of arithmetic expression using a binary tree '+' 3 5 '+' 3 '/' '*' '-' 4 7 6 Binary tree representation 3+4*(6-7)/5+3 #define operand 0 #define operator 1 struct infoRecord { char dataType; union { char opr; double val; }; } double evalExprTree(treeNode<infoRecord> *tree) /* Precondition: the expression tree is nonempty and has no syntax error. The algorithm is based on postorder traversal. */ { if (tree->info.dataType == operand) return tree->info.val; else { double d1 = evalExprTree(tree->left); double d2= evalExprTree(tree->right); char symb = tree->info.opr; return evaluate(symb, d1, d2); } }

This preview has intentionally blurred sections. Sign up to view the full version.

18 Huffman Code we have an alphabet of n symbols by assigning a bit string code to each symbol of the alphabet, a long message can be encoded by concatenating the individual codes of the symbols making up the message For example, Symbol Code A 00 B 01 C 10 D 11 The code for the message ABCACADA would be 0001100010001100 , which requires 16 bits. To reduce the length of the encoded message, we can use variable-length code the code for one symbol cannot be a prefix of the code for another symbol. For example, Symbol Code A 0 B 110 C 10 D 111 The code for the message ABCACADA would become 01101001001110 , which requires 14 bits. If the frequency of the characters within a message is known a priori, then an optimal encoding that minimizes the total length of the encoded message can be determined using the Huffman algorithm . A binary tree is used to construct/decode Huffman code. The binary tree is known as the Huffman tree . To decode Huffman code, we interpret a zero as a left branch and a one as a right branch.
19 Huffman tree of the above example. Algorithm to construct the Huffman tree Inputs to the algorithm: the set of symbols and their weights. struct record { int weight; char symbol; }; Suppose there are n symbols, n binary trees corresponding to the n symbols are created, each consists of one node. The binary trees are maintained in a (priority) list L. //L is a list of n trees ordered by weight of the tree treeNode<record>* huffmanTree( PriorityList & L) { //pseudo code int n = L.length(); for(int i = 1; i < n; i++) { treeNode<record>* t = new treeNode<record>; t->left = L.least(); /* retrieve & remove the tree in L with smallest weight */ t->right = L.least(); t->info.weight = t->left->info.weight + t->right->info.weight; L.insert(t); } return L.least(); }

This preview has intentionally blurred sections. Sign up to view the full version.

20 For example, there are 6 symbols with frequencies ( ( ( ( ( ( 13 , , 9 , , 7 , , 5 , , 3 , , 2 , 5 4 3 2 1 0 q q q q q q 2 3 5 2 3 5 5 10 7 9 16 2 3 5 5 10 13 23 2 3 5 5 10 13 23 7 9 16 39 00 01 1000 1001 101 11 (a) (b) (c) (d) (e) Huffman codes for the 6 symbols symbol code q 0 1000 q 1 1001 q 2 101 q 3 00 q 4 01 q 5 11
21 Priority queue The element to be deleted is the one with the highest priority.

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Spring '14
• VAN
• Binary heap

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern