Thursday, September 29, 2022

Data Structures and Algorithms (Some Questions for You - Answer to Question 9) - Tirthankar Pal - MBA from IIT Kharagpur, GATE, GMAT, IIT Written Test, Interview were a part of MBA Entrance, B.S. in Computer Science from NIELIT

 In a complete binary tree of depth d (complete including last level), give an expression to find the number of leaf nodes in the binary tree.

A node is a leaf node if both left and right child nodes of it are NULL. 
Here is an algorithm to get the leaf node count.

getLeafCount(node)
1) If node is NULL then return 0.
2) Else If left and right child nodes are NULL return 1.
3) Else recursively calculate leaf count of the tree using below formula.
    Leaf count of a tree = Leaf count of left subtree + 
                                 Leaf count of right subtree

Example Tree

Leaf count for the above tree is 3.


Output: 

The leaf count of binary tree is : 3

Time & Space Complexities: Since this program is similar to traversal of tree, time and space complexities will be same as Tree traversal 


Tirthankar Pal

MBA from IIT Kharagpur with GATE, GMAT, IIT Kharagpur Written Test, and Interview

2 year PGDM (E-Business) from Welingkar, Mumbai

4 years of Bachelor of Science (Hons) in Computer Science from the National Institute of Electronics and Information Technology

Google and Hubspot Certification

Brain Bench Certification in C++, VC++, Data Structure and Project Management

10 years of Experience in Software Development out of that 6 years 8 months in Wipro

Selected in Six World Class UK Universities:-

King's College London, Durham University, University of Exeter, University of Sheffield, University of Newcastle, University of Leeds


Data Structures and Algorithms (Some Questions for You - Answer to Question 8) - Tirthankar Pal - MBA from IIT Kharagpur, GATE, GMAT, IIT Written Test, Interview were a part of MBA Entrance, B.S. in Computer Science from NIELIT

Write an algorithm to evaluate a postfix expression

Evaluation of Postfix

Example -1:

Let the given expression be “2 3 1 * + 9 -“. We scan all elements one by one.
1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’
2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)
3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’
4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1 which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’.
5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’.
6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’.

7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’.

8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack) 

Example-2 : 456*+

StepInput symbolOperationStackCalculation
1.4Push4
2.5Push4,5
3.6Push4,5,6
4.*Pop( 2 elements) and evaluate45*6=30
5.Push result 304,30
6.+Pop( 2 elements) and evaluateEmpty4+30=34
7.Push result 3434
8.No more elements to popEmpty34(result)

Evaluation rule of a Postfix Expression states:

  1. While reading the expression from left to right, push the element in the stack if it is an operand.
  2. Pop the two operands from the stack, if the element is an operator and then evaluate it.
  3. Push back the result of the evaluation. Repeat it till the end of the expression.
  4. Algorithm
    1) Create a stack to store operands (or values).
    2) Scan the given expression and do following for every scanned element.

         a) If the element is a number, push it into the stack
         b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack
    3) When the expression is ended, the number in the stack is the final answer

    Tirthankar Pal

    MBA from IIT Kharagpur with GATE, GMAT, IIT Kharagpur Written Test, and Interview

    2 year PGDM (E-Business) from Welingkar, Mumbai

    4 years of Bachelor of Science (Hons) in Computer Science from the National Institute of Electronics and Information Technology

    Google and Hubspot Certification

    Brain Bench Certification in C++, VC++, Data Structure and Project Management

    10 years of Experience in Software Development out of that 6 years 8 months in Wipro

    Selected in Six World Class UK Universities:-

    King's College London, Durham University, University of Exeter, University of Sheffield, University of Newcastle, University of Leeds