25 Lecture

CS301

Midterm & Final Term Short Notes

Expression tree

An expression tree is a binary tree in which each internal node represents an arithmetic operator and each leaf node represents an operand. The tree is constructed by recursively applying the binary operator to the operands. Expression trees can


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is an expression tree? a) A binary tree with nodes representing operands b) A binary tree with nodes representing operators c) A binary tree with nodes representing both operands and operators d) A binary tree with nodes representing numbers

Answer: c

  1. What is the purpose of an expression tree? a) To represent a mathematical expression b) To store data in a tree structure c) To sort data in a binary tree d) To perform search operations on data in a binary tree

Answer: a

  1. Which traversal of an expression tree is used to evaluate the expression? a) Preorder b) Inorder c) Postorder d) Level order

Answer: c

  1. What is the time complexity of evaluating an expression tree? a) O(n) b) O(log n) c) O(n^2) d) O(2^n)

Answer: a

  1. How is an expression tree created from an infix expression? a) Using the preorder traversal b) Using the inorder traversal c) Using the postorder traversal d) Using a stack

Answer: d

  1. What is the maximum number of children a node in an expression tree can have? a) 0 b) 1 c) 2 d) 3

Answer: c

  1. Which of the following operations can be performed on an expression tree? a) Insertion of a node b) Deletion of a node c) Rotation of a node d) All of the above

Answer: d

  1. What is the purpose of a leaf node in an expression tree? a) To represent an operator b) To represent an operand c) To represent a binary operation d) To represent a unary operation

Answer: b

  1. Can an expression tree have duplicate nodes? a) Yes b) No

Answer: b

  1. What is the advantage of using an expression tree over a postfix expression? a) Faster evaluation b) Easier to read c) Takes less space d) All of the above

Answer: d



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is an expression tree? An expression tree is a binary tree representation of expressions, where the leaves are operands, and internal nodes are operators.

  2. How can we evaluate an expression tree? We can evaluate an expression tree recursively by evaluating the left and right subtrees and then applying the operator in the root.

  3. What is a prefix expression? A prefix expression is an expression where the operator comes before the operands, for example, + 2 3.

  4. How do we convert a prefix expression to an expression tree? We start from the leftmost operand and create a new node for each operator encountered, with the left child being the next operand and the right child being the next operator or operand.

  5. What is a postfix expression? A postfix expression is an expression where the operator comes after the operands, for example, 2 3 +.

  6. How do we convert a postfix expression to an expression tree? We start from the leftmost operand and create a new node for each operator encountered, with the right child being the next operand and the left child being the next operator or operand.

  7. What is an infix expression? An infix expression is an expression where the operator comes between the operands, for example, 2 + 3.

  8. How do we convert an infix expression to an expression tree? We use the shunting-yard algorithm to convert an infix expression to postfix notation and then create an expression tree from the postfix expression.

  9. What is the height of an expression tree? The height of an expression tree is the maximum depth of any leaf node in the tree.

  10. What is the time complexity of evaluating an expression tree? The time complexity of evaluating an expression tree is O(n), where n is the number of nodes in the tree.

Expression Tree, also known as an abstract syntax tree (AST), is a binary tree that is used to represent mathematical expressions. The tree consists of nodes that are either operators or operands. The leaves of the tree are the operands, while the internal nodes are the operators. The expression tree allows us to evaluate the expression in a natural and efficient way by traversing the tree in a post-order manner. To evaluate an expression, we traverse the tree in a post-order manner, performing the operation at each operator node and replacing the operator node with the result. We continue until we reach the root node, which will contain the final result. Expression trees can be constructed from infix expressions by converting them to postfix notation and then building the tree from the postfix expression. The postfix expression ensures that the operators are already in the correct order, making it easier to build the expression tree. The expression tree can also be used to convert the expression from infix to prefix or postfix notation. To convert an infix expression to a prefix or postfix expression, we can simply traverse the tree in the desired order and output the operators and operands as we encounter them. Overall, the expression tree is a powerful data structure for representing and manipulating mathematical expressions. It allows for efficient evaluation and conversion of expressions, making it a useful tool for a variety of applications.