Top Banner
Class No.23 Data Structures http://ecomputernotes .com
31

Computer notes - Expression Tree

Nov 29, 2014

Download

Education

ecomputernotes

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer notes - Expression Tree

Class No.23

Data Structures

http://ecomputernotes.com

Page 2: Computer notes - Expression Tree

Expression Tree

The inner nodes contain operators while leaf nodes contain operands.

a

c

+

b

g

*

+

+

d

*

*

e

f

http://ecomputernotes.com

Page 3: Computer notes - Expression Tree

Expression Tree

The tree is binary because the operators are binary.

a

c

+

b

g

*

+

+

d

*

*

e

f

http://ecomputernotes.com

Page 4: Computer notes - Expression Tree

Expression Tree

This is not necessary. A unary operator (!, e.g.) will have only one subtree.

a

c

+

b

g

*

+

+

d

*

*

e

f

http://ecomputernotes.com

Page 5: Computer notes - Expression Tree

Expression Tree

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

a

c

+

b

g

*

+

+

d

*

*

e

f

http://ecomputernotes.com

Page 6: Computer notes - Expression Tree

Enforcing Parenthesis

void inorder(TreeNode<int>* treeNode){ if( treeNode != NULL ){

cout << "(";inorder(treeNode->getLeft());cout << ")";cout << *(treeNode->getInfo());cout << "(";inorder(treeNode->getRight());cout << ")";

}} http://ecomputernotes.com

Page 7: Computer notes - Expression Tree

Expression Tree

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

a

c

+

b

g

*

+

+

d

*

*

e

f

http://ecomputernotes.com

Page 8: Computer notes - Expression Tree

Expression Tree

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

a

c

+

b

g

*

+

+

d

*

*

e

f

http://ecomputernotes.com

Page 9: Computer notes - Expression Tree

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.

http://ecomputernotes.com

Page 10: Computer notes - Expression Tree

Constructing Expression Tree

a b + c d e + * *

stack

http://ecomputernotes.com

Page 11: Computer notes - Expression Tree

Constructing Expression Tree

a b + c d e + * *

ba

Stack is growing left to right

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

top

http://ecomputernotes.com

Page 12: Computer notes - Expression Tree

Constructing Expression Tree

a b + c d e + * *

ba

Stack is growing left to right

+

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.

http://ecomputernotes.com

Page 13: Computer notes - Expression Tree

Constructing Expression Tree

a b + c d e + * *

ba

+ dc e

http://ecomputernotes.com

Page 14: Computer notes - Expression Tree

Constructing Expression Tree

a b + c d e + * *

ba

+ c

ed

+

http://ecomputernotes.com

Page 15: Computer notes - Expression Tree

Constructing Expression Tree

a b + c d e + * *

ba

+

c

ed

+

*

http://ecomputernotes.com

Page 16: Computer notes - Expression Tree

Constructing Expression Tree

a b + c d e + * *

ba

+

c

ed

+

*

*

http://ecomputernotes.com

Page 17: Computer notes - Expression Tree

Other Uses of Binary Trees

Huffman Encoding

http://ecomputernotes.com

Page 18: Computer notes - Expression Tree

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).

http://ecomputernotes.com

Page 19: Computer notes - Expression Tree

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.

http://ecomputernotes.comhttp://ecomputernotes.com

Page 20: Computer notes - Expression Tree

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.

Page 21: Computer notes - Expression Tree

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.

Page 22: Computer notes - Expression Tree

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.

Page 23: Computer notes - Expression Tree

Huffman Encoding

Original text: traversing threaded binary trees

size: 33 characters (space and newline)

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

i : 2n : 2r : 5s : 2t : 3v : 1y : 1

Page 24: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2

2 is equal to sum of the frequencies of the two children nodes.

Page 25: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2 2

There a number of ways to combine nodes. We have chosen just one such way.

Page 26: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2 2 2

Page 27: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2 2 2

4 4

Page 28: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2 2 2

5444

6

Page 29: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2 2 2

5444

869 10

Page 30: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2 2 2

5444

86

14

9

19

10

Page 31: Computer notes - Expression Tree

Huffman Encoding

v1

y1

SP3

r5

h1

e5

g

1

b1

NL

1

s2

n2

i2

d2

t3

a3

2 2 2

5444

86

14

9

19

10

33