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.
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}
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
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.
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.
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
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
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)
Traversals Exercise The order of these nodes being visited using 4 different traversal methods
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
Binary Search Trees Two binary search trees containing the same names as the tree in previous slide
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.
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.
Removing an Entry, Node Has Two Children (a) A binary search tree; (b) after removing Chad;
Heaps A complete binary tree Maxheap Minheap 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
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
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.
In Figure 1, the steps in adding 85 to the maxheap 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
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
The steps in adding 20, 40, 30, 10, 90, and 70 to a heap. Creating a Heap The steps in adding 20, 40, 30, 10, 90, and 70 to a heap.
Creating a Heap The steps in creating a heap by using reheap. More efficient to use reheap than to use add The steps in creating a heap by using reheap.
Breadth-First Traversal (ctd.) A trace of a breadth-first traversal for a directed graph, beginning at vertex A.
Depth-First Traversal A trace of a depth-first traversal beginning at vertex A of the directed graph
Shortest Path in an Weighted Graph Finding the cheapest path from vertex A to vertex H in the weighted graph
(a) A directed graph and (b) its adjacency matrix. The Adjacency Matrix (a) A directed graph and (b) its adjacency matrix.
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
Adjacency lists for the directed graph The Adjacency List Adjacency lists for the directed graph
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