1 / 29

Transforming Infix to Postfix

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 {[()]}

Download Presentation

Transforming Infix to Postfix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Infix-to-Postfix Algorithm

  3. 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}

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

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

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

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

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

  9. 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) Binary Trees

  10. Traversals Exercise • The order of these nodes being visited using 4 different traversal methods

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

  12. Binary Search Trees Two binary search trees containing the same names as the tree in previous slide

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

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

  15. Removing an Entry, Node Has Two Children (a) A binary search tree; (b) after removing Chad;

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

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

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

  19. Adding an Entry In Figure 1, the steps in adding 85 to the maxheap Begin at next available position for a leaf Follow path from this leaf toward root until find correct position for new entry

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

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

  22. Creating a Heap More efficient to use reheap than to use add The steps in creating a heap by using reheap.

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

  24. Depth-First Traversal A trace of a depth-first traversal beginning at vertex A of the directed graph

  25. Shortest Path in an Weighted Graph Finding the cheapest path from vertex A to vertex H in the weighted graph

  26. The Adjacency Matrix (a) A directed graph and (b) its adjacency matrix.

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

  28. The Adjacency List Adjacency lists for the directed graph

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

More Related