Draw the binary tree representation of the following arithmetic expression
CSCI 321 Computer Science III Spring 2019
Lecture 3 Activity 2
1. Draw the binary tree representation of the following arithmetic expression: “(((5+2) ∗ (2−1))/((2+9)+((7−2)−1)) ∗8)”. Hint: Review the print arithmetic expression example on Chapter Slide 26.
2. Give an O(n)-time algorithm for computing the depths of all nodes of a tree T, where n is the number of nodes of T. Write down the pseudocode. Hint: when you visit a node, you can perform some operation, for example, store some information in the node.
Hint
Management "1.) A binary tree is a tree in which each internal node has at most two children, the children of a node are an ordered pair (like left and right). All the children of an internal node left child and right child are called.Binary tree associated with an arithmetic expression has i) internal nodes: operators and ii) external nodes: operands. ...
"1.) A binary tree is a tree in which each internal node has at most two children, the children of a node are an ordered pair (like left and right). All the children of an internal node left child and right child are called.
Binary tree associated with an arithmetic expression has i) internal nodes: operators and ii) external nodes: operands.
2.) In computer science, a tree is an abstract model of a hierarchical structure which consists of nodes with a parent-child
relation.
The depth of a tree can be calculated like the example given below:
void updateDepth(Node node, int depth)
{
if (node != null)
{
node.depth = depth;
updateDepth(node.left, depth + 1); // left sub-tree
updateDepth(node.right, depth + 1); // right sub-tree