• No results found

expression tree & huffman encoding.pdf

N/A
N/A
Protected

Academic year: 2020

Share "expression tree & huffman encoding.pdf"

Copied!
42
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures & Algorithm

CSC-102

Lecture 7

Expression Tree

&

Huffman Encoding

(2)

2 A special kind of binary tree in which:

1. Each leaf node contains a single operand

2. Each nonleaf node contains a single binary operator

3. The left and right subtrees of an operator node represent subexpressions that must be evaluated

before applying the operator at the root of the subtree.

(3)

3

A Four-Level Binary Expression

‘*’

‘-’

‘8’ ‘5’

‘/’

‘+’

‘4’

‘3’

(4)

4

Levels Indicate Precedence

The levels of the nodes in the tree indicate their relative precedence of evaluation (we do not need parentheses to indicate precedence).

Operations at higher levels of the tree are evaluated later than those below them.

(5)

5

A Binary Expression Tree

What value does it have?

( 4 + 2 ) * 3 = 18

‘*’

‘+’

‘4’

‘3’

(6)

6

Easy to generate the infix, prefix, postfix

expressions (how?)

Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )

Prefix: * - 8 5 / + 4 2 3

Postfix: 8 5 - 4 2 + 3 / *

‘*’

‘-’

‘8’ ‘5’

‘/’

‘+’

‘4’

‘3’

(7)

7

Inorder Traversal: (A + H) / (M - Y)

‘/’

‘+’

‘A’ ‘H’

‘-’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree last

(8)

8

Preorder Traversal: / + A H - M Y

‘/’

‘+’

‘A’ ‘H’

‘-’

‘M’ ‘Y’

tree

Print left subtree second Print right subtree last

(9)

9

‘/’

‘+’

‘A’ ‘H’

‘-’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree second

Print last

(10)

Expression Trees

(11)
(12)

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

(13)

Constructing Expression Tree

• a b + c d e + * *

(14)

Constructing Expression Tree

• a b + c d e + * *

b a

Stack is growing left to right

If symbol is an operand, put it in a one node tree and push it on a stack.

(15)

Constructing Expression Tree

• a b + c d e + * *

b a

Stack is growing left to right

+

(16)

Constructing Expression Tree

• a b + c d e + * *

b a

(17)

Constructing Expression Tree

• a b + c d e + * *

b a

+ c

e d

(18)

Constructing Expression Tree

• a b + c d e + * *

b a

+

c

e d

(19)

Constructing Expression Tree

• a b + c d e + * *

b a

+

c

e d

+ *

(20)

Expression Tree

Postorder traversal: a b c * + d e * f + g * +

which is the postfix form.

a

c +

b

g *

+

+

d *

*

e

(21)

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.

(22)

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.

(23)

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.

(24)

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

(25)

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,

(26)

Huffman Encoding

Original text:

traversing threaded binary trees

size: 33 characters (space SP and newline NL)

NL : 1 SP : 3 a : 3 b : 1 d : 2 e : 5 g : 1 h : 1

(27)

Huffman Encoding

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

(28)

Huffman Encoding

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

(29)

Huffman Encoding

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

(30)

Huffman Encoding

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

2 2 2

(31)

Huffman Encoding

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

2 2 2

5 4

4 4

(32)

Huffman Encoding

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

2 2 2

(33)

Huffman Encoding

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

2 2 2

(34)

Huffman Encoding

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

2 2 2

(35)

Huffman Encoding

• Start at the root. Assign 0 to left branch and 1 to the right branch.

• Repeat the process down the left and right subtrees.

(36)

Huffman Encoding

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

2 2 2

(37)

Huffman Encoding

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

2 2 2

(38)

Huffman Encoding

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

2 2 2

5 4 4 4 8 6 14 9 19 10 33 1 0 1 0 1 0 1 0 1

0 0 1 0 1

1

(39)

Huffman Encoding

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

2 2 2

5 4 4 4 8 6 14 9 19 10 33 1 0 1 0 1 0 1 0 1

0 0 1 0 1

1

0 0 1 0 1 0 1

1 0 1

(40)

Huffman Encoding

Huffman character codes

NL  10000 SP  1111

a  000 b  10001 d  0100 e  101 g  10010 h  10011 i  0101 n  0110 r  110 s  0111 t  001 v  11100 y  11101

• Notice that the code is variable length.

• Letters with higher

frequencies have shorter codes.

• The tree could have been built in a number of ways; each would yielded

different codes but the code would still be

(41)

Huffman Encoding

Original: traversing threaded binary trees

Encoded:

00111000011100101110011101010110100101111001 10011110101000010010101001111100001010110000 11011101111100111010110101110000

(42)

Huffman Encoding

Original: traversing threaded binary trees With 8 bits per character, length is 264.

Encoded:

00111000011100101110011101010110100101111001 10011110101000010010101001111100001010110000 11011101111100111010110101110000

References

Related documents

The Hybrid model combines features of Pools and Bilateral Contracts (Shahidehpour et al., 2002). In this model, a Pool isn’t mandatory, and customers can either negotiate a

See Nielsen and Hunter’s (this issue) discussion of “sequential and informational complementarity”. The issue of performance management and evaluation as competing management tools

According to a survey of visitors to museums in Japan with archaeological collections (Haggis 2008 – Chapter 35), intended to ascertain which museum activities people find

Cutler (1984) explains that native speakers of English focus on stressed syllables when listening to a stream of speech as part of the decoding process; i.e., for native

To append data, a client first determines the next available position in the shared log – using a sequencer node as an optimization for avoiding contention with other appending

Now in low cost housing, building material cost is less because we make use of the locally available materials and also the labour cost can be reduced by properly making the

Special cases and applications are described in a collection of related articles: estimator selection and performance assessment based on uncensored data (Dudoit and van der

The first blink involves the triggering widget (e.g. ”headline blinks sportHL on clicking”) while subsequent blinks subordinate the rendering of a widget to click in its