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 9107083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:29:18+00:00 2026-06-17T02:29:18+00:00

Below is my code: class Node{ int value; Node left; Node right; Node parent;

  • 0

Below is my code:

class Node{
    int value;
    Node left;
    Node right;
    Node parent;
       //getters, setters
}

Create the Tree

    private static void createTree() throws FileNotFoundException{
    Map<String,Node> nodeMap = new HashMap<String,Node>();
    Scanner sc = new Scanner(new File("<location>"));
    int row =0;
    while(sc.hasNextLine()){
        String line = sc.nextLine();
        Scanner scanLine = new Scanner(line);
        System.out.println(line);
        int col =0;

        while(scanLine.hasNextInt()){
            int value = scanLine.nextInt();
            System.out.println(row+","+col+"="+value);
            Node node = new Node(value);
            nodeMap.put(row+","+col,node);
            if(row >0){
                if(col %2 ==0){
                    //left node
                    Node parent = nodeMap.get(row-1+","+col/2);
                    if(parent !=null){
                        node.setParent(parent);
                        parent.setLeft(node);
                    }
                }else{
                    //right node
                    Node parent = nodeMap.get(row-1+","+(col-1)/2);
                    if(parent !=null){
                        node.setParent(parent);
                        parent.setRight(node);
                    }
                }
            }
            col++;
        }
        row++;
    }
    System.out.println(nodeMap);
    Node root = nodeMap.get("0,0");
    traverseTree(root);
    System.out.println("sum="+sum);


}

Actual Traversal

    static int sum =0;
private static void traverseTree(Node n){
    if(n != null){
        sum+=n.value;
        traverseTree(n.left);
        traverseTree(n.right);
    }
}

I have 2 questions:

  1. Read the input and create the tree: I read it from a file and store
    the root node in the hashmap. What are the alternatives?

  2. In the recursive search, I am keeping the sum outside the function, so its possible to calculate the sum in a sequential fashion. Is it possible to keep the sum variable inside and return the total value at the end?

  • 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-17T02:29:19+00:00Added an answer on June 17, 2026 at 2:29 am

    I read it from a file and store the root node in the hashmap. What are the alternatives?

    How you are doing it seems pretty reasonable given the apparent input format, however I’d recommend using a List<List<Node>> rather than storing it with strings, as your performance may degrade fairly quickly because the strings are very similar, which may cause hash collisions, degrading your map’s performance greatly if the input is large.

    In the recursive search, I am keeping the sum outside the function, so its possible to calculate the sum in a sequential fashion. Is it possible to keep the sum variable inside and return the total value at the end?

    private static int traverseTree(Node n) {
        if (n != null) {
            return n.value + traverseTree(n.left) + traverseTree(n.right);
        } else {
            return 0;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Node class as follows: public class Node { private int value;
I have below code: class Program { static void Main(string[] args) { Task[] tasks
I am creating excel file using below code public class ResultSetToExcel { private HSSFWorkbook
I have the code below : public class Anything { public int Data {
The below code in Java throws Null pointer exception. public class New{ int i;
I having below code for different artifacts, Entity public class ChooseFirst { public int
I have a class as below: public class Node { public int NodeID {
I can't understand how works the code below: class Host: name = None fileList
Hi i am working on scrapy Below is my code class examplespider(CrawlSpider): name =
I'm getting this error message with the code below: class Money { public: Money(float

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.