You are on page 1of 37

Data Structures and File Management-II

(ITT 301)
Unit 1 - Part 1
Advanced Linear Structures
Lecturer: Cecil White
UNIVERSITY OF THE COMMONWEALTH CARIBBEAN
Specific Objectives
• Evaluate infix to prefix and postfix expressions.
• Construct an expression tree from infix expression.
• Implement double linked list and circular linked list with algorithm.
• Implement priority and circular queues.

Unit 1: Advanced Linear Structures - Part 1 2


Introduction
• Hello students, let us embark on an exciting journey towards learning
the subject of linear data structures.
• We begin the subject by discussing the rules for converting a
expression from infix to prefix and postfix formats.
• We also focus on constructing the expression tree from infix
expression.
• Later, we explore the concepts of the different data structures such as
doubly linked list, circular linked list, priority queue and circular
queue.
• Lastly we will implement the data structures in C++ program.

Unit 1: Advanced Linear Structures - Part 1 3


Infix, Prefix and Postfix
• A mathematical expression is written in the form of 3 * 2 / (5 + 45) % 10.
• What would be the answer?
• There is no need to think about how you are going to compute the result
because no matter what you do, it will not be the best algorithm for
calculating expressions.
• That is because there is a lot of overhead in computing the result for
expressions which are expressed in this form; which results in a loss of
efficiency.
• The expression that you see above is known as Infix Notation.

Unit 1: Advanced Linear Structures - Part 1 4


Infix, Prefix and Postfix (Cont.)
• It is a convention which humans are familiar with and is easier to
read.
• But for a computer, calculating the result from an expression in this
form is difficult.
• The Prefix and Postfix Notations make the evaluation procedure really
simple.
• This means that your program would have to have two separate
functions, one to convert the infix expression to post or prefix and the
second to compute the result from the converted expression

Unit 1: Advanced Linear Structures - Part 1 5


Infix, Prefix and Postfix (Cont.)
Infix notation
• Consider a simple expression : A + B
• This notation is called Infix Notation.
• This notation is the standard way in which we write expressions.
• The rule for infix notation is that the operator is placed between the
operands.
Prefix notation
• Consider a simple expression : A + B
• The equivalent prefix notation is + A B
• The rule for prefix notation is that the operator precedes the
operands.
Unit 1: Advanced Linear Structures - Part 1 6
Infix, Prefix and Postfix (Cont.)
Postfix notation
• Consider a simple expression : A + B
• The equivalent postfix notation is A B +
• The rule for postfix notation is that the operator is placed after the
operands.
• Postfix is also known as Reverse Polish.

Unit 1: Advanced Linear Structures - Part 1 7


Infix, Prefix and Postfix (Cont.)
Postfix notation
• Consider a simple expression : A + B
• The equivalent postfix notation is A B +
• The rule for postfix notation is that the operator is placed after the
operands.
• Postfix is also known as Reverse Polish.
• So if X1 and X2 are two operands and OP is a given operator then
infix prefix postfix
X1 OP X2 = OP X1 X2 = X1 X2 OP

Unit 1: Advanced Linear Structures - Part 1 8


Infix, Prefix and Postfix (Cont.)
• For computers to perform quick calculations, they must work on
prefix or postfix expressions.
• Example:-
• The expression is A + B / C
• First we need to use the BODMAS rule (This rule states that each
operator has its own priority and the operators with the highest
priority are evaluated first).
• The operator priority in descending order is
• B O D M A S (Bracket Open -> Division -> Multiplication -> Addition ->
Subtraction)
• So in this example, B / C is evaluated first and the result is added to A.
Unit 1: Advanced Linear Structures - Part 1 9
Infix, Prefix and Postfix (Cont.)
Convert into Postfix
• To convert A + B / C into postfix, we convert one operation at a time.
• The operators with maximum priority are converted first followed by
those which are placed lower in the order.
• Hence, A + B / C can be converted into postfix as follows:-
• A + B / C (First Convert B / C -> B C /)
• A + B C / (Next Operation is A + (BC/) -> A BC/ +)
• A B C / + (Resultant Postfix Form)

Unit 1: Advanced Linear Structures - Part 1 10


Infix, Prefix and Postfix (Cont.)
Convert into Prefix
• The same procedure is to be applied to convert infix into prefix except
that during conversion of a single operation, the operator is placed
before the two operands.
• Let us take the same example and convert it to Prefix Notation.
• A + B / C (First Convert B / C -> / B C)
• A + / B C (Next Operation is A + (/BC) -> + A /BC
• + A / B C (Resultant Prefix Form)

Unit 1: Advanced Linear Structures - Part 1 11


Infix, Prefix and Postfix (Cont.)
Example:-
[INFIX TO POSTFIX]
• The expression is A + B * ( C + D )
Solution:-
• A+B*CD+
• A+BCD+*
• ABCD+*+

Unit 1: Advanced Linear Structures - Part 1 12


Infix, Prefix and Postfix (Cont.)
Example:-
[INFIX TO PREFIX]
• The expression is A + B * ( C + D )
Solution:-
• A + B * (+ C D)
• A + * B +C D
• +A*B+CD

Unit 1: Advanced Linear Structures - Part 1 13


Infix, Prefix and Postfix (Cont.)
Example:-
[INFIX TO PREFIX]
• The expression is A + B * ( C + D )
Solution:-
• A + B * (+ C D)
• A + * B +C D
• +A*B+CD

• Complete Tutorial 1 (Found in Schoology)

Unit 1: Advanced Linear Structures - Part 1 14


Expression Tree
• An expression tree is a data structure used to represent an arithmetic
expression.
• There are two types of nodes in an expression tree: operators and
numbers.
• An operator node will have children that are also expressions. So for
example, the expression (4 - 2) will be represented by an operator
node.
• It will store the fact that it's the subtraction operator, and it will have
two children, each number node (leaves), 4 and 2.

Unit 1: Advanced Linear Structures - Part 1 15


Expression Tree (Cont.)
• A more complicated expression will have more nodes associated with
it.
• For example, consider the expression (2 + 6) / (9 % 5)
• Each operator has two operands, a left and a right operand.
• To represent the expression, put each operator in a node, and put
each operand as a left or right child (as appropriate)
• In an expression tree, leaves are always numbers, and interior nodes
are always operations.

Unit 1: Advanced Linear Structures - Part 1 16


Expression Tree (Cont.)
• Given a node, there is one of two ways to evaluate it:
• If it is a leaf node, simply return the number stored in that node.
• If it is an interior node, it is an operation.
• Evaluate the left and right subtrees and then apply the operation to
the left and right results. Return the result.

Unit 1: Advanced Linear Structures - Part 1 17


Postfix to Expression Tree Rules
• Terms: Append x to y means add child x to node y.
• Rule1: Operators can have children but operands cannot.
• Rule2: Nodes can only have 2 children.
• Rule3: When appending nodes, always append to right first.
• If right is occupied, then append to left.

Unit 1: Advanced Linear Structures - Part 1 18


Postfix to Expression Tree Algorithm
• 1. Get the last symbol (rightmost) of postfix notation, create a node
for it and designate the new node as the root.
• 2. Set the root node as current node.
• 3. For each element from right to left (excluding the last):
• 3.1 Create a node for it.
• 3.2 If current node cannot have more children, search for the first
parent/grandparent that can have more children and set it as the
current node.
• 3.3 Append the new node to the current node.
• 3.4 Set new node as current node.

Unit 1: Advanced Linear Structures - Part 1 19


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 20


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 21


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 22


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 23


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 24


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 25


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 26


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 27


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 28


Postfix to Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 29


Prefix from Expression Tree
• Rule 1: Start at the root node.
• Rule 2: When node is visited for the first time, output value of node.
• Rule 3: Go from left to right when visiting children.
• Rule 4: If left child has children, visit their children first before going
to right child.
• Rule 5: Prefix notation is complete when every node is visited.

Unit 1: Advanced Linear Structures - Part 1 30


Prefix from Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 31


Prefix from Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 32


Prefix from Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 33


Prefix from Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 34


Prefix from Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 35


Prefix from Expression Tree (Step by Step)

Unit 1: Advanced Linear Structures - Part 1 36


Prefix from Expression Tree (Step by Step)

Complete Tutorial 2 (Found in Schoology)

Unit 1: Advanced Linear Structures - Part 1 37

You might also like