Expression Tree The inner nodes contain operators while leaf nodes contain operands. a c + b g * d e f Start of lecture 25.

Slides:



Advertisements
Similar presentations
Lecture 4 (week 2) Source Coding and Compression
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
Huffman Encoding Dr. Bernard Chen Ph.D. University of Central Arkansas.
Greedy Algorithms (Huffman Coding)
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Trees Chapter 8.
Huffman Coding: An Application of Binary Trees and Priority Queues
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
CS 206 Introduction to Computer Science II 04 / 29 / 2009 Instructor: Michael Eckmann.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Chapter 9: Huffman Codes
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Data Compression Basics & Huffman Coding
Spring 2015 Mathematics in Management Science Binary Linear Codes Two Examples.
Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare.
Data Structures and Algorithms Huffman compression: An Application of Binary Trees and Priority Queues.
1 Project 7: Huffman Code. 2 Extend the most recent version of the Huffman Code program to include decode information in the binary output file and use.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Huffman Encoding Veronica Morales.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
CS-2852 Data Structures LECTURE 13B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Compression.  Compression ratio: how much is the size reduced?  Symmetric/asymmetric: time difference to compress, decompress?  Lossless; lossy: any.
Huffman Coding. Huffman codes can be used to compress information –Like WinZip – although WinZip doesn’t use the Huffman algorithm –JPEGs do use Huffman.
Lossless Compression CIS 465 Multimedia. Compression Compression: the process of coding that will effectively reduce the total number of bits needed to.
Huffman’s Algorithm 11/02/ Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.
Foundation of Computing Systems
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Huffman Codes. 2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman.
Lecture 12 Huffman Algorithm. In computer science and information theory, a Huffman code is a particular type of optimal prefix code that is commonly.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
3.3 Fundamentals of data representation
Huffman Codes ASCII is a fixed length 7 bit code that uses the same number of bits to define each character regardless of how frequently it occurs. Huffman.
HUFFMAN CODES.
COMP261 Lecture 22 Data Compression 2.
Lecture No.14 Data Structures Dr. Sohail Aslam
Infix to postfix conversion
Lecture No.13 Data Structures Dr. Sohail Aslam
Lecture No.06 Data Structures Dr. Sohail Aslam
Huffman Coding Based on slides by Ethan Apter & Marty Stepp
Binary Search Tree (BST)
Lecture No.15 Data Structures Dr. Sohail Aslam
CSC 172– Data Structures and Algorithms
Data Compression If you’ve ever sent a large file to a friend, you may have compressed it into a zip archive like the one on this slide before doing so.
Chapter 8 – Binary Search Tree
Chapter 9: Huffman Codes
Find in a linked list? first last 7  4  3  8 NULL
Math 221 Huffman Codes.
Chapter 11 Data Compression
Huffman Coding CSE 373 Data Structures.
Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths.
Topic 20: Huffman Coding The author should gaze at Noah, and ... learn, as they did in the Ark, to crowd a great deal of matter into a very small compass.
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Presentation transcript:

Expression Tree The inner nodes contain operators while leaf nodes contain operands. a c + b g * d e f Start of lecture 25.

Lecture No.25 Data Structures Dr. Sohail Aslam

Expression Tree The tree is binary because the operators are binary. a + b g * d e f

Expression Tree This is not necessary. A unary operator (!, e.g.) will have only one subtree. a c + b g * d e f

Expression Tree Inorder traversal yields: a+b*c+d*e+f*g a c + b g * d

Enforcing Parenthesis /* inorder traversal routine using the parenthesis */ void inorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) if(treeNode->getLeft() != NULL && treeNode->getRight() != NULL) //if not leaf cout<<"("; inorder(treeNode->getLeft()); cout << *(treeNode->getInfo())<<" "; inorder(treeNode->getRight()); cout<<")"; }

Expression Tree Inorder : (a+(b*c))+(((d*e)+f)*g) a c + b g * d e f

Expression Tree Postorder traversal: a b c * + d e * f + g * + which is the postfix form. a c + b g * d e f

Constructing Expression Tree Algorithm to convert postfix expression into an expression tree. We already have an expression to convert an infix expression to postfix. Read a symbol from the postfix expression. If symbol is an operand, put it in a one node tree and push it on a stack. If symbol is an operator, pop two trees from the stack, form a new tree with operator as the root and T1 and T2 as left and right subtrees and push this tree on the stack.

Constructing Expression Tree a b + c d e + * * stack

Constructing Expression Tree a b + c d e + * * top If symbol is an operand, put it in a one node tree and push it on a stack. a b Stack is growing left to right

Constructing Expression Tree a b + c d e + * * If symbol is an operator, pop two trees from the stack, form a new tree with operator as the root and T1 and T2 as left and right subtrees and push this tree on the stack. + a b Stack is growing left to right

Constructing Expression Tree a b + c d e + * * + c d e a b

Constructing Expression Tree a b + c d e + * * + c + a b d e

Constructing Expression Tree a b + c d e + * * + * a b c + d e

Constructing Expression Tree a b + c d e + * * * + * a b c + d e

Other Uses of Binary Trees Huffman Encoding

Huffman Encoding Data compression plays a significant role in computer networks. To transmit data to its destination faster, it is necessary to either increase the data rate of the transmission media or to simply send less data. Improvements with regard to the transmission media has led to increase in the rate. The other options is to send less data by means of data compression. Compression methods are used for text, images, voice and other types of data (space probes).

Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths for the letters used in the original message. Huffman code is also part of the JPEG image compression scheme. The algorithm was introduced by David Huffman in 1952 as part of a course assignment at MIT.

Huffman Encoding To understand Huffman encoding, it is best to use a simple example. Encoding the 32-character phrase: "traversing threaded binary trees", If we send the phrase as a message in a network using standard 8-bit ASCII codes, we would have to send 8*32= 256 bits. Using the Huffman algorithm, we can send the message with only 116 bits.

Huffman Encoding List all the letters used, including the "space" character, along with the frequency with which they occur in the message. Consider each of these (character,frequency) pairs to be nodes; they are actually leaf nodes, as we will see. Pick the two nodes with the lowest frequency, and if there is a tie, pick randomly amongst those with equal frequencies. Start of Lecture 26

Huffman Encoding Make a new node out of these two, and make the two nodes its children. This new node is assigned the sum of the frequencies of its children. Continue the process of combining the two nodes of lowest frequency until only one node, the root, remains.

Huffman Encoding Original text: traversing threaded binary trees size: 33 characters (space and newline) NL : 1 SP : 3 a : 3 b : 1 d : 2 e : 5 g : 1 h : 1 i : 2 n : 2 r : 5 s : 2 t : 3 v : 1 y : 1

Huffman Encoding a 3 e 5 r 5 t 3 i 2 n 2 s 2 d 2 2 SP 3 NL 1 b 1 g 1 h 2 is equal to sum of the frequencies of the two children nodes. a 3 e 5 r 5 t 3 i 2 n 2 s 2 d 2 2 SP 3 NL 1 b 1 g 1 h 1 v 1 y 1

Huffman Encoding a 3 e 5 r 5 t 3 i 2 n 2 s 2 d 2 2 2 SP 3 NL 1 b 1 g 1 There a number of ways to combine nodes. We have chosen just one such way. a 3 e 5 r 5 t 3 i 2 n 2 s 2 d 2 2 2 SP 3 NL 1 b 1 g 1 h 1 v 1 y 1

Huffman Encoding a 3 e 5 r 5 t 3 i 2 n 2 s 2 d 2 2 2 2 SP 3 NL 1 b 1 g v 1 y 1

Huffman Encoding a 3 e 5 r 5 t 3 4 4 i 2 n 2 s 2 d 2 2 2 2 SP 3 NL 1 b v 1 y 1

Huffman Encoding 6 a 3 e 5 r 5 4 5 t 3 4 4 i 2 n 2 s 2 d 2 2 2 2 SP 3 NL 1 b 1 g 1 h 1 v 1 y 1

Huffman Encoding 9 10 6 8 a 3 e 5 r 5 4 5 t 3 4 4 i 2 n 2 s 2 d 2 2 2 SP 3 NL 1 b 1 g 1 h 1 v 1 y 1

Huffman Encoding 19 14 9 10 6 8 a 3 e 5 r 5 4 5 t 3 4 4 i 2 n 2 s 2 d SP 3 NL 1 b 1 g 1 h 1 v 1 y 1

Huffman Encoding v 1 y SP 3 r 5 h e g b NL s 2 n i d t a 4 8 6 14 9 19 10 33 End of lecture 25.