Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:35:07+00:00 2026-06-01T18:35:07+00:00

There are two related excises in The Algorithm Design Manual . Basically, I know

  • 0

There are two related excises in The Algorithm Design Manual.

Basically, I know how to solve the first excise, but I don’t know how to solve the 2nd one using the first one’s solution as a hint.

Excise of an arithmetic expression tree

arithmetic expression tree

Above is an arithmetic expression tree. Suppose an arithmetic
expression is given as a tree. Each leaf is an integer and each
internal node is one of the standard arithmetical operations
(+,−,∗,/). For example, the expression 2 + 3 ∗ 4 + (3 ∗ 4)/5 is
represented by the tree in above Figure. Give an O(n) algorithm for
evaluating such an expression, where there are n nodes in the tree.

Ok, this is not hard. My solution is like this:

    public float evaluate() {
        return evaluate(root);
    }

    private float evaluate(Node_EX _node) {
        if (_node.left == null || _node.right == null)
            return Float.parseFloat(_node.key);
        String op = _node.key;
        if (op == "+") {
            return evaluate(_node.left) + evaluate(_node.right);
        } else if (op == "-") {
            return evaluate(_node.left) - evaluate(_node.right);
        } else if (op == "*") {
            return evaluate(_node.left) * evaluate(_node.right);
        } else {
            return evaluate(_node.left) / evaluate(_node.right);
        }
    }

I just use recursive way to solve the expression tree for the result.

Excise of an arithmetic expression DAG

arithmetic expression dag

Suppose an arithmetic expression is given as a DAG (directed acyclic
graph) with common subexpressions removed. Each leaf is an integer and
each internal node is one of the standard arithmetical operations
(+,−,∗,/). For example, the expression 2 + 3 ∗ 4 + (3 ∗ 4)/5 is
represented by the DAG in above Figure. Give an O(n + m) algorithm
for evaluating such a DAG, where there are n nodes and m edges in the
DAG. Hint: modify an algorithm for the tree case to achieve the
desired efficiency.

Ok, there is such a hint in the description: Hint: modify an algorithm for the tree case to achieve the desired efficiency.

I am quite confused by this hint, actually. For a typical tree related thing, we normally can use recursive to solve. However, if this is a graph, my first intuitive is to use BFS or DFS to solve it. Then how can I relate BFS or DFS to the tree, though DFS is actually a recursive?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:35:07+00:00Added an answer on June 1, 2026 at 6:35 pm

    I believe, to achieve the desired efficiency, the problem wants you to avoid re-evaluating parts of the tree you’ve already visited. Once you’ve reached and evaluated some sub-tree in the DAG (every node in the tree represents the sub-tree rooted at that node), you can store the resulting value and associate it with that sub-tree. Then, when you visit it again, you can check whether you’ve pre-computed that value and just retrieve it rather than evaluating it again.

    There are many different ways you can store and retrieve these values a simple one being to modify the structure of a node to allow for a cacheable result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am newbie to Linux Kernel. I know that there are two context 1.
In my database I have two closely related tables. There is a frequently called
we have two related tables(inventory and inventoryLocalization) on Sql Server 2005. the first table's
I see an error message related to transaction isolation levels. There are two tables
There are sort of two related questions here: A) How is enum implemented? For
I have two questions related to CUDA 4.0 Peer access: Is there any way
I have a situation where there two related large python classes and hence i
I have two related questions. First, i have a some information such as: (732,
I'm trying to export and import two related models using FasterCSV. The first model
I have read a document that they say: In java there two types of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.