Program 5 - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Program 5

Description:

Start by putting a set of parenthesis around the infix. expression: ... Each character that is popped off should. be put onto the end of the postfix string. ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 36
Provided by: amyby
Category:

less

Transcript and Presenter's Notes

Title: Program 5


1
Program 5
  • Converting an infix expression to postfix
  • When evaluating an infix expression, the standard
    order
  • of operations are followed
  • 1. Expressions in parenthesis
  • 2. Multiplication/Division from left to right
  • 3. Addition/Subtraction from left to right

2
Program 5 converting infix to postfix
  • It's easy to see which operation is performed
    first,
  • second, etc...with an infix expression
  • (4 5) (1 2)
  • It becomes a little more difficult with postfix
    expressions
  • 4512

3
Program 5 converting infix to postfix
  • To account for this problem, the different
    operators and parenthesis
  • are assigned a priority value
  • operator priority
  • ( 0
  • ) 0
  • 1
  • - 1
  • 2
  • / 2
  • These priorities will be used in order to help
    determine when to pop
  • an item from a stack or when to push an item onto
    a stack.

4
Program 5 converting infix to postfix
  • Start by putting a set of parenthesis around the
    infix
  • expression
  • - the left one goes on the stack
  • - the right one goes on the end of the infix
    string
  • Process the infix expression one character at a
    time,
  • from left to right
  • - if the character is a digit or letter, put it
    on the end of
  • the postfix expression

5
Program 5 converting infix to postfix
  • Process the infix expression one character at a
    time,
  • from left to right
  • - if the character is a left parenthesis, push
    it on the
  • stack
  • - if the character is an operator, pop the stack
    until
  • the top value has a smaller priority than the
    current
  • character. Each character that is popped off
    should
  • be put onto the end of the postfix string.
    Once done
  • popping, push the operator onto the stack.

6
Program 5 converting infix to postfix
  • Process the infix expression one character at a
    time,
  • from left to right
  • - if the character is a right parenthesis, pop
    the stack
  • until the top value is a left parenthesis.
    Each value
  • that is popped off should be put onto the end
    of the
  • postfix string. Once done popping, pop off
    the left
  • parenthesis.

7
Program 5 converting infix to postfix
  • When implementing in C, two "parallel" stacks
    will
  • be used
  • 1. The first stack will hold the various
    operators and left parenthesis in the infix
    expression.
  • 2. The second stack will hold the priority for
    the various operators and left parenthesis.
  • Since these stacks are "parallel", every time one
    of
  • them is altered the other must also have the same
  • alteration applied.

8
Program 5 converting infix to postfix
  • Push a left parenthesis and its corresponding
    priority on the stacks
  • Append a right parenthesis onto the end of the
    infix expression
  • While the end of the infix expression has not
    been found
  • If the character in the infix expression is a
    letter or digit
  • Put it on the end of the postfix expression
  • Else if the character in the infix expression
    is a left parenthesis
  • Push it and its corresponding priority on the
    stacks
  • Else if the character in the infix expression
    is an operator
  • Find the priority of the current character in
    the infix expression
  • Find the priority of the top item on the
    character stack
  • While priority of top item on char stack gt
    priority of current char
  • Pop the character and integer stacks
  • Put the popped character on the end of the
    postfix expression

9
Program 5 converting infix to postfix
  • Else if the character in the infix expression is
    a right parenthesis
  • Find the top character on the character stack
  • While the top character is not a left
    parenthesis
  • Pop the character and integer stacks
  • Put the popped character on the end of the
    postfix expression
  • Find the new top character on the character
    stack
  • Endwhile
  • Pop the character and integer stacks to
    remove the left parenthesis
  • Endif
  • Endwhile

10
Program 5 Building an expression tree from a
postfix expression
  • This process requires a stack, but this one will
    hold tree nodes,
  • rather than data.
  • The postfix expression will be processed one
    character at a time
  • - if the character is a digit, then it is the
    beginning of a constant
  • value. Create a tree node that has a data
    field equal to the
  • constant value and push it on the stack.
  • NOTE Dont forget to ensure that processing
    continues with
  • the character AFTER the constant value.

11
Program 5 Building an expression tree from a
postfix expression
  • The postfix expression will be processed one
    character
  • at a time
  • - if the character is a letter( ie. a variable),
    create a
  • tree node that has a data field equal to the
    letter.
  • - if the character is an operator, create a tree
    node
  • that has a data field equal to the operator.
    Initialize
  • the new nodes right and left children by
    popping the
  • stack. Push the new node onto the stack.

12
Program 5 Building an expression tree from a
postfix expression
  • Once the entire postfix expression is processed,
    there
  • will be one node remaining on the stack. It is
    the root of
  • the tree.

13
C string class
  • A standardized string object that can be used in
    place of
  • character arrays.
  • Multiple Declaration Formats
  • string str This invokes the default constructor
    and
  • makes str an empty string.
  • string str (s) This creates and initializes str
    to contain a copy of
  • s, which may be a string or character array
  • string str (charAr, n) This creates and
    initializes str to contain a
  • copy of the 1st n characters of charAr

14
C string class
  • Reading a string object
  • getline(cin, str)
  • This extracts characters from cin and stores
    them in str until a newline character OR end of
    file is reached
  • getline(cin, str, delimiter)
  • This extracts characters from cin and stores
    them in str until the delimiter is found (not
    stored in str) OR end of file is reached
  • cin gtgt str
  • This extracts characters from cin and stores
    them in str until whitespace is found OR end of
    file is reached.

15
C string class
  • Printing a string
  • cout ltlt str
  • This inserts characters from str into cout
  • Assignment Operator Format
  • str value
  • Assigns a copy of value to str. value may be a
    string, char array, or char

16
C string class
  • Concatenation Operators
  • str value or value str
  • returns the result of concatenating str and
    value, which may be a string, char array, or
    character
  • str value
  • Appends a copy of value to str. value may be a
    string, char array, or character.

17
C string class
  • Subscript Notation
  • strp
  • returns a reference to the character stored in
    str at position p
  • Relational Operators
  • str lt s str lt s
  • str gt s str gt s
  • str s str ! s
  • s may be a string or char array

18
C string class
  • Useful methods
  • str.c_str()
  • converts the string object str to a null
    terminated char array
  • returns const char
  • str.substr(p, n)
  • returns a copy of a sub-string n characters
    long, starting at position p

19
AVL tree deletions
  • As with deletion from a binary search tree, a
    node is deleted from
  • an AVL tree using the standard inorder successor
    (predecessor)
  • logic for binary search trees.

Deleting 15 using inorder successor logic results
in
20
AVL tree deletions
  • But, just like insertion, deletion can cause an
    imbalance, which
  • will need to be fixed by applying one (or more)
    of the four
  • rotations.

Deleting 12 using inorder successor logic results
in
21
AVL tree deletions
  • What kind of rotation should be performed to fix
    the imbalance?

22
AVL tree deletions
  • If we identify the parent of the actual node that
    was deleted, then
  • - If the left child was deleted, the balance
    factor at the parent
  • decreased by 1.
  • - If the right child was deleted, the balance
    factor at the parent
  • increased by 1.
  • The change in balance factor can move up the tree
    from the
  • deleted node's parent all the way to the root.
    Therefore, it's
  • possible that a node's balance factor could
    become 2 or -2. If
  • this happens, then balance needs to be restored.

23
AVL tree deletions
  • Let A be the node where balance must be restored.
  • If the deleted node was in A's right subtree,
    then let B be the root
  • of A's left subtree. Then
  • - B has balance factor 0 or 1 after deletion --
    then perform
  • a single right rotation
  • - B has balance factor -1 after deletion -- then
    perform a
  • left-right rotation

24
AVL tree deletions
  • If the deleted node was in A's left subtree, then
    let B
  • be the root of A's right subtree. Then
  • - B has balance factor 0 or -1 after deletion
  • then perform a single left rotation
  • - B has balance factor 1 after deletion -- then
  • perform a right-left rotation

25
AVL tree deletions
After a single left rotation
26
AVL tree deletions
Delete 9 from the tree
27
AVL tree deletions
A is node 5. 9 was deleted from As right
subtree, so B is node 2, which has balance factor
-1.
28
AVL tree deletions
Is the tree balanced after the rotation?
29
AVL tree deletions
  • Unlike insertions, one rotation may not be enough
    to restore balance to the tree.
  • To fix the imbalance, find the next node where
    balance factor is bad (call this A)
  • - if As balance factor is positive, then let B
    be As left child
  • - if Bs left subtree height is larger than Bs
    right subtree height, then
  • perform a single right rotation
  • - if Bs right subtree height is larger than
    Bs left subtree height, then
  • perform a left-right rotation
  • - if Bs left and right subtrees have equal
    height, you may perform either
  • rotation

30
AVL tree deletions
  • To fix the imbalance, find the next node where
    balance factor is bad (call
  • this A)
  • - if As balance factor is negative, then let B
    be As right child
  • - if Bs right subtree height is larger than
    Bs left subtree height,
  • then perform a single left rotation
  • - if Bs left subtree height is larger than Bs
    right subtree height,
  • then perform a right-left rotation
  • - if Bs left and right subtrees have equal
    height, you may
  • perform either rotation

31
AVL tree deletions
What type of rotation must be performed to fix
the imbalance?
32
AVL tree deletions
A single left rotation with the highlighted
nodesOR
33
AVL tree deletions
A right-left rotation with the highlighted nodes
34
AVL tree deletions
Result after the single left rotation
35
AVL tree deletions
Result after the right-left rotation
Write a Comment
User Comments (0)
About PowerShow.com