0% found this document useful (0 votes)
85 views28 pages

Expression Trees Heap Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views28 pages

Expression Trees Heap Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Expression Tree :

• Expression tree is used to represent the various expressions.


• The expression tree is a binary tree with each internal node representing an operand, and
each leaf node representing an operator.

Properties of an expression tree


• Leaf nodes represent operands (numbers or variables).
• Internal nodes represent operators (such as +, -, *, /).
• Each operator in the tree corresponds to a sub-expression of the original expression.
• The structure of the tree preserves operator precedence, ensuring that the order of
operations is maintained.
• For example, the expression (3 + 5) * (2 - 1) can be represented as an expression tree where:
• The root is the multiplication operator ‘*’.
• The left child is the addition operator ‘+’, with operands ‘3’ and ‘5’ as its children.
• The right child is the subtraction operator’ -’, with operands ‘2’ and ‘1’ as its children.
Types of Expressions Represented by Expression Trees
1. Arithmetic Expressions Infix: 3 + 4 * 2
2. Boolean Expressions Infix: A AND B OR C
3. Relational Expressions Infix: A > B AND C < D
Construction of an Expression Tree
• An expression tree can be constructed using infix, prefix, or postfix notations.
Construction Process (Infix to Expression Tree) [use operator precedence table]
• Step 1: Start with the lowest precedence operator (if multiple operators are
present).
• Step 2: Make this operator the root of the tree.
• Step 3: The left subtree is created from the part of the expression to the left of the
operator, and the right subtree is created from the part of the expression to the
right of the operator.
• Step 4: Recursively apply this process to the left and right subexpressions.
Example: A + B * C
• The + has a lower precedence than *, so + becomes the root.
• Operand A becomes the left child of +.
• The expression B*C becomes the right subtree. Since * has the highest precedence,
it is the root of this subtree, and B and C are its children.
Construction Process (Postfix to Expression Tree)
• Step 1: Start from the left of the postfix expression and push operands onto a stack.
• Step 2: When an operator is encountered, pop the required number of operands (2 for
binary operators) from the stack, create a new node for the operator, and make the popped
operands its children.
• Step 3: Push the new operator node back onto the stack.
• Step 4: At the end of the expression, the stack will contain a single node, which is the root
of the expression tree.
• Example: A B C * +
• Push A, B, and C onto the stack.
• Pop B and C, create a node with *, and push it back onto the stack.
• Pop A and the * node, create a node with + and push it back onto the stack.
• The final stack contains the root of the tree.
Construction of Expression tree for given infix expression
(a+b) +(C/d)
((a+b)+c)+d
Given an expression, Exp = ((a + b) – (c * d)) % ((e ^f) / (g – h)), construct the corresponding
binary tree.
• (A+B*C)-((D*E+F)/G)
• FIRST CONVERT IT INTO POST FIX EXPRESSION
• ABC*+ DE* F+ G/-

Example 9.4 Given the expression, Exp = a + b / c * d – e, construct the corresponding binary
tree.
• Solution Use the operator precedence chart to find the sequence in which operations will be
performed.
• The given expression can be written as
• Exp = ((a + ((b/c) * d)) – e)
The infix expression is
E+(d-((a/b)*c))
=10+(30-((30/10)*2))
=10+(30-(3*2))
=10+(30-6)
=34

Draw the binary expression tree that represents the following postfix
expression:
AB+C*D–
A Heap data structure:
• A Heap is a complete binary tree data structure that satisfies the heap
property:

• It's a complete binary tree (meaning all levels are fully filled except possibly
the last, which is filled from left to right).

• 2 types of heap properties 1. max- heap 2. min heap property


• where the value of each node is either greater than or equal to (max-heap) the values of
its children .

• where the value of each node is either less than or equal to (min-heap) the values
of its children.
• BINARY HEAPS
• A binary heap is a complete binary tree in which every node satisfies the heap property
which states that:
• If B is a child of A, then key(A) ≥ key(B)
• This implies that elements at every node will be either greater than or equal to the element
at its left and right child.
• Thus, the root node has the highest key value in the heap.
• Such a heap is commonly known as a max-heap.

• Alternatively, elements at every node will be either less than or equal to the element at its
left and right child.
• Thus, the root has the lowest key value.
• Such a heap is called a min-heap.
• Heap sort is a comparison-based sorting algorithm that uses the heap data
structure to efficiently organize data.

• It operates by first building a heap (a special type of binary tree) from the
input data and then repeatedly removing the root (largest or smallest
element) and heapifying the remaining elements until all elements are sorted.

• Heapify: The process of rearranging the heap to maintain its heap property
after an element is removed or inserted.

• This algorithm has a time complexity of O(n log n), making it efficient for large
datasets.
How it works:

• Build the Heap: (insert heap)


• The algorithm first converts the input array into a heap, either a max-heap or a min-heap,
depending on the desired sorting order (ascending or descending).

• Extract (del heap) and Heapify:


• Repeatedly, the root element (largest or smallest in a max-heap or min-heap) is swapped
with the last element in the heap, and the heap is heapified to restore the heap property.

• Sort:
• This process is repeated until all elements are extracted and placed in their sorted order.
• Inserting a New Element in a Binary Heap
• Consider a max heap H with n elements.
• Inserting a new value into the heap is done in the following
• two steps:
1. Add the new value at the bottom of H in such a way that H is still a complete binary tree but
not necessarily a heap.
2. Let the new value rise to its appropriate place in H so that H now becomes a heap as well.
• To do this, compare the new value with its parent to check if they are in the correct order.
• If they are, then the procedure halts, else the new value and its parent’s value are swapped
and Step 2 is repeated.
Deleting an Element from a Binary Heap
• Consider a max heap H having n elements.
• An element is always deleted from the root of the heap. So, deleting an element from the
heap is done in the following three steps:
1. Replace the root node’s value with the last node’s value so that H is still a complete binary
tree but not necessarily a heap.
2. Delete the last node.
3. Sink down the new root node’s value so that H satisfies the heap property. In this step,
interchange the root node’s value with its child node’s value (whichever is largest among its
children).
Here, the value of root node = 54 and the
value of the last node = 11.
So, replace 54 with 11 and delete the last
node.
1. Value : 45
2. Value : 32
3. Value : 1
0 1 2 3 4 5 6 7 8 9
45 32 1 25 14 20 28 62 16 21

45

32 1
A heap data structure:
A Heap is a complete binary tree data structure that satisfies the
heap property: 0 1 2 3 4 5 6 7 8 9
45 32 1 25 14 20 28 62 16 21

45

32 1
4. Value : 25
25 14 20
5. Value : 14
6. Value : 20
20 is not following heap rule , node value must be greater than or equal
to children. But here 1 < 20 children so interchange 1, 20
A heap data structure:
A Heap is a complete binary tree data structure that satisfies the
heap property: 0 1 2 3 4 5 6 7 8 9
45 32 1 25 14 20 28 62 16 21

45

32 28
7: Value : 28
25 14 1 20
interchange 20 and 28
8 : Value : 62 62

Shows not following heap property so interchange 62 and 25 …


now also not following heap property interchange 62 and 32
now also not following heap property interchange 62 and 45
45
45

32 28
32 28
25 14 1 20
62 14 1 20
62
25

62
45

45 28
62 28
32 14 1 20
32 14 1 20
25
25

You might also like