transforming infix to postfix steps to convert the infix expression a / b * ( c + ( d – e ) ) to...

29
Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form.

Post on 21-Dec-2015

290 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Transforming Infix to Postfix

Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form.

Page 2: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Infix-to-Postfix Algorithm

Symbol in Infix

Action

Operand Append to end of output expression

Operator ^ Push ^ onto stack

Operator +,-,*, or /

Pop operators from stack, append to output expression until stack empty or top has lower precedence than new operator. Then push new operator onto stack

Open parenthesis

Push ( onto stack, treat it as an operator with the lowest precedence

Close parenthesis

Pop operators from stack, append to output expression until we pop an open parenthesis. Discard both parentheses.

Page 3: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Checking for Balanced (), [], {}

The contents of a stack during the scan of an expression that contains the balanced delimiters {[()]}

a{b[c(d+e)/2 – f] +1}

Page 4: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Evaluating Postfix Expression

The stack during the evaluation of the postfix expression

a b + c / when a is 2, b is 4 and c is 3

Page 5: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

The Program Stack

The program stack at 3 points in time; (a) when main begins execution; (b) when methodA begins execution, (c)

when methodB begins execution.

Page 6: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Circular Linked Implementations

of a Queue

A circular linked chain with an external reference to its last node that (a) has more than one node; (b) has one node;

(c) is empty.

Page 7: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Array-Based Implementation of a Queue

An array that represents a queue without shifting its entries: (c) after several more additions & removals;

(d) after two additions that wrap around to the beginning of the array

Page 8: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Binary Trees• If a binary tree of height h has all leaves on the same

level h and every nonleaf in a full binary tree has exactly two children

• A complete binary tree is full to its next-to-last level– Leaves on last level filled from left to right

Page 9: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Binary Trees

• Total number of nodes n for a full tree can be calculated as:

• The height of a binary tree with n nodes that is either complete or full is log2(n + 1)

1

0

2 2 1h

i h

i

n

Page 10: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Traversals Exercise

• The order of these nodes being visited using 4 different traversal methods

Page 11: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Answer• In this binary tree, D = node, L = left, R =

right• Preorder (DLR) traversal yields: A, H, G, I,

F, E, B, C, D • Postorder (LRD) traversal yields: G, F, E, I,

H, D, C, B, A • In-order (LDR) traversal yields: G, H, F, I, E,

A, B, D, C • Level-order traversal yields: A, H, B, G, I, C,

F, E, D

Page 12: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Binary Search Trees

Two binary search trees containing the same names as the tree in previous slide

Page 13: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Removing an Entry, Node Has Two Children

Node N and its subtrees; (a) entry a is immediately before e, b is immediately after e; (b) after deleting the node that

contained a and replacing e with a.

Page 14: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Removing an Entry, Node Has Two Children

Node N and its subtrees; (a) entry a is immediately before e, b is immediately after e; (b) after deleting the node that

contained a and replacing e with a.

Page 15: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Removing an Entry, Node Has Two Children

(a) A binary search tree; (b) after removing Chad;

Page 16: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Heaps• A complete binary tree

– Nodes contain Comparable objects– Each node contains no smaller (or no larger)

than objects in its descendants

• Maxheap– Object in a node is ≥ its descendant objects.

Root node contains the largest data

• Minheap– Object in a node is ≤ descendant objects– Root node contains the smallest data

Page 17: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Using an Array to Represent a Heap

• When a binary tree is complete– Can use level-order traversal to store data in

consecutive locations of an array

• Enables easy location of the data in a node's parent or children– Parent of a node at i is found at i/2

(unless i is 1)– Children of node at i found at indices

2i and 2i + 1

Page 18: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Using an Array to Represent a Heap

Fig 1. (a) A complete binary tree with its nodes numbered in level order; (b) its representation as an array.

Page 19: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Adding an Entry

In Figure 1, the steps in adding 85 to the maxheap

Begin at next available position for a leafFollow path from this leaf toward root until find correct position for new entry

Page 20: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Removing the Root

To remove a heap's root•Replace the root with heap's last child

•This forms a semiheap•Then use the method reheap

•Transforms the semiheap to a heap

Page 21: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Creating a Heap

The steps in adding 20, 40, 30, 10, 90, and 70 to a heap.

Page 22: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Creating a Heap

The steps in creating a heap by using reheap.

More efficient to use reheap than to

use add

More efficient to use reheap than to

use add

Page 23: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Breadth-First Traversal(ctd.) A trace of a breadth-first traversal for a directed graph,

beginning at vertex A.

Page 24: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Depth-First TraversalA trace of a depth-first traversal

beginning at vertex A of the directed graph

Page 25: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

Shortest Path in an Weighted Graph

Finding the cheapest path from vertex A to vertex H

in the weighted graph

Page 26: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

The Adjacency Matrix

(a) A directed graph and (b) its adjacency matrix.

Page 27: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

The Adjacency Matrix

• Adjacency matrix uses fixed amount of space– Depends on number of vertices– Does not depend on number of edges

• Typically the matrix will be sparse

• Presence of an edge between two vertices can be known immediately

• All neighbors of a vertex found by scanning entire row for that vertex

Page 28: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

The Adjacency List

Adjacency lists for the directed

graph

Page 29: Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form

The Adjacency List

• Represents only edges that originate from the vertex

• Space not reserved for edges that do not exist– Uses less memory than corresponding

adjacency matrix– Thus more often used than adjacency matrix