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

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

View Full Document Right Arrow Icon
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); } }
Image of page 1

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

View Full Document Right Arrow Icon
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.
Image of page 2
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(); }
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
21 Priority queue The element to be deleted is the one with the highest priority.
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern