transforming infix to postfix
DESCRIPTION
Transforming Infix to Postfix. Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form. Infix-to-Postfix Algorithm. Checking for Balanced () , [] , {}. The contents of a stack during the scan of an expression that contains the balanced delimiters {[()]} - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/1.jpg)
Transforming Infix to Postfix
Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form.
![Page 2: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/10.jpg)
Traversals Exercise
• The order of these nodes being visited using 4 different traversal methods
![Page 11: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/12.jpg)
Binary Search Trees
Two binary search trees containing the same names as the tree in previous slide
![Page 13: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/15.jpg)
Removing an Entry, Node Has Two Children
(a) A binary search tree; (b) after removing Chad;
![Page 16: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/21.jpg)
Creating a Heap
The steps in adding 20, 40, 30, 10, 90, and 70 to a heap.
![Page 22: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/24.jpg)
Depth-First TraversalA trace of a depth-first traversal
beginning at vertex A of the directed graph
![Page 25: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/26.jpg)
The Adjacency Matrix
(a) A directed graph and (b) its adjacency matrix.
![Page 27: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/28.jpg)
The Adjacency List
Adjacency lists for the directed
graph
![Page 29: Transforming Infix to Postfix](https://reader033.vdocuments.us/reader033/viewer/2022061616/568135ef550346895d9d5fbd/html5/thumbnails/29.jpg)
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